2

I want to convert a UTC time to a localisedDateTime or zonedDateTime.

I have the following input: 2013-07-10 02:52:49,-44.490947,171.220966 Where the date time is UTC time zone.

I get my zoneId with latitude and longitude which gives me Pacific/Auckland.

With this, I try to get the local time at zonePacific/Auckland from the timestamp and expect as shows the following conversion the result I am looking for.

Playing around with my timeStamp I get:

2013-07-10T02:52:49+12:00[Pacific/Auckland].

But I am looking for the following output:

2013-07-10T14:52:49

I don't think that manipulating the string the change it to a number and add manually is the right answer.

I am sure that we can do this with dates but I am new to Java and after surfing for hours I have not yet found my solution.

The solutions below while they seem valid show the date 2013-07-9 and so does it in my code. So if the website shows the time im expecting why are we not getting the same result?

Santa Clauze
  • 163
  • 1
  • 17
  • 1
    It sounds like you misunderstood. The +12:00 in the string mean that 12 hours have already been added compared to UTC. So to convert to UTC you need to *subtract* them again. The correct UTC time for the same instant/moment is 2013-07-09T14:52:49+00:00, so the **nineth** of July. And you are correct: you shouldn’t do it as string manipulation. – Ole V.V. May 30 '19 at 06:33

1 Answers1

1

When you capture the date time

2013-07-10T02:52:49+12:00[Pacific/Auckland]

, capture it as ZonedDateTime, so that it will have the zone as UTC+12:00.

On that use dateTime.withZoneSameInstant(ZoneId.of("UTC")); to convert the date time into UTC+0 HRS which results 2013-07-10T14:52:49

If you get above data time as a string, then you can convert it to UTC ZonedDateTime as below.

    String time = "2013-07-10T02:52:49+12:00[Pacific/Auckland]";
    ZonedDateTime zdt = ZonedDateTime.parse(time);
    ZoneId zoneId = zdt.getZone();
    System.out.println(zdt);
    System.out.println(zdt.withZoneSameInstant(zoneId).toLocalDateTime());
    zdt = zdt.withZoneSameInstant(ZoneId.of("UTC"));
    System.out.println(zdt);

Output:

2013-07-10T02:52:49+12:00[Pacific/Auckland]
2013-07-10T02:52:49
2013-07-09T14:52:49Z[UTC]
Naveen
  • 907
  • 2
  • 15
  • 25
  • It’s correct and therefore gives the correct result (`2013-07-09T14:52:49Z[UTC]`). `zdt.withZoneSameInstant(zoneId)` is redundant, and normally you would not want to convert to `LocalDateTime`, throwing away the time zone information. – Ole V.V. May 30 '19 at 06:37
  • Why do you have the 9th on your date then rather than the 10th? Auckland being in advance on UTC i would expect `(2013-07-10T14:52:49Z[UTC]). ` – Santa Clauze May 30 '19 at 06:59
  • 1
    @SantaClauze If the time in Auckland ticks *ahead* of the UTC time, then it should be *later* in Auckland, for instance, if the UTC time is 06:15, then it's 12 hours *later* in Auckland, that is, 18:45. The very instant of `2013-07-10T02:52:49+12:00[Pacific/Auckland]` is the same as `2013-07-09T14:52:49Z[UTC]`. – MC Emperor May 30 '19 at 07:53