Skip to main content
Updated links as per current state of technology.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k

Simply format the date using DateTimeFormatterDateTimeFormatter with a pattern matching the input string (the tutorial is available here).

Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter)LocalDateTime#parse(text, formatter) instead of LocalDate#parse(text, formatter)LocalDate#parse(text, formatter). And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter)ZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadocthe javadoc, listing all available format patterns:

Do note that it has several predefined formatterspredefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

For a particular input string format, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text)LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIMEISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text)LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATEISO_LOCAL_DATE), and ZonedDateTime#parse(text)ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIMEISO_ZONED_DATE_TIME).

In case you're not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormatSimpleDateFormat using a format pattern matching the input string.

Note the importance of the explicit Locale argument. If you omit it, then it will use the default localedefault locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadocthe javadoc, listing all available format patterns:

Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here).

Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter) instead of LocalDate#parse(text, formatter). And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Do note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

For a particular input string format, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME).

In case you're not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormat using a format pattern matching the input string.

Note the importance of the explicit Locale argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here).

Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter) instead of LocalDate#parse(text, formatter). And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Do note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

For a particular input string format, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME).

In case you're not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormat using a format pattern matching the input string.

Note the importance of the explicit Locale argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Advocate Java 8 approach.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k

That's the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole Simply format the date using SimpleDateFormat using a format pattern matching the input stringjava.util.Date class was de-facto deprecated (discommended) since introduction of java.time API in Java 8 (2014).

Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here).

String string = "January 2, 2010";
DateFormatDateTimeFormatter formatformatter = new SimpleDateFormatDateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
DateLocalDate date = formatLocalDate.parse(string, formatter);
System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010-01-02

Note the importance of: if your format pattern happens to contain the explicit Locale argument. If you omit ittime part as well, then it will use the default locale which is not necessarily English as used in the month nameLocalDateTime#parse(text, formatter) instead of the input stringLocalDate#parse(text, formatter). If the locale doesn't match withAnd, if your format pattern happens to contain the input stringtime zone as well, then you would confusingly get ause java.text.ParseException even though when the format pattern seems validZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadocthe javadoc, listing all available format patterns:

LetterSymbol Date or Time ComponentMeaning Presentation Examples
G Era designatorera Texttext ADAD; Anno Domini; A
uyearyear2004; 04
y Yearyear-of-era Yearyear 1996; 962004; 04
YD Week yearday-of-year Yearnumber 2009; 09189
M/L Month in yearmonth-of-year Monthnumber/text July;7; 07; Jul; 07July; J
wd Week in yearday-of-month Numbernumber 2710
WQ/q Week in monthquarter-of-year Numbernumber/text 23; 03; Q3; 3rd quarter
DY Day in yearweek-based-year Numberyear 1891996; 96
dw Day in monthweek-of-week-based-year Numbernumber 1027
FW Day of week in monthweek-of-month Numbernumber 24
E Day in weekday-of-week Texttext Tuesday; TueTue; Tuesday; T
ue/c Day number of weeklocalized day-of-week Numbernumber/text 12; 02; Tue; Tuesday; T
Fweek-of-monthnumber3
a Am/am-pm marker-of-day Texttext PM
Hh Hour in dayclock-hour-of-am-pm (01-2312) Numbernumber 012
kK Hour in dayhour-of-am-pm (10-2411) Numbernumber 240
Kk Hour in am/clock-hour-of-am-pm (01-1124) Numbernumber 0
hH Hour in am/pmhour-of-day (10-1223) Numbernumber 120
m Minute in hourminute-of-hour Numbernumber 30
s Second in minutesecond-of-minute Numbernumber 55
S Millisecondfraction-of-second Numberfraction 978
zA Time zonemilli-of-day General time zonenumber Pacific Standard Time; PST; GMT-08:001234
Zn Time zonenano-of-second RFC 822 time zonenumber -0800987654321
XN Time zoneISO 8601 time zonenano-of-day -08; -0800; -08:00

Note that the patterns are case sensitive and that text based patterns of four characters or more represent the full form; otherwise a short or abbreviated form is used if available. So e.g. MMMMM or more is unnecessary.

Here are some examples of valid SimpleDateFormat patterns to parse a given string to date:

Input stringPattern
2001.07.04 AD at 12:08:56 PDTnumber yyyy.MM.dd G 'at' HH:mm:ss z1234000000
Wed, Jul 4, '01EEE, MMM d, ''yyV
12:08 PM h:mm a
12 o'clock PM, Pacific Daylight Timetime-zone ID hh 'o''clock' a, zzzz
0:08 PM, PDTzone-id K:mm a, zAmerica/Los_Angeles; Z; -08:30
02001.July.04 AD 12:08 PMyyyyy.MMMM.dd GGG hh:mm aaaz
Wed, 4 Jul 2001 12:08:56 -0700 EEE, d MMM yyyy HH:mm:ss Ztime-zone namezone-namePacific Standard Time; PST
010704120856-0700yyMMddHHmmssZOlocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
2001-07-04T12:08:56.235-0700yyyy-MM-dd'T'HH:mm:ss.SSSZXzone-offset 'Z' for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
2001-07-04T12:08:56.235-07:00yyyy-MM-dd'T'HH:mm:ss.SSSXXXxzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
2001-W27-3YYYY-'W'ww-uZzone-offsetoffset-Z+0000; -0800; -08:00;

An importantDo note is that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat is not, thread safe. In other words, you should never declare and assign it as a static or instance variable and then reuse it from different methods/threads. You should always create it brand new within the method local scopecould thus also define your own, if necessary.


Java 8 update

IfFor a particular input string format, you happendon't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be on Java 8 or newerparsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, then use DateTimeFormatterLocalDate#parse(text) parses an ISO date without the time component (also heresee ISO_LOCAL_DATE), click the link to see all predefined formatters and available format patterns; the tutorial is available here). This new API is inspired byZonedDateTime#parse(text) parses an ISO date with an offset and time zone added JodaTime(see ISO_ZONED_DATE_TIME).


Pre-Java 8

In case you're not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormat using a format pattern matching the input string.

String string = "January 2, 2010";
DateTimeFormatterDateFormat formatterformat = DateTimeFormatter.ofPatternnew SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
LocalDateDate date = LocalDateformat.parse(string, formatter);
System.out.println(date); // 2010-01-Sat Jan 02 00:00:00 GMT 2010

Note: if your format pattern happens to contain the time part as wellimportance of the explicit Locale argument. If you omit it, then it will use the LocalDateTime#parse(text, formatter) insteaddefault locale which is not necessarily English as used in the month name of LocalDate#parse(text, formatter)the input string. And, if your format pattern happens to containIf the time zone as welllocale doesn't match with the input string, then useyou would confusingly get a ZonedDateTime#parse(text, formatter) insteadjava.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadocthe javadoc, listing all available format patterns:

SymbolLetter MeaningDate or Time Component Presentation Examples
GeratextAD; Anno Domini; A
u yearEra designator yearText 2004; 04AD
y year-of-eraYear yearYear 2004; 041996; 96
DY day-of-yearWeek year numberYear 1892009; 09
M/L month-of-yearMonth in year number/textMonth 7; 07;July; Jul; July; J07
dw day-of-monthWeek in year numberNumber 1027
Q/qW quarter-of-yearWeek in month number/textNumber 3; 03; Q3; 3rd quarter2
YD week-based-yearDay in year yearNumber 1996; 96189
wd week-of-week-based-yearDay in month numberNumber 2710
WF week-of-monthDay of week in month numberNumber 42
Eday-of-weektextTue; Tuesday; T
e/c localized day-of-weekDay in week number/textText 2; 02; Tue; Tuesday; TTuesday; Tue
Fu week-of-monthDay number of week numberNumber 31
a am-Am/pm-of-day marker textText PM
hH clock-hour-of-am-pmHour in day (10-1223) numberNumber 120
Kk hour-of-am-pmHour in day (01-1124) numberNumber 024
kK clock-hour-of-am-Hour in am/pm (10-2411) numberNumber 0
Hh hour-of-dayHour in am/pm (01-2312) numberNumber 012
m minute-of-hourMinute in hour numberNumber 30
s second-of-minuteSecond in minute numberNumber 55
S fraction-of-secondMillisecond fractionNumber 978
Az milli-of-dayTime zone numberGeneral time zone 1234Pacific Standard Time; PST; GMT-08:00
nZ nano-of-secondTime zone numberRFC 822 time zone 987654321-0800
NX nano-of-dayTime zone numberISO 8601 time zone 1234000000-08; -0800; -08:00

Note that the patterns are case sensitive and that text based patterns of four characters or more represent the full form; otherwise a short or abbreviated form is used if available. So e.g. MMMMM or more is unnecessary.

Here are some examples of valid SimpleDateFormat patterns to parse a given string to date:

 
Input stringPattern
2001.07.04 AD at 12:08:56 PDTVyyyy.MM.dd G 'at' HH:mm:ss ztime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
Wed, Jul 4, '01zEEE, MMM d, ''yytime-zone namezone-namePacific Standard Time; PST
12:08 PMOh:mm alocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
12 o'clock PM, Pacific Daylight TimeXhh 'o''clock' a, zzzzzone-offset 'Z' for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
0:08 PM, PDTxK:mm a, z
zone-offset02001.July.04 AD 12:08 PM offsetyyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -x0700 +0000; -08; -0830; -08:30; -083015; -08:30:15;EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700ZyyMMddHHmmssZ
2001-07-04T12:08:56.235-0700 zoneyyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-offset07-04T12:08:56.235-07:00 offsetyyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-ZW27-3 +0000; -0800; -08:00;YYYY-'W'ww-u

DoAn important note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary tothat SimpleDateFormat, is not thread safe. You could thus also define your own, if necessary.

For a particular input string format In other words, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text)should never declare and assign it as a static or instance variable and then reuse it already uses the ISO_LOCAL_DATE_TIME formatterfrom different methods/threads. Similarly, LocalDate#parse(text) parses an ISO date withoutYou should always create it brand new within the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME)method local scope.

That's the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Simply format the date using SimpleDateFormat using a format pattern matching the input string.

String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010

Note the importance of the explicit Locale argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M/L Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
u Day number of week Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zoneISO 8601 time zone -08; -0800; -08:00

Note that the patterns are case sensitive and that text based patterns of four characters or more represent the full form; otherwise a short or abbreviated form is used if available. So e.g. MMMMM or more is unnecessary.

Here are some examples of valid SimpleDateFormat patterns to parse a given string to date:

Input stringPattern
2001.07.04 AD at 12:08:56 PDT yyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01EEE, MMM d, ''yy
12:08 PM h:mm a
12 o'clock PM, Pacific Daylight Time hh 'o''clock' a, zzzz
0:08 PM, PDT K:mm a, z
02001.July.04 AD 12:08 PMyyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700 EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3YYYY-'W'ww-u

An important note is that SimpleDateFormat is not thread safe. In other words, you should never declare and assign it as a static or instance variable and then reuse it from different methods/threads. You should always create it brand new within the method local scope.


Java 8 update

If you happen to be on Java 8 or newer, then use DateTimeFormatter (also here, click the link to see all predefined formatters and available format patterns; the tutorial is available here). This new API is inspired by JodaTime.

String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02

Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter) instead of LocalDate#parse(text, formatter). And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Symbol Meaning Presentation Examples
GeratextAD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
Eday-of-weektextTue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
Vtime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
ztime-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset 'Z' for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;

Do note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

For a particular input string format, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME).

That's the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date class was de-facto deprecated (discommended) since introduction of java.time API in Java 8 (2014).

Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here).

String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02

Note: if your format pattern happens to contain the time part as well, then use LocalDateTime#parse(text, formatter) instead of LocalDate#parse(text, formatter). And, if your format pattern happens to contain the time zone as well, then use ZonedDateTime#parse(text, formatter) instead.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Symbol Meaning Presentation Examples
G era text AD; Anno Domini; A
uyearyear2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
Fweek-of-monthnumber3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset 'Z' for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-offsetoffset-Z+0000; -0800; -08:00;

Do note that it has several predefined formatters for the more popular patterns. So instead of e.g. DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);, you could use DateTimeFormatter.RFC_1123_DATE_TIME. This is possible because they are, on the contrary to SimpleDateFormat, thread safe. You could thus also define your own, if necessary.

For a particular input string format, you don't need to use an explicit DateTimeFormatter: a standard ISO 8601 date, like 2016-09-26T17:44:57Z, can be parsed directly with LocalDateTime#parse(text) as it already uses the ISO_LOCAL_DATE_TIME formatter. Similarly, LocalDate#parse(text) parses an ISO date without the time component (see ISO_LOCAL_DATE), and ZonedDateTime#parse(text) parses an ISO date with an offset and time zone added (see ISO_ZONED_DATE_TIME).


Pre-Java 8

In case you're not on Java 8 yet, or are forced to use java.util.Date, then format the date using SimpleDateFormat using a format pattern matching the input string.

String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010

Note the importance of the explicit Locale argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a java.text.ParseException even though when the format pattern seems valid.

Here's an extract of relevance from the javadoc, listing all available format patterns:

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M/L Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
u Day number of week Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00

Note that the patterns are case sensitive and that text based patterns of four characters or more represent the full form; otherwise a short or abbreviated form is used if available. So e.g. MMMMM or more is unnecessary.

Here are some examples of valid SimpleDateFormat patterns to parse a given string to date:

 
Input stringPattern
2001.07.04 AD at 12:08:56 PDTyyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01EEE, MMM d, ''yy
12:08 PMh:mm a
12 o'clock PM, Pacific Daylight Timehh 'o''clock' a, zzzz
0:08 PM, PDTK:mm a, z
02001.July.04 AD 12:08 PM yyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700 EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700 yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00 yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3 YYYY-'W'ww-u

An important note is that SimpleDateFormat is not thread safe. In other words, you should never declare and assign it as a static or instance variable and then reuse it from different methods/threads. You should always create it brand new within the method local scope.

Markdown now finally supports tables.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Letter  Date or Time Component  Presentation        Examples
------  ----------------------  ------------------  -------------------------------------
G       Era designator          Text                AD
y       Year                    Year                1996; 96
Y       Week year               Year                2009; 09
M/L     Month in year           Month               July; Jul; 07
w       Week in year            Number              27
W       Week in month           Number              2
D       Day in year             Number              189
d       Day in month            Number              10
F       Day of week in month    Number              2
E       Day in week             Text                Tuesday; Tue
u       Day number of week      Number              1
a       Am/pm marker            Text                PM
H       Hour in day (0-23)      Number              0
k       Hour in day (1-24)      Number              24
K       Hour in am/pm (0-11)    Number              0
h       Hour in am/pm (1-12)    Number              12
m       Minute in hour          Number              30
s       Second in minute        Number              55
S       Millisecond             Number              978
z       Time zone               General time zone   Pacific Standard Time; PST; GMT-08:00
Z       Time zone               RFC 822 time zone   -0800
X       Time zone               ISO 8601 time zone  -08; -0800; -08:00
LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
yYearYear1996; 96
YWeek yearYear2009; 09
M/LMonth in yearMonthJuly; Jul; 07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesday; Tue
uDay number of weekNumber1
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
ZTime zoneRFC 822 time zone-0800
XTime zoneISO 8601 time zone-08; -0800; -08:00
Input string                            Pattern
------------------------------------    ----------------------------
2001.07.04 AD at 12:08:56 PDT           yyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01                         EEE, MMM d, ''yy
12:08 PM                                h:mm a
12 o'clock PM, Pacific Daylight Time    hh 'o''clock' a, zzzz
0:08 PM, PDT                            K:mm a, z
02001.July.04 AD 12:08 PM               yyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700          EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700                       yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700            yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00           yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3                              YYYY-'W'ww-u
Input stringPattern
2001.07.04 AD at 12:08:56 PDTyyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01EEE, MMM d, ''yy
12:08 PMh:mm a
12 o'clock PM, Pacific Daylight Timehh 'o''clock' a, zzzz
0:08 PM, PDTK:mm a, z
02001.July.04 AD 12:08 PMyyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3YYYY-'W'ww-u

Java 8 update

Java 8 update

Symbol  Meaning                     Presentation  Examples
------  --------------------------  ------------  ----------------------------------------------
G       era                         text          AD; Anno Domini; A
u       year                        year          2004; 04
y       year-of-era                 year          2004; 04
D       day-of-year                 number        189
M/L     month-of-year               number/text   7; 07; Jul; July; J
d       day-of-month                number        10

Q/q     quarter-of-year             number/text   3; 03; Q3; 3rd quarter
Y       week-based-year             year          1996; 96
w       week-of-week-based-year     number        27
W       week-of-month               number        4
E       day-of-week                 text          Tue; Tuesday; T
e/c     localized day-of-week       number/text   2; 02; Tue; Tuesday; T
F       week-of-month               number        3

a       am-pm-of-day                text          PM
h       clock-hour-of-am-pm (1-12)  number        12
K       hour-of-am-pm (0-11)        number        0
k       clock-hour-of-am-pm (1-24)  number        0

H       hour-of-day (0-23)          number        0
m       minute-of-hour              number        30
s       second-of-minute            number        55
S       fraction-of-second          fraction      978
A       milli-of-day                number        1234
n       nano-of-second              number        987654321
N       nano-of-day                 number        1234000000

V       time-zone ID                zone-id       America/Los_Angeles; Z; -08:30
z       time-zone name              zone-name     Pacific Standard Time; PST
O       localized zone-offset       offset-O      GMT+8; GMT+08:00; UTC-08:00;
X       zone-offset 'Z' for zero    offset-X      Z; -08; -0830; -08:30; -083015; -08:30:15;
x       zone-offset                 offset-x      +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z       zone-offset                 offset-Z      +0000; -0800; -08:00;
SymbolMeaningPresentationExamples
GeratextAD; Anno Domini; A
uyearyear2004; 04
yyear-of-erayear2004; 04
Dday-of-yearnumber189
M/Lmonth-of-yearnumber/text7; 07; Jul; July; J
dday-of-monthnumber10
Q/qquarter-of-yearnumber/text3; 03; Q3; 3rd quarter
Yweek-based-yearyear1996; 96
wweek-of-week-based-yearnumber27
Wweek-of-monthnumber4
Eday-of-weektextTue; Tuesday; T
e/clocalized day-of-weeknumber/text2; 02; Tue; Tuesday; T
Fweek-of-monthnumber3
aam-pm-of-daytextPM
hclock-hour-of-am-pm (1-12)number12
Khour-of-am-pm (0-11)number0
kclock-hour-of-am-pm (1-24)number0
Hhour-of-day (0-23)number0
mminute-of-hournumber30
ssecond-of-minutenumber55
Sfraction-of-secondfraction978
Amilli-of-daynumber1234
nnano-of-secondnumber987654321
Nnano-of-daynumber1234000000
Vtime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
ztime-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset 'Z' for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-offsetoffset-Z+0000; -0800; -08:00;
Letter  Date or Time Component  Presentation        Examples
------  ----------------------  ------------------  -------------------------------------
G       Era designator          Text                AD
y       Year                    Year                1996; 96
Y       Week year               Year                2009; 09
M/L     Month in year           Month               July; Jul; 07
w       Week in year            Number              27
W       Week in month           Number              2
D       Day in year             Number              189
d       Day in month            Number              10
F       Day of week in month    Number              2
E       Day in week             Text                Tuesday; Tue
u       Day number of week      Number              1
a       Am/pm marker            Text                PM
H       Hour in day (0-23)      Number              0
k       Hour in day (1-24)      Number              24
K       Hour in am/pm (0-11)    Number              0
h       Hour in am/pm (1-12)    Number              12
m       Minute in hour          Number              30
s       Second in minute        Number              55
S       Millisecond             Number              978
z       Time zone               General time zone   Pacific Standard Time; PST; GMT-08:00
Z       Time zone               RFC 822 time zone   -0800
X       Time zone               ISO 8601 time zone  -08; -0800; -08:00
Input string                            Pattern
------------------------------------    ----------------------------
2001.07.04 AD at 12:08:56 PDT           yyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01                         EEE, MMM d, ''yy
12:08 PM                                h:mm a
12 o'clock PM, Pacific Daylight Time    hh 'o''clock' a, zzzz
0:08 PM, PDT                            K:mm a, z
02001.July.04 AD 12:08 PM               yyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700          EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700                       yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700            yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00           yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3                              YYYY-'W'ww-u

Java 8 update

Symbol  Meaning                     Presentation  Examples
------  --------------------------  ------------  ----------------------------------------------
G       era                         text          AD; Anno Domini; A
u       year                        year          2004; 04
y       year-of-era                 year          2004; 04
D       day-of-year                 number        189
M/L     month-of-year               number/text   7; 07; Jul; July; J
d       day-of-month                number        10

Q/q     quarter-of-year             number/text   3; 03; Q3; 3rd quarter
Y       week-based-year             year          1996; 96
w       week-of-week-based-year     number        27
W       week-of-month               number        4
E       day-of-week                 text          Tue; Tuesday; T
e/c     localized day-of-week       number/text   2; 02; Tue; Tuesday; T
F       week-of-month               number        3

a       am-pm-of-day                text          PM
h       clock-hour-of-am-pm (1-12)  number        12
K       hour-of-am-pm (0-11)        number        0
k       clock-hour-of-am-pm (1-24)  number        0

H       hour-of-day (0-23)          number        0
m       minute-of-hour              number        30
s       second-of-minute            number        55
S       fraction-of-second          fraction      978
A       milli-of-day                number        1234
n       nano-of-second              number        987654321
N       nano-of-day                 number        1234000000

V       time-zone ID                zone-id       America/Los_Angeles; Z; -08:30
z       time-zone name              zone-name     Pacific Standard Time; PST
O       localized zone-offset       offset-O      GMT+8; GMT+08:00; UTC-08:00;
X       zone-offset 'Z' for zero    offset-X      Z; -08; -0830; -08:30; -083015; -08:30:15;
x       zone-offset                 offset-x      +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z       zone-offset                 offset-Z      +0000; -0800; -08:00;
LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
yYearYear1996; 96
YWeek yearYear2009; 09
M/LMonth in yearMonthJuly; Jul; 07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesday; Tue
uDay number of weekNumber1
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
ZTime zoneRFC 822 time zone-0800
XTime zoneISO 8601 time zone-08; -0800; -08:00
Input stringPattern
2001.07.04 AD at 12:08:56 PDTyyyy.MM.dd G 'at' HH:mm:ss z
Wed, Jul 4, '01EEE, MMM d, ''yy
12:08 PMh:mm a
12 o'clock PM, Pacific Daylight Timehh 'o''clock' a, zzzz
0:08 PM, PDTK:mm a, z
02001.July.04 AD 12:08 PMyyyyy.MMMM.dd GGG hh:mm aaa
Wed, 4 Jul 2001 12:08:56 -0700EEE, d MMM yyyy HH:mm:ss Z
010704120856-0700yyMMddHHmmssZ
2001-07-04T12:08:56.235-0700yyyy-MM-dd'T'HH:mm:ss.SSSZ
2001-07-04T12:08:56.235-07:00yyyy-MM-dd'T'HH:mm:ss.SSSXXX
2001-W27-3YYYY-'W'ww-u

Java 8 update

SymbolMeaningPresentationExamples
GeratextAD; Anno Domini; A
uyearyear2004; 04
yyear-of-erayear2004; 04
Dday-of-yearnumber189
M/Lmonth-of-yearnumber/text7; 07; Jul; July; J
dday-of-monthnumber10
Q/qquarter-of-yearnumber/text3; 03; Q3; 3rd quarter
Yweek-based-yearyear1996; 96
wweek-of-week-based-yearnumber27
Wweek-of-monthnumber4
Eday-of-weektextTue; Tuesday; T
e/clocalized day-of-weeknumber/text2; 02; Tue; Tuesday; T
Fweek-of-monthnumber3
aam-pm-of-daytextPM
hclock-hour-of-am-pm (1-12)number12
Khour-of-am-pm (0-11)number0
kclock-hour-of-am-pm (1-24)number0
Hhour-of-day (0-23)number0
mminute-of-hournumber30
ssecond-of-minutenumber55
Sfraction-of-secondfraction978
Amilli-of-daynumber1234
nnano-of-secondnumber987654321
Nnano-of-daynumber1234000000
Vtime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
ztime-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset 'Z' for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-offsetoffset-Z+0000; -0800; -08:00;
Commonmark migration
Source Link
Loading
Loading
I'm using Java 10...
Source Link
Cœur
  • 39k
  • 25
  • 207
  • 282
Loading
Active reading.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
Loading
deleted 18 characters in body
Source Link
Dave Jarvis
  • 31.3k
  • 43
  • 186
  • 325
Loading
added explanation of the Java Time variants without an explicit formatter
Source Link
Tunaki
  • 138.1k
  • 46
  • 369
  • 443
Loading
Explain when to use ZonedDateTime
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
Clarified.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
Clarified more as to patterns.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
minor fixes and improvements
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
Improved with pattern examples and note about thread safety.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
Clarifiy deprecation+locale. Improved tables.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
Java 8 update.
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
added all patterns
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
Rollback to Revision 3
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading
added 1216 characters in body
Source Link
Eric Leschinski
  • 155.2k
  • 96
  • 423
  • 337
Loading
added 2 characters in body
Source Link
Brad Larson
  • 170.3k
  • 45
  • 401
  • 574
Loading
added 2 characters in body; added 4 characters in body
Source Link
Lion
  • 19.1k
  • 22
  • 83
  • 111
Loading
Source Link
BalusC
  • 1.1m
  • 377
  • 3.7k
  • 3.6k
Loading