javadatetimezoneddatetimethreetenbp

ZonedDateTime with timezone added to print format


I'm using https://github.com/JakeWharton/ThreeTenABP in my project.

I have org.threeten.bp

ZonedDateTime: 2019-07-25T14:30:57+05:30[Asia/Calcutta]

How can I get this printed with addition of the timezone hours? ie the result should have 2019-07-25T20:00:57


Solution

  • Get the offset in the form of seconds from ZonedDateTime

    ZonedDateTime time = ZonedDateTime.parse("2019-07-25T14:30:57+05:30");
    long seconds = time.getOffset().getTotalSeconds();
    

    Now get the LocalDateTime part from ZonedDateTime

    LocalDateTime local = time.toLocalDateTime().plusSeconds(seconds);   //2019-07-25T20:00:57  
    

    toLocalDateTime

    Gets the LocalDateTime part of this date-time.

    If you want to get the local date time in UTC use toInstant()

    This returns an Instant representing the same point on the time-line as this date-time. The calculation combines the local date-time and offset.

    Instant i = time.toInstant();   //2019-07-25T09:00:57Z