1

I have a

OFfsetDateTime `OffsetDateTime date = OffsetDateTime.parse("2020-03-02T14:25:54.871Z");

what does the "Z" stands for? How can I add an hour to it?

I tried to do

OffsetDateTime date = OffsetDateTime.parse("2020-03-02T14:25:54.871Z+1");

, but that doesn't parse.

Thanks :)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Thomas
  • 169
  • 10
  • 1
    `Z` is a short way to express an offset of `00:00`, it means *Zulu Time*... It is the only letter (I know) that can be parsed to an offset, for real offsets, you need to write something like `+01:00` or `+0100`, see the possibilities [here](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html); – deHaar Mar 02 '20 at 15:56
  • "Z" stands for [**Zulu**](https://greenwichmeantime.com/info/history/zulu/) (Greenwich Mean Time) and it's a timezone. You can't add one, you just use "1:00" as the offset. – Elliott Frisch Mar 02 '20 at 15:57
  • maybe this question/answer helps: [how to convert UTC date-time into ISO 8601 format in Java?](https://stackoverflow.com/q/60451547/85421) – user85421 Mar 02 '20 at 16:05
  • What is your expected result? Please note that if you keep hour of day the same and change the offset (for example substitute `+01:00` for `Z`), you will get a different point in time. – Ole V.V. Mar 02 '20 at 17:56

1 Answers1

2

You don’t want that.

  1. You want the same instant, the same point in time. So convert your OffsetDateTime to a different offset adjusting the time of day to take into account that the time of day at the new offset is different.
  2. For most purposes you don’t want to specify the offset to convert to directly. Instead specify which time zone you want to convert to. Let Java find the correct offset for that time zone for that day for you.

The Z means offset zero from UTC. It is sometimes called “Zulu time zone” even though it’s an offset, not a time zone.

Convert the time to a different time zone

For example:

    OffsetDateTime date = OffsetDateTime.parse("2020-03-02T14:25:54.871Z");
    ZoneId newZone = ZoneId.of("Europe/Prague");
    OffsetDateTime dateAtNewOffset
            = date.atZoneSameInstant(newZone).toOffsetDateTime();
    System.out.println(dateAtNewOffset);

Output from this piece of code is:

2020-03-02T15:25:54.871+01:00

Please note that the hour of day is now 15 instead of 14 to compensate for the different offset. When the time is 14:25 UTC, it is 15:25 in Prague (except when summer time (DST) is in effect; Java will handle that for you too if you have a date in the summer time part of the year).

To specify offset

If you did want to specify the offset yourself:

    ZoneOffset newOffset = ZoneOffset.ofHours(1);
    OffsetDateTime dateAtNewOffset = date.withOffsetSameInstant(newOffset);

The result is the same as before.

To add 1 hour to the offset without changing the hour of day

To answer the question that you ask. I repeat my warning, THIS IS NOT WHAT YOU WANT.

    ZoneOffset oldOffset = date.getOffset();
    ZoneOffset newOffset = ZoneOffset.ofTotalSeconds(Math.toIntExact(
            oldOffset.getTotalSeconds() + Duration.ofHours(1).getSeconds()));
    OffsetDateTime dateAtNewOffset = date.withOffsetSameLocal(newOffset);

Now the output resembles what you asked for:

2020-03-02T14:25:54.871+01:00

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Understanding that the true question was different from what was asked, then answering both in details... This is really a great answer ! – Pierre Gramme Feb 17 '21 at 11:21