The Java 8 OffsetDateTime represents a date-time with an offset. OffsetDateTime will add the offset(time in hours) from the UTC to obtain the local date-time value.
You can use the OffsetDateTime.parse("2020-07-02T23:33:58.519972500+01:00")
method to parse a string value to the OffsetDateTime object in Java
In this tutorial, you will learn how to convert a string to an OffsetDateTime object.
Check this tutorial to convert an Instant to OffsetDateTime value, How to convert an Instant to Java 8 OffsetDateTime
Using OffsetDateTime.parse() with Predefined formatter
The Java 8 DateTimeFormatter class provides predefined formats for parsing a string to OffsetDateTime object.
To convert a string to an OffsetDateTime object,
- Invoke the
OffsetDateTime.parse()
method by passing the string and a formatted pattern - Formatter should be predefined pattern with Offset value(
DateTimeFormatter.ISO_OFFSET_DATE_TIME
) which is equivalent to “‘yyyy-MM-ddTH:mm:ss ZZ'” - It will return an OffsetDateTime value.
Always try to use a Predefined formatting pattern to make it universal, but not when a custom date format is required. Also, OffsetDateTime always expects offset value(+01:00,-0800) while parsing, and if missed to provide, will throw DateTimeParseException
.
Code
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
//main
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2023-02-19T23:33:58+01:00",formatter);
System.out.println(offsetDateTime); //2023-02-19T23:33:58+01:00
You can also invoke the OffsetDateTime .parse(string)
method without providing the DateTimeFormatter value explicitly as below because internally DateTimeFormatter.ISO_OFFSET_DATE_TIME
will be applied for parsing the value.
Code
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
//main
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2020-07-02T23:33:58.519972500+01:00");
System.out.println(offsetDateTime); //2020-07-02T23:33:58.519972500+01:00
Using OffsetDateTime.parse() Custom format pattern
The Custom formatted patterns help leverage parsing different user-specific date formats.
To convert a String to OffsetDateTime using a Custom formatted pattern,
- Create a DateTimeFormatter pattern(
yyyyMMdd H:mm:ss VV
) to match the string - Invoke the
OffsetDateTime.parse()
method by passing the string and a formatted pattern - It will return an OffsetDateTime object.
Use custom formats for business-specific requirements
Let us see some of the different patterns that can be used to convert a string to an OffsetDateTime object,
Code
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
//main
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd H:mm:ss VV");
OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("20200702 23:33:58 -08:30",formatter1);
System.out.println(offsetDateTime1); //2020-07-02T23:33:58-08:30
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("uuuu/MM/dd H:mm:ss xx");
OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2020/07/02 23:33:58 +0200",formatter2);
System.out.println(offsetDateTime2); //2020-07-02T23:33:58+02:00
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss XXXX");
OffsetDateTime offsetDateTime3 = OffsetDateTime.parse("2020-07-02 23:33:58 -083015",formatter3);
System.out.println(offsetDateTime3); //2020-07-02T23:33:58-08:30:15
DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("EEE yyyyMMdd H:mm:ss ZZ");
OffsetDateTime offsetDateTime4 = OffsetDateTime.parse("Sun 20230219 23:33:58 -0800",formatter4);
System.out.println(offsetDateTime4); //2023-02-19T23:33:58-08:00
DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("EEEE yyyyMMdd H:mm:ss O");
OffsetDateTime offsetDateTime5 = OffsetDateTime.parse("Sunday 20230219 23:33:58 GMT+8",formatter5);
System.out.println(offsetDateTime5); //2023-02-19T23:33:58+08:00
Using LocalDateTime
The Java 8 LocalDateTime represents the date-time value without a time zone.
To convert a string to OffsetDateTime using LocalDateTime,
- Parse the input string to the LocalDateTime object using the
LocalDateTime.parse()
method. - Invoke the
OffsetDateTime.of()
method with localDateTime object and ZoneOffset value(ZoneOffset.of("+10:10")
). - It returns an OffsetDateTime object.
- If ZoneOffset is not set, then DateTimeParseException will be thrown.
Use when the input string contains only date-time value and without offset value.
Code
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
//main
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd H:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse("20230219 23:33:58", formatter);
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, ZoneOffset.of("+10:10"));
System.out.println(offsetDateTime); //2023-02-19T23:33:58+10:10
Using LocalDate and LocalTime
The Java 8 LocalDate represents only date without time-zone, and LocalTime represents only time without a time-zone.
To convert the string to OffsetDateTime using LocalDate and LocalTime,
- Parse the input string to LocalDate(
LocalDate.parse("2023-02-19")
) and LocalTime(LocalTime.parse("12:10:20")
) objects respectively. - Invoke the
OffsetDateTime.of()
method with localDate, localTime object and ZoneOffset value(ZoneOffset.of("+10:10")
). - It returns an OffsetDateTime object.
- If ZoneOffset is not set, then DateTimeParseException will be thrown.
Use when partial information is found in the string, either date/time.
Code
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
//main
LocalDate localDate = LocalDate.parse("2023-02-19");
LocalTime localtime = LocalTime.MIN;
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDate,localtime, ZoneOffset.of("+10:10"));
System.out.println(offsetDateTime); //2023-02-19T00:00+10:10