2

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

nullUser
  • 1,159
  • 2
  • 15
  • 26
  • 2
    Your time is already with that addition offset. If you set offset `+00:00` you will get `2019-07-25T09:00:57+00:00`. Also you can look at Deadpool's answer to see how to done that. But in my opinion, that is redundant. – KunLun Jul 25 '19 at 17:07

2 Answers2

2

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
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
2

You misunderstood. The offset of +05:30 in your string means that 5 hours 30 minutes have already been added to the time compared to UTC. So adding them once more will not make any sense.

If you want to compensate for the offset, simply convert your date-time to UTC. For example:

    ZonedDateTime zdt = ZonedDateTime.parse("2019-07-25T14:30:57+05:30[Asia/Calcutta]");
    OffsetDateTime utcDateTime = zdt.toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC);
    System.out.println(utcDateTime);

Output:

2019-07-25T09:00:57Z

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161