Java code convert RFC3339 and Unix timestamps to Zoned time

joel
1 min readApr 12, 2020

--

  1. RFC3339 time could be converted to any zoned time.
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.of("America/Toronto"));
Instant instant = Instant.parse( "2020–04–05T21:00:00.000000000Z" );
ZoneId z = ZoneId.of( "America/Toronto" );
ZonedDateTime zdt = instant.atZone( z );
System.out.io.print(zdt.format((formatter)));

2. Unix timestamps

DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.of("America/Toronto"));
Instant instant = Instant.ofEpochMilli(Long.parseLong("1586379600")*1000);
ZoneId z = ZoneId.of( "America/Toronto" );
ZonedDateTime zdt = instant.atZone( z );
System.out.io.print(zdt.format((formatter)));

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response