Overview
Java defines a class (java.text.SimpleDateFormat
) that provides a convenient way to convert strings with date and/or time information so that it appears in your desired format.
Date and
...
time patterns
Date and time formats are specified by with date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A'
to 'Z'
and from 'a'
to 'z'
are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using with single quotes ('
) to avoid interpretation. "''"
represents a single quote. All other characters are not interpreted; they 're are simply copied into the output string during formatting or matched against the input string during parsing.
The following pattern letters are defined (all other characters from 'A'
to 'Z'
and from 'a'
to 'z'
are reserved). :
Table plus |
---|
highlightColor | #ffffec |
---|
rowStyles | background:#f0f0f0;color:#222;font-size:14px;border-bottom:black 2px solid;border-top:black 2px solid;, |
---|
style | background-color:white; |
---|
autoNumber | true |
---|
|
Letter | Date or Time Componenttime component | Examples |
---|
G | Era designator | GG → AD | y | Year | "yy" → 14 "yyyy" → 2014 | M | Month in year | "M" → 3 "MM" → 03 "MMM" → Mar "MMMM" → March | w | Week in year | "w" → 09 09 | W | Week in month | "W" → 1 | D | Day in year | "D" → 60 "DDD" → 060 | d | Day in month | "d" → 1 "dd" → 01 | F | Day of week in month | "F" → 1 | E | Day in week | "EEE" → Sat "EEEE" → Saturday | a | Am/pm marker | "a" → PM "aa" → | H | Hour in day (0-23) | "H" → 8 8 "HH" → 08 | k | Hour in day (1-24) | "k" → 8 "kk" → 08 | K | Hour in am/pm (0-11 AM/PM) | "K" → 8 "KK" → 08 08 | h | Hour in am/pm (1-12, AM/PM) | "h" → 8 "hh" → 08 08 | m | Minute in hour | "m" → 6 6 "mm" → 06 | s | Second in minute | "s" → 5 "ss" → 05 05 | S | Millsecond (0-999) | "SSS" → 978 | z | Time zone | "z" → EST "zz" → EST "zzzz" → Eastern Standard Time Time | Z | Time zone | -0800 |
|
Examples
The following examples show how date and time patterns are interpreted in the U.S. locale. The given For this example, consider the date and time are as 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone. The following examples show how date and time patterns are interpreted in the U.S. locale:
Table plus |
---|
highlightColor | #ffffec |
---|
rowStyles | background:#f0f0f0;color:#222;font-size:14px;border-bottom:black 2px solid;border-top:black 2px solid;, |
---|
style | background-color:white; |
---|
autoNumber | true |
---|
|
Date and Time Patterntime pattern | Result |
---|
"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 | "EEE, dd MMM yyyy HH:mm:ss z" | Wed, 04 July 2001 12:08:56 PST |
|
Tip |
---|
You can test out a Date date and Time Pattern time pattern using this online tool. |
Additional
...
references
JavaDoc for java.text.SimpleDateFormat class