Java Tutorial/Development/DateFormat

Материал из Java эксперт
Перейти к: навигация, поиск

Convert Date to String

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    Date today = Calendar.getInstance().getTime();
    String reportDate = df.format(today);
    System.out.println("Report Date: " + reportDate);
  }
}





Convert string date to long value

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
  public static void main(String[] args) throws Exception {
    String today = "21/12/2007";
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date date = formatter.parse(today);
    long dateInLong = date.getTime();
    System.out.println("date = " + date);
    System.out.println("dateInLong = " + dateInLong);
  }
}





DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.CANADA).format(new Date())

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    String s = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.CANADA).format(new Date());
    System.out.println(s);
  }
}





DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA).format(new Date())

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    String s = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA).format(new Date());
    System.out.println(s);
  }
}





DateFormat.getTimeInstance(DateFormat.LONG, Locale.CANADA).format(new Date())

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    String s = DateFormat.getTimeInstance(DateFormat.LONG, Locale.CANADA).format(new Date());
    System.out.println(s);
  }
}





DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CANADA).format(new Date())

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    String s = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CANADA).format(new Date());
    System.out.println(s);
  }
}





DateFormat.SHORT

import java.text.DateFormat;
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    String strDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
    System.out.println(strDate);
  }
}





Find the current date format

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Main {
  public static void main(String args[]) {
    SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT);
    System.out.println("The short date format is  " + df.toPattern());
    Locale loc = Locale.ITALY;
    df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, loc);
    System.out.println("The short date format is  " + df.toPattern());
  }
}





Format a date-time value:short, medium and long

import java.text.DateFormat;
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    // Format date in a short format
    String today = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date);
    System.out.println("Today " + today);
    // Format date in a medium format
    today = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(date);
    System.out.println("Today " + today);
    // Format date in a long format
    today = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date);
    System.out.println("Today " + today);
  }
}





Format date in Default format

import java.text.DateFormat;
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    String strDate = DateFormat.getDateInstance(DateFormat.DEFAULT).format(date);
    System.out.println(strDate);
  }
}





Format date in Full format

import java.text.DateFormat;
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    
    String strDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);
    System.out.println(strDate);
  }
}





Format date in Long format

import java.text.DateFormat;
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    String strDate = DateFormat.getDateInstance(DateFormat.LONG).format(date);
    System.out.println(strDate);
  }
}





Format date in Medium format

import java.text.DateFormat;
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    String strDate = DateFormat.getDateInstance(DateFormat.MEDIUM).format(date);
    System.out.println(strDate);
  }
}





Formatting and Parsing a Time for a Locale: specify the locale when creating the SimpleDateFormat object.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Locale locale = Locale.FRENCH;
    DateFormat formatter = new SimpleDateFormat("HH:mm:ss zzzz", locale);
    String s = formatter.format(new Date());
    System.out.println(s);
  }
}





Formatting and Parsing a Time for a Locale Using Default Formats

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Locale locale = Locale.ITALIAN;
    Date date = new Date();
    String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
    System.out.println(s);
  }
}





Format with a default format

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    String s = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CANADA).format(new Date());
    System.out.println(s);
  }
}





Parse date string input with DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.CANADA)

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Date date = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.CANADA).parse("21.33.03");
    System.out.println(date);
  }
}





Validate a date Using DateFormat

import java.text.DateFormat;
public class Main {
  public static boolean isValidDateStr(String date) throws Exception {
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
    df.setLenient(false);
    df.parse(date);
    return true;
  }
  public static void main(String[] args) throws Exception{
    System.out.println(isValidDateStr("1900-13-12"));
  }
}