-
public class
SimpleDateFormat extends DateFormat
SimpleDateFormat
is a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date-and-text), parsing (text, date), and normalization.
SimpleDateFormat
allows you to start by selecting any user-specified method for date-time formatting. However, want to use dateformat
gettimeinstance
, getdateinstance
or getdatetimeinstance
Create a date-time formatter. Each class method returns a date/time formatter initialized in the default format. You can modify the formatting by the Applypattern
method as needed.
SimpleDateFormat function Inheritance relationship:
java.lang.object
|
+----Java.text.Format
|
+----Java.text.DateFormat
|
+----Java.text.SimpleDateFormat
Below is a small example:
Import java.text.*;
Import java.util.Date;
/**
SimpleDateFormat function Syntax:
G-Age Marker
Y years
M-month
D-Day
h in the morning or afternoon (1~12)
H at the time of Day (0~23)
M min
s S
S ms
E-week
D the first day of the year
F January The first few weeks
W The first few weeks of the year
W January The first few weeks
A morning/afternoon marker
K in one day (1~24)
K in the morning or afternoon (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 mm min 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 years mm DD Day hh mm min ss sec E");
SimpleDateFormat myfmt4=new SimpleDateFormat (
"D-day of the year, the first W week of January, the first W week of the year, the K-time Z time zone in a day");
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:
December 16, 2004 17:24 27 seconds
04/12/16 17:24
2004-12-16 17:24:27
December 16, 2004 17:24 27 sec Thu
No. 351 Day of the Year 51st Monday Month 3rd week in one day 17 o'clock CST time zone
Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 17:24:27 CST 2004
JAVA Date parsing (timestamp resolved to fixed format)