Cork Kochi
Cork Kochi

Reputation: 1891

ZonedDateTime parse to another format of ZonedDateTime

The ZonedDateTime currently in

`2020-06-07T14:10:00+01:00`

The format the ZonedDateTime that I am looking for

2020-06-07T13:10:24Z

Upvotes: 0

Views: 338

Answers (2)

WJS
WJS

Reputation: 40024

Try the following:

String time = "2020-06-07T14:10:00+01:00";                         

Desired output format = 2020-06-07T13:10:24Z

ZonedDateTime zdt = ZonedDateTime.parse(time,                      
        DateTimeFormatter.ISO_ZONED_DATE_TIME);                   
System.out.println(zdt.format(DateTimeFormatter.ISO_INSTANT));

Prints

2020-06-07T13:10:00Z
      

Upvotes: 0

Daniel Jacob
Daniel Jacob

Reputation: 1536

You can do it like this:

ZonedDateTime.parse("2020-06-07T14:10:00+01:00").toInstant()

Or if you must have a ZonedDateTime:

ZonedDateTime.parse("2020-06-07T14:10:00+01:00").withZoneSameInstant(ZoneOffset.UTC);

Upvotes: 2

Related Questions