Java OffsetDateTime.parse() Method with Examples

Tags: Java OffsetDateTime Java 8 DateTimeFormatter

In this Java core tutorial we learn how to use the java.time.OffsetDateTime.parse() method to parse a String to OffsetDateTime object in Java programming language.

Table of contents

  1. Parse String to OffsetDateTime object in Java
  2. Parse String to OffsetDateTime with DateTimeFormatter in Java

Parse String to OffsetDateTime object in Java

In Java we can use the OffsetDateTime.parse(CharSequence text) method to parse a date time String in ISO-8601 extended offset date-time format to a OffsetDateTime object as the following example Java code.

OffsetDateTimeParseExample1.java

import java.time.OffsetDateTime;

public class OffsetDateTimeParseExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.parse("2022-05-08T11:06:18.878564600+02:00");

        System.out.println(offsetDateTime);
    }
}
The output as below.
2022-05-08T11:06:18.878564600+02:00

Parse String to OffsetDateTime with DateTimeFormatter in Java

Using the OffsetDateTime.parse(CharSequence text, DateTimeFormatter formatter) method we can parse a String in specified date time format pattern to OffsetDateTime object as the Java code below.

OffsetDateTimeParseExample2.java

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class OffsetDateTimeParseExample2 {
    public static void main(String... args) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss z");
        OffsetDateTime offsetDateTime = OffsetDateTime.parse("11/07/2022 11:20:30 +02:00", dateTimeFormatter);

        System.out.println(offsetDateTime);
    }
}
The output as below.
2022-07-11T11:20:30+02:00

Happy Coding 😊

Java OffsetDateTime.now() Method with Examples

Java OffsetDateTime.of() Method with Examples

Java OffsetDateTime.ofInstant() Method with Examples

Java OffsetDateTime.withOffsetSameLocal() Method with Examples

Java OffsetDateTime.withOffsetSameInstant() Method with Examples

Java OffsetDateTime.toLocalDateTime() Method with Examples

Java OffsetDateTime.toLocalDate() Method with Examples

Java OffsetDateTime.toLocalTime() Method with Examples

Java OffsetDateTime.toOffsetTime() Method with Examples

Java OffsetDateTime.toZonedDateTime() Method with Examples

Java OffsetDateTime.toInstant() Method with Examples

Java OffsetDateTime.toEpochSecond() Method with Examples

Java OffsetDateTime.toString() Method with Examples

Java OffsetDateTime.getOffset() Method with Examples

Java OffsetDateTime.getYear() Method with Examples

Java OffsetDateTime.getMonthValue() Method with Examples

Java OffsetDateTime.getMonth() Method with Examples

Java OffsetDateTime.getDayOfMonth() Method with Examples

Java OffsetDateTime.getDayOfYear() Method with Examples

Java OffsetDateTime.getDayOfWeek() Method with Examples

Java OffsetDateTime.getHour() Method with Examples

Java OffsetDateTime.getMinute() Method with Examples

Java OffsetDateTime.getSecond() Method with Examples

Java OffsetDateTime.getNano() Method with Examples

Java OffsetDateTime.withYear() Method with Examples

Java OffsetDateTime.withMonth() Method with Examples

Java OffsetDateTime.withDayOfMonth() Method with Examples

Java OffsetDateTime.withDayOfYear() Method with Examples

Java OffsetDateTime.withHour() Method with Examples

Java OffsetDateTime.withMinute() Method with Examples

Java OffsetDateTime.withSecond() Method with Examples

Java OffsetDateTime.withNano() Method with Examples

Java OffsetDateTime.plusYears() Method with Examples

Java OffsetDateTime.plusMonths() Method with Examples

Java OffsetDateTime.plusWeeks() Method with Examples

Java OffsetDateTime.plusDays() Method with Examples

Java OffsetDateTime.plusHours() Method with Examples

Java OffsetDateTime.plusMinutes() Method with Examples

Java OffsetDateTime.plusSeconds() Method with Examples

Java OffsetDateTime.plusNanos() Method with Examples

Java OffsetDateTime.minusYears() Method with Examples

Java OffsetDateTime.minusMonths() Method with Examples

Java OffsetDateTime.minusWeeks() Method with Examples

Java OffsetDateTime.minusDays() Method with Examples

Java OffsetDateTime.minusHours() Method with Examples

Java OffsetDateTime.minusMinutes() Method with Examples

Java OffsetDateTime.minusSeconds() Method with Examples

Java OffsetDateTime.minusNanos() Method with Examples

Java OffsetDateTime.format() Method with Examples

Java OffsetDateTime.atZoneSameInstant() Method with Examples

Java OffsetDateTime.atZoneSimilarLocal() Method with Examples

Java Compare two OffsetDateTime Values