Java 8 - Create Instant from LocalDateTime using TimeZone

I have a date stored in the database in the format of the string ddMMyyyy and hh: mm and TimeZone. I want to create Instant, based on this information, but I do not know how to do it.

sort of

LocalDateTime dateTime = LocalDateTime.of(2017, Month.JUNE, 1, 13, 39);
Instant instant = dateTime.toInstant(TimeZone.getTimeZone("ECT"));
+10
source share
3 answers

First you can create ZonedDateTimewith this time zone, and then call toInstant:

LocalDateTime dateTime = LocalDateTime.of(2017, Month.JUNE, 15, 13, 39);
Instant instant = dateTime.atZone(ZoneId.of("Europe/Paris")).toInstant();
System.out.println(instant); // 2017-06-15T11:39:00Z

I also switched to using the full name of the time zone (on Vasily’s advice), as it is less ambiguous.

+23
source

TimeZone. ZoneId, , .

LocalDateTime dateTime = LocalDateTime.of(2017, Month.JUNE, 1, 13, 39);
ZonedDateTime.of(dateTime, ZoneId.of("ECT")).toInstant();
+4

I think the following code should work:

LocalDateTime time = LocalDateTime.of(2017, Month.JUNE, 15, 13, 39);
ZonedDateTime.of(time, TimeZone.getTimeZone("ZONE").toZoneId()).toInstant();

You just need to replace β€œZONE” with the time zone that you need.

-2
source

All Articles