Parsing a string into a ZonedDateTime with UTC timezone

7.1k Views Asked by At

How do you parse, using using the ThreeTen library (I can't use java 8) a string of the following format:

15 Aug 2014

forcing this to become a ZonedDateTime meaning 15 August 2014 in UTC timezone at midnight?

1

There are 1 best solutions below

0
On

I assume you mean 2014, not 2010. If so then following code can help you:

LocalDate date = 
  LocalDate.parse(
    "15 Aug 2014", 
    DateTimeFormatter.ofPattern("dd MMM uuuu", Locale.ENGLISH));
ZonedDateTime zdt = date.atStartOfDay(ZoneOffset.UTC);
System.out.print(zdt);
// output: 2014-08-15T00:00Z

Another more clunky way would be to use a specialized DateTimeFormatterBuilder using the method parseDefaulting() for the missing time and offset (not tested).