SimpleDateFormat is a class to solve our time format problem.
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-dependent manner. It allows formatting (date->text), parsing (text->date) and normalization. SimpleDateFormat enables the selection of any user-defined date-time format pattern.
Date and time patterns Date and time formats are specified by date and time pattern strings. In date and time pattern strings, the unquoted letters 'A' through 'Z' and 'a' through 'z' are interpreted as pattern letters and are used to represent date or time string elements. Text can be enclosed in single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they are simply copied to the output string during formatting, or matched against the input string during parsing.
The following pattern letters are defined (all other characters 'A' to 'Z' and 'a' to 'z' are reserved):
letter date or time element representation example
G Era identifier Text AD
Year y Year 1996; 96
M month in year Month July; Jul; 07
w Number of weeks in year Number 27
W Number of weeks in month Number 2
D Number of days in the year Number 189
d Number of days in the month Number 10
F Week of the month Number 2
E Day of the week Text Tuesday; Tue
a Am/pm Marker Text PM
H Hour of the day (0-23) Number 0
k Hour of the day (1-24) Number 24
Number of hours in K am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Number of minutes in an hour Number 30
s Seconds in minutes Number 55
S Number of milliseconds Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z time zone RFC 822 time zone -0800
The following example shows how to interpret date and time patterns in the US locale. The given date and time is 2001-07-04 12:08:56 local time in the United States Pacific Time Zone
Date and time pattern results
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700
Practical application examples of SimpleDateFormat in programming:
(1) Formatting (Date->Text)
Generally, commonly used date and time in Chinese are: 20070719 20:29:30
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("Date to String "+formater.format(new Date()));
Similar common forms include yyMMdd hh:mm:ss yyyy-MM-dd hh:mm:ss dd-MM-yyyy hh:mm:ss
But it is worth noting that this format (19JUL07) and its similar forms have a little trick 19JUL07
SimpleDateFormat formater = new SimpleDateFormat("ddMMMyy",,new Locale("US"))
System.out.println("Date to String "+formater.format(new Date()).toUpperCase());
Since it is in English, the Locale object parameter should be used, otherwise the default local Locale is used.
(2) Analysis (Text->Date)
Generally speaking, you can use whatever form of date and time mode and text parameters you want to get the time in the desired format. For example, 2007-7-19 will return a java.util.Date type time object, and the hours, minutes, and seconds will be filled with 0s.
formater = new SimpleDateFormat("yyyyMMMdd",new Locale("US"));
try...{
System.out.println("String to Date "+formater.parse("2007sep01"));
} catch (ParseException e) ...{
e.printStackTrace();
}
In particular,
formater = new SimpleDateFormat("yyyyMMMdd",new Locale("US"));
try...{
System.out.println("String to Date "+formater.parse("2007sep01"));
} catch (ParseException e) ...{
e.printStackTrace();
}
It also returns a date object of type java.util.Date, with hours, minutes and seconds padded with 0s.
(3) Text -> Timestamp, Date -> Timestamp
Timestamp t;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try...{
t = new Timestamp(format.parse("2007-07-19 00:00:00").getTime());
} catch (ParseException e) ...{
e.printStackTrace();
}
Timestamp t;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
t = new Timestamp(new Date().getTime