using ZoneDateTime.parse, how would you parse the following
Mon Jul 01 2019 00:00:00 GMT-0500 (Central Daylight Time)
Have to use zoneDateTime.parse is a must.
This is not for homework but for work and standards are already set.
To solve a problem like this, first try to build a formatter that will produce the same output. Then use that for parsing.
Read the javadoc of DateTimeFormatter
to learn what formatting symbols can produce the desired output.
Mon E day-of-week text Tue; Tuesday; T
Jul M/L month-of-year number/text 7; 07; Jul; July; J
01 d day-of-month number 10
2019 u year year 2004; 04
00 H hour-of-day (0-23) number 0
: : fixed text
00 m minute-of-hour number 30
: : fixed text
00 s second-of-minute number 55
GMT 'GMT' fixed text
-0500 x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
( ( fixed text
Central Daylight Time z time-zone name zone-name Pacific Standard Time; PST
) ) fixed text
Then read the fine print to learn how many of each format letter you need, eg that you need dd
to get 2-digit day-of-month with leading zero.
Testing the result:
String expected = "Mon Jul 01 2019 00:00:00 GMT-0500 (Central Daylight Time)";
String format = "EEE MMM dd uuuu HH:mm:ss 'GMT'xx (zzzz)";
ZonedDateTime zdt = ZonedDateTime.of(2019, 7, 1, 0, 0, 0, 0, ZoneId.of("America/Chicago"));
System.out.println(zdt.format(DateTimeFormatter.ofPattern(format)));
System.out.println(expected);
Output
Mon Jul 01 2019 00:00:00 GMT-0500 (Central Daylight Time)
Mon Jul 01 2019 00:00:00 GMT-0500 (Central Daylight Time)
Use it
String input = "Mon Jul 01 2019 00:00:00 GMT-0500 (Central Daylight Time)";
String format = "EEE MMM dd uuuu HH:mm:ss 'GMT'xx (zzzz)";
ZonedDateTime zdt = ZonedDateTime.parse(input, DateTimeFormatter.ofPattern(format));
System.out.println(zdt);
Output
2019-07-01T00:00-05:00[America/Chicago]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.