patternjavaCritical
Java string to date conversion
Viewed 0 times
stringconversiondatejava
Problem
What is the best way to convert a
Ultimately, I want to break out the month, the day, and the year as integers so that I can use
to convert the date into time.
String in the format 'January 2, 2010' to a Date in Java?Ultimately, I want to break out the month, the day, and the year as integers so that I can use
Date date = new Date();
date.setMonth()..
date.setYear()..
date.setDay()..
date.setlong currentTime = date.getTime();to convert the date into time.
Solution
That's the hard way, and those
Simply format the date using
In your specific case of "January 2, 2010" as the input string:
Note: if your format pattern happens to contain the time part as well, then use
Here's an extract of relevance from the javadoc, listing all available format patterns:
Symbol
Meaning
Presentation
Examples
era
text
AD; Anno Domini; A
year
year
2004; 04
year-of-era
year
2004; 04
day-of-year
number
189
month-of-year
number/text
7; 07; Jul; July; J
day-of-month
number
10
quarter-of-year
number/text
3; 03; Q3; 3rd quarter
week-based-year
year
1996; 96
week-of-week-based-year
number
27
week-of-month
number
4
day-of-week
text
Tue; Tuesday; T
localized day-of-week
number/text
2; 02; Tue; Tuesday; T
week-of-month
number
3
am-pm-of-day
text
PM
clock-hour-of-am-pm (1-12)
number
12
hour-of-am-pm (0-11)
number
0
clock-hour-of-am-pm (1-24)
number
0
hour-of-day (0-23)
number
0
minute-of-hour
number
30
second-of-minute
number
55
fraction-of-second
fraction
978
milli-of-day
number
1234
nano-of-second
number
987654321
nano-of-day
number
1234000000
time-zone ID
zone-id
America/Los_Angeles; Z; -08:30
time-zone name
zone-name
Pacific Standard Time; PST
localized zone-offset
offset-O
GMT+8; GMT+08:00; UTC-08:00;
zone-offset 'Z' for zero
offset-X
Z; -08; -0830; -08:30; -083015; -08:30:15;
zone-offset
offset-x
+0000; -08; -0830; -08:30; -083015; -08:30:15;
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.
For a particular input string format, you don't need to use an explicit
Pre-Java 8
In case you're not on Java 8 yet, or are forced to use
Note the importance of the explicit
Here's an extract of relevance from the javadoc, listing all available format patterns:
Letter
Date or Time Component
Presentation
Examples
Era designator
Text
AD
Year
Year
1996; 96
Week year
Year
2009; 09
Month in year
Month
July; Jul; 07
Week in year
Number
27
Week in month
Number
2
Day in year
Number
189
Day in month
Number
10
Day of week in month
Number
2
Day in week
Text
Tuesday; Tue
Day number of week
Number
1
Am/pm marker
Text
PM
Hour in day (0-23)
Number
0
Hour in day (1-24)
Number
24
Hour in am/pm (0-11)
Number
0
Hour in am/pm (1-12)
Number
12
Minute in hour
Number
30
Second in minute
Number
55
Millisecond
Number
978
Time zone
General time zone
Pacific Standard Time; PST; GMT-08:00
Time zone
RFC 822 time zone
-0800
Time zone
ISO 8601 time
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).In your specific case of "January 2, 2010" as the input string:
- "January" is the full text month, so use the
MMMMpattern for it
- "2" is the short day-of-month, so use the
dpattern for it.
- "2010" is the 4-digit year, so use the
yyyypattern for it.
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-02Note: 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
Gera
text
AD; Anno Domini; A
uyear
year
2004; 04
yyear-of-era
year
2004; 04
Dday-of-year
number
189
M/Lmonth-of-year
number/text
7; 07; Jul; July; J
dday-of-month
number
10
Q/qquarter-of-year
number/text
3; 03; Q3; 3rd quarter
Yweek-based-year
year
1996; 96
wweek-of-week-based-year
number
27
Wweek-of-month
number
4
Eday-of-week
text
Tue; Tuesday; T
e/clocalized day-of-week
number/text
2; 02; Tue; Tuesday; T
Fweek-of-month
number
3
aam-pm-of-day
text
PM
hclock-hour-of-am-pm (1-12)
number
12
Khour-of-am-pm (0-11)
number
0
kclock-hour-of-am-pm (1-24)
number
0
Hhour-of-day (0-23)
number
0
mminute-of-hour
number
30
ssecond-of-minute
number
55
Sfraction-of-second
fraction
978
Amilli-of-day
number
1234
nnano-of-second
number
987654321
Nnano-of-day
number
1234000000
Vtime-zone ID
zone-id
America/Los_Angeles; Z; -08:30
ztime-zone name
zone-name
Pacific Standard Time; PST
Olocalized zone-offset
offset-O
GMT+8; GMT+08:00; UTC-08:00;
Xzone-offset 'Z' for zero
offset-X
Z; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offset
offset-x
+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-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).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 2010Note 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
GEra designator
Text
AD
yYear
Year
1996; 96
YWeek year
Year
2009; 09
M/LMonth in year
Month
July; Jul; 07
wWeek in year
Number
27
WWeek in month
Number
2
DDay in year
Number
189
dDay in month
Number
10
FDay of week in month
Number
2
EDay in week
Text
Tuesday; Tue
uDay number of week
Number
1
aAm/pm marker
Text
PM
HHour in day (0-23)
Number
0
kHour in day (1-24)
Number
24
KHour in am/pm (0-11)
Number
0
hHour in am/pm (1-12)
Number
12
mMinute in hour
Number
30
sSecond in minute
Number
55
SMillisecond
Number
978
zTime zone
General time zone
Pacific Standard Time; PST; GMT-08:00
ZTime zone
RFC 822 time zone
-0800
XTime zone
ISO 8601 time
Code Snippets
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-02String 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 2010Context
Stack Overflow Q#4216745, score: 1853
Revisions (0)
No revisions yet.