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 |
u |
year |
year |
2004; 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 |
F |
week-of-month |
number |
3 |
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 zone |
ISO 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 string |
Pattern |
| 2001.07.04 AD at 12:08:56 PDTnumber |
yyyy.MM.dd G 'at' HH:mm:ss z1234000000 |
| Wed, Jul 4, '01 |
EEE, 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 PM |
yyyyy.MMMM.dd GGG hh:mm aaaz |
| Wed, 4 Jul 2001 12:08:56 -0700 |
EEE, d MMM yyyy HH:mm:ss Ztime-zone name |
zone-name |
Pacific Standard Time; PST |
| 010704120856-0700 |
yyMMddHHmmssZO |
localized zone-offset |
offset-O |
GMT+8; GMT+08:00; UTC-08:00; |
| 2001-07-04T12:08:56.235-0700 |
yyyy-MM-dd'T'HH:mm:ss.SSSZX |
zone-offset 'Z' for zero |
offset-X |
Z; -08; -0830; -08:30; -083015; -08:30:15; |
| 2001-07-04T12:08:56.235-07:00 |
yyyy-MM-dd'T'HH:mm:ss.SSSXXXx |
zone-offset |
offset-x |
+0000; -08; -0830; -08:30; -083015; -08:30:15; |
| 2001-W27-3 |
YYYY-'W'ww-uZ |
zone-offset |
offset-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 |
G |
era |
text |
AD; 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 |
E |
day-of-week |
text |
Tue; 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 string |
Pattern |
| 2001.07.04 AD at 12:08:56 PDT |
Vyyyy.MM.dd G 'at' HH:mm:ss z |
time-zone ID |
zone-id |
America/Los_Angeles; Z; -08:30 |
| Wed, Jul 4, '01 |
zEEE, MMM d, ''yy |
time-zone name |
zone-name |
Pacific Standard Time; PST |
| 12:08 PM |
Oh:mm a |
localized zone-offset |
offset-O |
GMT+8; GMT+08:00; UTC-08:00; |
| 12 o'clock PM, Pacific Daylight Time |
Xhh 'o''clock' a, zzzz |
zone-offset 'Z' for zero |
offset-X |
Z; -08; -0830; -08:30; -083015; -08:30:15; |
| 0:08 PM, PDT |
xK: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-0700 |
ZyyMMddHHmmssZ |
| 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.