Parsing ZonedDateTime independently on local timezone

Viewed 185

I am trying to parse ZonedDateTime (or Instant) without having to go through LocalDate, which is influenced with local timezone.

I would like to be able to do something like:

val string = "2020-02-02T10:00:00"
val timeZone = ZoneId.of("Europe/Vienna")
val zoned = ZonedDateTime.parse(string, DateTimeFormatter.ISO_DATE_TIME, timeZone)

However there is not even any similar method - all the methods require using of LocalDate, which unfortunately yields different results when having different local timezone. I want to completely ignore local timezone and be able to create ZonedDateTime or Instant just from given parameters.

1 Answers

The input string you have just represent local date time, so first parse it into LocalDateTime and then convert it into ZoneDateTime at zone, I would not say Instant because it always represent the instant in UTC

ZonedDateTime dateTime = LocalDateTime.parse(string,DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                                      .atZone(timeZone);
Related