This post shows how to convert a Java LocalDateTime object to an OffsetDateTime object at the current zone offset. For example, let’s say we are in Singapore and want to convert the current LocalDateTime to a string value. This string value will contain the date/time information plus the offset.
Convert To OffsetDateTime Using Zone Offset
Basically, we want to append a LocalDateTime object with the current zone offset without converting it first to a string object (to concatenate it with +/-##:##) and then to an OffsetDateTime object. We just want the zone offset based on where we execute the codes.
LocalDateTime To OffsetDateTime Codes
Consider the following codes. First, we create a LocalDateTime object. Then, we get our offset using OffsetDateTime on the current date and time. As a result, we get a string value representing our current offset. In our case, the offset is +8.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.turreta.localdatetimetooffsetdatetime; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; public class ConvertDemo { public static void main(String[] args) { LocalDateTime ldtNow = LocalDateTime.of(2018, 6, 1, 7, 30); System.out.println(ldtNow); // My current offset is +8 ZoneOffset offset = OffsetDateTime.now().getOffset(); OffsetDateTime offsetDateTime = ldtNow.atOffset(offset); System.out.println(offsetDateTime); } } |
Next, we use the offset to convert the Java LocalDateTime object to an OffsetDateTime object by using the atOffset method. With this technique, we can avoid string concatenation and let the appropriate objects do the conversion work.
Testing
When we run the Java codes we have, we get the following output.
1 2 | 2018-06-01T07:30 2018-06-01T07:30+08:00 |
What if we only use OffsetDateTime.now() instead with Java LocalDateTime object? We could do that. However, the resulting string may require manipulation depending on our needs. Consider the following codes and the output.
1 2 | System.out.println(OffsetDateTime.now()); // Output: 2021-07-10T16:18:52.437883400+08:00 |
Unless we need to capture the time accurately, we still need to manipulate the string value to extract the date/time only up to the minute. Alternatively, we could use a DateTimeFormatter instance with just an instance of OffsetDateTime. Meaning, we do not use a Java LocalDateTime object at all. Consider the following codes.
1 2 3 4 | String date = OffsetDateTime.now().toString(); OffsetDateTime dt = OffsetDateTime.parse(date); DateTimeFormatter fmt = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mmXXXXX"); System.out.println(fmt.format(dt)); |
The codes generate the following output.
1 2 | 2021-07-10T16:31:07.806883300+08:00 2021-07-10T16:31+08:00 |
For more information on the pattern, please see OffsetDateTime’s toString method documentation. We could use any of the following ISO-8601 formats for reference.
1 2 3 4 5 | uuuu-MM-dd'T'HH:mmXXXXX uuuu-MM-dd'T'HH:mm:ssXXXXX uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX |
This post is (now) part of a reboot Java tutorial.