Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert LocalDateTime to OffsetDateTime?

Tags:

How to convert LocalDateTime to OffsetDateTime?

private OffsetDateTime getEntryDate(Payment payment) {
    return Optional.ofNullable(payment)
                   .map(Payment::getEntryDate)
                   .map(SHOULD RETURN OffsetDateTime)
                   .orElse(null);
}

Payment::getEntryDate will return LocalDateTime

like image 455
Melad Basilius Avatar asked Apr 01 '19 12:04

Melad Basilius


People also ask

How do I convert LocalDateTime to another timezone?

The LocalDateTime has no time zone; to convert the LocalDateTime to ZonedDateTime , we can use . atZone(ZoneId. systemDefault()) to create a ZonedDateTime containing the system default time zone and convert it to another time zone using a predefined zone id or offset.

How do I convert LocalDateTime to epoch?

In Java, with a given LocalDateTime object we can use the LocalDateTime. toEpochSecond(ZoneOffset offset) method to convert it to an epoch seconds value which is the number of seconds from the epoch of 1970-01-01T00:00:00Z as the example Java code below. The output as below.


1 Answers

You need to obtain the ZoneOffset to use when creating your OffsetDateTime. One approach is to use a ZoneId for your location:

final ZoneId zone = ZoneId.of("Europe/Paris");
LocalDateTime localDateTime = LocalDateTime.now();
ZoneOffset zoneOffSet = zone.getRules().getOffset(localDateTime);
OffsetDateTime offsetDateTime = localDateTime.atOffset(zoneOffSet);
System.out.println(offsetDateTime); // 2019-08-08T09:54:10.761+02:00
like image 166
CeeTee Avatar answered Oct 10 '22 02:10

CeeTee