- public class SimpleDateFormat extends DateFormat
SimpleDateFormat
is a concrete class for formatting and analyzing data in a country-sensitive manner. It allows formatting (date -> text), parsing (text -> date) and normalization.
SimpleDateFormat
Allows selection of any user-specified startup for date-time formatting. However, would like to use DateFormat
in getTimeInstance
, getDateInstance
or getDateTimeInstance
create a date-time formatter. Each class method returns a date/time formatter initialized in the default format. applyPattern
Formatting can be modified with methods as needed .
The inheritance relationship of the SimpleDateFormat function:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
The following is a small example:
import java.text.*;
import java.util.Date;
/**
SimpleDateFormat function syntax:
G year identifier
y year
M month
d day
h hour in the morning or afternoon (1~12)
H hour in the day (0~23)
m minutes
s seconds
S milliseconds
E week
D of the year The day of the
month F The week of the month
w The week of the year
W The week of the month
a AM/PM marker
k o'clock in the day (1~24)
K o'clock in the morning or PM (0~11)
z time zone
*/
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy year MM month dd day HH hours mm minutes ss seconds");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm") ;
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//equivalent to now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy year MM month dd day HH hour mm minute ss second E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"The D day of the year, the wth Monday of the year, the Wth week of the month, the k time of the day in the z time zone");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString( ));
}
}
effect:
Dec 16 2004
17:24:27 04/12/16 17:24
2004-12-16 17:24:27
Dec 16 2004 17:24:27 Thurs
Day 351 of the year 51st Monday of the year 3rd week of the month at 17:00 CST time zone
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
The following is a JavaBean:
public class FormatDateTime {
public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy year MM month dd day HH hour mm minute ss second E ");
return myFmt.format(dt);
}
public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分");
return myFmt.format(dt);
}
public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}
public static void main(String[] args) {
Date now=new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime. toShortTimeString(now));
}
}
Main test result called: Dec 16 17:38:26
2004 Thu
Dec 16 17:38
17 38 26 0965
04/12/16 17:38