-2

I have two objects of ZoneOffset's parsed from Strings. How can I sum them up and apply to ZonedDateTime?

For example:
original ZonedDateTime is 2017-12-27T18:30:00, first offset is +03, second offset is +05

How can I get the output of 2017-12-28T18:30:00+08:00 or 2017-12-28T10:30:00?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
San
  • 115
  • 3
  • 16
  • 3
    That does not make any logical sense, does it? Adding up zones seems incorrect, why would you want to do that? – luk2302 Dec 27 '17 at 16:47
  • I have data array with offsets refer not to GMT+0, I have to process it somehow. – San Dec 27 '17 at 16:50
  • 3
    Still, that makes no sense. The only reasonable thing is to *convert* dates between timezone. But +03 +05 resulting in +08 is lacking any meaning. – luk2302 Dec 27 '17 at 16:53
  • "I have to process it somehow" doesn't clarify the requirement, though. Because, among other things, 5 + 3 is not 11; so is that a typo, or is there some way we should understand your requirements so that we know what to do to get 11 out of those inputs? – Mark Adelsberger Dec 27 '17 at 16:55
  • Yes, I need to convert dates that already have offset to another parameterized timezone. – San Dec 27 '17 at 16:55
  • @Mark, thats typo, sorry. – San Dec 27 '17 at 16:57
  • Please show some code. What exactly is your input, what exactly is the expected output and for what reason? – luk2302 Dec 27 '17 at 17:01
  • "2017-12-27T18:30:00" is not a `ZonedDateTime`-representation (lacking the offset). Please try to rewrite your example code for sake of more clarity. – Meno Hochschild Dec 27 '17 at 18:52

1 Answers1

2

I understand your question this way (please check if correct): You’ve got one ZonedDateTime with a usual offset from UTC. I will call it dateTimeWithBaseOffset. And you’ve got another ZonedDateTime with an offset relative to the offset of the former ZonedDateTime. This is really incorrect; the designers of the class decided that the offset is from UTC, but someone used it differently from the intended. I will call the latter dateTimeWithOffsetFromBase.

Best of course if you can fix the code that produced dateTimeWithOffsetFromBase with the unorthodox offset. I am assuming that for now this will not be a solution you can use. So you need to correct the incorrect offset into an offset from UTC.

It’s not bad:

    ZoneOffset baseOffset = dateTimeWithBaseOffset.getOffset();
    ZoneOffset additionalOffset = dateTimeWithOffsetFromBase.getOffset();
    ZoneOffset correctedOffset = ZoneOffset.ofTotalSeconds(baseOffset.getTotalSeconds()
            + additionalOffset.getTotalSeconds());

    OffsetDateTime correctedDateTime = dateTimeWithOffsetFromBase.toOffsetDateTime()
            .withOffsetSameLocal(correctedOffset);
    System.out.println(correctedDateTime);

Using your sample date-times this prints

2017-12-28T18:30+08:00

If you want the time at UTC:

    correctedDateTime = correctedDateTime.withOffsetSameInstant(ZoneOffset.UTC);
    System.out.println(correctedDateTime);

This prints the datetime you asked for:

2017-12-28T10:30Z

For a date-time with an offset, we don’t need to use ZonedDateTime, OffsetDateTime will do and may communicate better to the reader what we’re up to (ZonedDateTime works too, though).

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