17

I like to know if it is possible to convert in Java 8 from LocalDate to OffsetDateTime.

For example assume that I have got this LocalDate:

1992-12-28

Then I would like to have it converted to this OffsetDateTime:

1992-12-28T00:00-03:00

Assume that we know the time zone, for example America/Santiago.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
  • 1
    You’ll need at least `LocalDateTime`. – achAmháin Jul 26 '19 at 06:45
  • `LocalDate` has no time, but `OffsetDateTime` has, that is, needs one - so you must first specify which time of day it should get (kind of the point of having an `OffsetDateTime`, otherwise the offset has no meaning at all) – user85421 Jul 26 '19 at 06:50
  • It's possible. You'd need to decide a time zone for that. Conventionslly one would convert the start of day, so your `OffsrtDateTime` will have hour of day set to 0. For example `yourLocalDate.atStartOfDay(ZoneId.of("Europe/Madrid")).toOffsetDateTime()`. – Ole V.V. Jul 26 '19 at 07:17

5 Answers5

18

You can try this..

OffsetDateTime o = OffsetDateTime.of(LocalDate.now(),LocalTime.NOON, ZoneOffset.UTC);
OffsetDateTime o1 = OffsetDateTime.of(LocalDateTime.now(), ZoneOffset.UTC);
Rowi
  • 545
  • 3
  • 9
11

I took the freedom of editing some assumptions into your question. Some reasonable ones, IMHO, but you should check. Please correct if there’s something wrong there. Under those assumption the correct conversion is:

    ZoneId zone = ZoneId.of("Europe/Madrid");
    LocalDate date = LocalDate.of(2019, Month.JULY, 27);
    OffsetDateTime odt = date.atStartOfDay(zone)
            .toOffsetDateTime();
    System.out.println(odt);

Output:

2019-07-27T00:00+02:00

Java knows about summer time (DST) and finds the correct offset for the time zone taking summer time into account. The atStartOfDay method that I call yields a ZonedDateTime, that is a date and time with time zone. It has a toOffsetDateTime method for converting to the OffsetDateTime that you asked for.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
6

There are many ways to convert LocalDate to OffsetDateTime. Some of them are listed below:

1. Using LocalDate#atStartOfDay​(ZoneId zone) => ZonedDateTime#toOffsetDateTime():

LocalDate date = LocalDate.now();
ZoneOffset offset = ZoneOffset.UTC;
OffsetDateTime odt = date.atStartOfDay(offset).toOffsetDateTime();

This can also be used when you have ZoneId available e.g.

LocalDate date = LocalDate.now();

// You can use a custom ZoneId e.g. ZoneId.of("Europe/London")
ZoneId zoneId = ZoneId.systemDefault(); 

OffsetDateTime odt = date.atStartOfDay(zoneId).toOffsetDateTime();

2. Using LocalDate#atStartOfDay() => LocalDateTime#atOffset​(ZoneOffset offset):

LocalDate date = LocalDate.now();
ZoneOffset offset = ZoneOffset.UTC;
OffsetDateTime odt = date.atStartOfDay().atOffset(offset);

3. Using LocalDate#atTime(OffsetTime time):

LocalDate date = LocalDate.now();
ZoneOffset offset = ZoneOffset.UTC;
OffsetDateTime odt = date.atTime(OffsetTime.of(LocalTime.MIDNIGHT, offset));

4. Using OffsetDateTime#of​(LocalDate date, LocalTime time, ZoneOffset offset):

LocalDate date = LocalDate.now();
ZoneOffset offset = ZoneOffset.UTC;
OffsetDateTime odt = OffsetDateTime.of(date, LocalTime.MIDNIGHT, offset);

Notes:

  1. In all the solutions given above, replace the sample ZoneOffset as required e.g. ZoneOffset offset = ZoneOffset.of("+02:00").
  2. In all the solutions given above, replace the sample LocalDate as required e.g. LocalDate date = LocalDate.of(2021, 3, 14).
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Good. In the UTC case 2. is simpler than 1. and therefore preferred. We don’t often have an `OffsetTime`, so I don’t easily imagine a use for 3., but who knows? – Ole V.V. Mar 14 '21 at 08:50
1

Assuming you actually have LocalDateTime, you can do it like:

ZoneOffset zoneOffset = ZoneOffset.ofHours(2);
OffsetDateTime offsetDateTime = ldt.atOffset(zoneOffset);

With ldt being your LocalDateTime instance.

achAmháin
  • 4,176
  • 4
  • 17
  • 40
1

We do this all the time in our project. Especially to check if the a date is within a given date range. Here is how we do it.

public static OffsetDateTime getOffsetDateTime(LocalDate date, String zone) {
    return date.atStartOfDay(ZoneId.of(zone)).toOffsetDateTime();
}

The zone looks like Australia/Melbourne or UTC. You can alternatively use ZoneId directly if you have a handle to it. Setting it at a particular zone is important since OffsetDateTime will datetime with zone.

the invocation looks like below

String defaultZone = "Australia/Melbourne";
OffsetDateTime meetingStartDate = DateUtils.getOffsetDateTime(project.getCanStartDate(), defaultZone);
Sacky San
  • 1,535
  • 21
  • 26
  • 1
    I find it cumbersome to user ZoneOffset and ZoneId for a system that is user facing. Using zone as String is quite intuitive to developers as well as users. Mapping addresses using Google Maps API also map it quite easily to zone strings. Hence I wanted to note the use of zone as String. The question also specified the same in its last sentence but none of the answers actually captured that. – Sacky San Jun 03 '21 at 13:18