0

From this time :

String date = "2023-01-31T23:00:00.000Z";

Which is in zulu time (UTC). How can I get the LocalDateTime in Paris time ? which can be UTC+1 or UTC+2 depending on the offeset if it's Summer or Winter.

I tried that but it doses not work :

    String date = "2023-01-31T23:00:00.000Z";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);
    LocalDateTime datetime = LocalDateTime.parse(date, formatter);
    System.out.println(datetime);
    System.out.println(datetime.atZone(ZoneId.of("Europe/Paris")));

The output :

2023-01-31T23:00
2023-01-31T23:00+01:00[Europe/Paris]
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
AyoubMK
  • 526
  • 1
  • 4
  • 19
  • 1
    Have a look at [Convert date and time between timezone](https://mkyong.com/java/java-convert-date-and-time-between-timezone/) – MadProgrammer May 30 '23 at 08:49
  • 1
    Try `ZonedDateTime zdt = ZonedDateTime.parse(date);ZonedDateTime zdtp = zdt.withZoneSameInstant(ZoneId.of("Europe/Paris"));` – g00se May 30 '23 at 08:56
  • 1
    Unless you have very specific reasons, you should not want a `LocalDateTime` for the date and time in Paris time zone. The *local* in the class name means "without time zone or UTC offset". Prefer a `ZonedDateTime` for a date and time in a specific time zone. You can do everything with it that you could have done with a `LocalDateTime` and more. So just `Instant.parse(date).atZone(ZoneId.of("Europe/Paris"))` (a little bit simpler than what @g00se showed you, which works too). – Ole V.V. May 30 '23 at 11:03
  • Related/similar: [How to convert ISO 8601 formatted DateTime to another time zone in Java?](https://stackoverflow.com/questions/50748930/how-to-convert-iso-8601-formatted-datetime-to-another-time-zone-in-java) – Ole V.V. May 30 '23 at 15:52

1 Answers1

4

Thanks to @Ole V.V. & @g00se

There is the why to cast a String to LocalDateTime with the right Zone :

Solution 1 :

    String date = "2023-01-31T23:00:00.000Z";
    ZonedDateTime zonedDateTime = ZonedDateTime.parse(date);

    ZoneId paris = ZoneId.of("Europe/Paris");

    LocalDateTime localDateTime = zonedDateTime.withZoneSameInstant(paris).toLocalDateTime();
    System.out.println(localDateTime);

Solution 2 :

    String date = "2023-01-31T23:00:00.000Z";

    ZoneId paris = ZoneId.of("Europe/Paris");

    LocalDateTime localDateTime = Instant.parse(date).atZone(paris).toLocalDateTime();
    System.out.println(localDateTime);
AyoubMK
  • 526
  • 1
  • 4
  • 19
  • I've already posted that [admittedly without the trivial step of turning it into `LocalDateTime`] ;) – g00se May 30 '23 at 09:10
  • 2
    @g00se don't answer questions in comments; or, don't expect people not to provide answers with the same content as comments. Comments are meant for clarification, and are not considered permanent. – Andy Turner May 30 '23 at 09:22
  • 1
    @g00se I admit that. I put it as answer to be more visible for other people. Thank you :) – AyoubMK May 30 '23 at 09:29