In Java, how can we convert a date to the desired format, or convert a string to a date in a certain format, such as converting a date or time in the database into a desired format, java provides the simpledateformat class for implementation. The following is the usage and example of simpledateformat:
Java. Lang. Object
Java. Text. Format
Java. Text. dateformat
Java. Text. simpledateformat
All Implemented interfaces:
Serializable, cloneable
Simpledateformat is a specific class related to the language environment to format and parse dates. It allows formatting (date-> text), parsing (text-> date), and normalization.
Simpledateformat allows you to select any custom date-Time Format mode. However, we recommend that you use gettimeinstance, getdateinstance, or getdatetimeinstance in dateformat to create a date-time formatter. Every such method can return a date/time formatter initialized in the default format.
Package Org. BUPT. xiaoye. chapter3; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. date; public class dateformattest {public static void main (string [] ARGs) throws parseexception {simpledateformat ceshifmt0 = new simpledateformat ("mm minutes SS seconds ON dd, mm, gyyyy"); simpledateformat ceshifmt1 = new simpledateformat ("yyyy/mm/dd hh: MM "); simpledateformat ceshifmt2 = new simpledateformat (" yyyy-mm-dd hh: mm: SS "); simpledateformat ceshifmt3 = new simpledateformat ("mm: DD, yyyy, HH, MM: SS seconds e"); simpledateformat ceshifmt4 = new simpledateformat ("yyyy/mm/dd e "); simpledateformat ceshifmt5 = new simpledateformat ("day d in a year, week W, week W in a month, and Time Zone Z in K"); date now = new date (); system. out. println (ceshifmt0.format (now); system. out. println (ceshifmt1.format (now); system. out. println (ceshifmt2.format (now); system. out. println (ceshifmt3.format (now); system. out. println (ceshifmt4.format (now); system. out. println (ceshifmt5.format (now ));}}
Running result:
October 19, 2014 AD
2014-10-19 18:08:08
Sunday, December 18, October 19, 2014
Sunday
292nd days in a year, 43rd weeks, 4th weeks in a month, 18 hours CST Time Zone
Simpledateformat:
G-era identifier
Y years
M month
D
H: In the morning or afternoon (1 ~ 12)
H: In a day (0 ~ 23)
MB
S seconds
S millisecond
E week
D. day of the year
F The day of the week in January
W. week of the year
W. week of March January
A morning/afternoon Mark
K hours in a day (1 ~ 24)
K hours in the morning or afternoon (0 ~ 11)
Z Time Zone
How to Use simpledateformat format