Java Convert OffsetDateTime to String

Tags: Java OffsetDateTime Java 8 DateTimeFormatter String

In this Java core tutorial we learn how to format a java.time.OffsetDateTime object to a date time String in Java programming language with different solutions and example Java code.

Table of contents

  1. Convert OffsetDateTime to String with Custom Date Time Pattern
  2. Convert OffsetDateTime to String in Localized Pattern
  3. Convert OffsetDateTime to String using Predefined DateTimeFormatter Constants
  4. Convert OffsetDateTime to String using toString() method

Convert OffsetDateTime to String with Custom Date Time Pattern

In Java program, with a given OffsetDateTime object we can use the OffsetDateTime.format(DateTimeFormatter formatter) method to format it to a String in custom date time pattern defined by DateTimeFormatter object as the following example Java code.

ConvertOffsetDateTimeToStringExample1.java

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

public class ConvertOffsetDateTimeToStringExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss Z");
        String offsetDateTimeString1 = offsetDateTime.format(dateTimeFormatter1);

        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("dd-M-yyyy hh:mm:ss a X");
        String offsetDateTimeString2 = offsetDateTime.format(dateTimeFormatter2);

        DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy HH:mm:ss O");
        String offsetDateTimeString3 = offsetDateTime.format(dateTimeFormatter4);

        System.out.println("dd/MM/yyyy HH:mm:ss Z -> " + offsetDateTimeString1);
        System.out.println("dd-M-yyyy hh:mm:ss a X -> " + offsetDateTimeString2);
        System.out.println("EEEE, MMM dd yyyy HH:mm:ss O -> " + offsetDateTimeString3);
    }
}
The output as below.
dd/MM/yyyy HH:mm:ss Z -> 14/05/2022 13:46:54 +0700
dd-M-yyyy hh:mm:ss a X -> 14-5-2022 01:46:54 pm +07
EEEE, MMM dd yyyy HH:mm:ss O -> Saturday, May 14 2022 13:46:54 GMT+7

Convert OffsetDateTime to String in Localized Pattern

With java.time.format.DateTimeFormatter class we can get a specified localized date time format patterns in different style using the DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method.

Below are the list of format styles we can use to format OffsetDateTime object

  • FormatStyle.MEDIUM to format medium text style such as 14 May 2022, 1:47:25 pm
  • FormatStyle.SHORT to format short text style such as 14/5/22, 1:47 pm

In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method with FormatStyle.MEDIUM to format a OffsetDateTime object to String.

ConvertOffsetDateTimeToStringExample2.java

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

public class ConvertOffsetDateTimeToStringExample2 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String offsetDateTimeString = offsetDateTime.format(dateTimeFormatter);

        System.out.println(offsetDateTimeString);
    }
}
The output as below.
14 May 2022, 1:47:25 pm

In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method with FormatStyle.SHORT to format a OffsetDateTime object to String.

ConvertOffsetDateTimeToStringExample3.java

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

public class ConvertOffsetDateTimeToStringExample3 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String offsetDateTimeString = offsetDateTime.format(dateTimeFormatter);

        System.out.println(offsetDateTimeString);
    }
}
The output as below.
14/5/22, 1:47 pm

Convert OffsetDateTime to String using Predefined DateTimeFormatter Constants

The java.time.format.DateTimeFormatter class provides predefined constants which we can use to format a OffsetDateTime object.

  • DateTimeFormatter.ISO_LOCAL_DATE to format a ZonedDateTime object to String in ISO-8601 extended local date format.
  • DateTimeFormatter.ISO_OFFSET_DATE to format a ZonedDateTime object to String in ISO-8601 extended offset date format.
  • DateTimeFormatter.ISO_DATE to format a ZonedDateTime object to String in ISO-8601 extended date format.
  • DateTimeFormatter.ISO_LOCAL_TIME to format a ZonedDateTime object to String in ISO-8601 extended local time format.
  • DateTimeFormatter.ISO_OFFSET_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset time format.
  • DateTimeFormatter.ISO_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset time format.
  • DateTimeFormatter.ISO_LOCAL_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset date-time format.
  • DateTimeFormatter.ISO_OFFSET_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset date-time format.
  • DateTimeFormatter.ISO_ZONED_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset date-time format to add the time-zone.
  • DateTimeFormatter.ISO_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended local or offset date-time format, as well as the extended non-ISO form specifying the time-zone.
  • DateTimeFormatter.ISO_ORDINAL_DATE to format a ZonedDateTime object to String in ISO-8601 extended ordinal date format.
  • DateTimeFormatter.ISO_WEEK_DATE to format a ZonedDateTime object to String in ISO-8601 extended week-based date format.
  • DateTimeFormatter.ISO_INSTANT to format a ZonedDateTime object to String in ISO-8601 instant format.
  • DateTimeFormatter.BASIC_ISO_DATE to format a ZonedDateTime object to String in ISO-8601 basic local date format.
  • DateTimeFormatter.RFC_1123_DATE_TIME to format a ZonedDateTime object to String in RFC-1123 format.

In the following Java program we show how to use the DateTimeFormatter constants above.

ConvertOffsetDateTimeToStringExample4.java

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

public class ConvertOffsetDateTimeToStringExample4 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        String offsetDateTimeString1 = offsetDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String offsetDateTimeString2 = offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE);
        String offsetDateTimeString3 = offsetDateTime.format(DateTimeFormatter.ISO_DATE);
        String offsetDateTimeString4 = offsetDateTime.format(DateTimeFormatter.ISO_LOCAL_TIME);
        String offsetDateTimeString5 = offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_TIME);
        String offsetDateTimeString6 = offsetDateTime.format(DateTimeFormatter.ISO_TIME);
        String offsetDateTimeString7 = offsetDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        String offsetDateTimeString8 = offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        String offsetDateTimeString9 = offsetDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
        String offsetDateTimeString10 = offsetDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
        String offsetDateTimeString11 = offsetDateTime.format(DateTimeFormatter.ISO_ORDINAL_DATE);
        String offsetDateTimeString12 = offsetDateTime.format(DateTimeFormatter.ISO_WEEK_DATE);
        String offsetDateTimeString13 = offsetDateTime.format(DateTimeFormatter.ISO_INSTANT);
        String offsetDateTimeString14 = offsetDateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
        String offsetDateTimeString15 = offsetDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME);

        System.out.println("DateTimeFormatter.ISO_LOCAL_DATE -> " + offsetDateTimeString1);
        System.out.println("DateTimeFormatter.ISO_OFFSET_DATE -> " + offsetDateTimeString2);
        System.out.println("DateTimeFormatter.ISO_DATE -> " + offsetDateTimeString3);
        System.out.println("DateTimeFormatter.ISO_LOCAL_TIME -> " + offsetDateTimeString4);
        System.out.println("DateTimeFormatter.ISO_OFFSET_TIME -> " + offsetDateTimeString5);
        System.out.println("DateTimeFormatter.ISO_TIME -> " + offsetDateTimeString6);
        System.out.println("DateTimeFormatter.ISO_LOCAL_DATE_TIME -> " + offsetDateTimeString7);
        System.out.println("DateTimeFormatter.ISO_OFFSET_DATE_TIME -> " + offsetDateTimeString8);
        System.out.println("DateTimeFormatter.ISO_ZONED_DATE_TIME -> " + offsetDateTimeString9);
        System.out.println("DateTimeFormatter.ISO_DATE_TIME -> " + offsetDateTimeString10);
        System.out.println("DateTimeFormatter.ISO_ORDINAL_DATE -> " + offsetDateTimeString11);
        System.out.println("DateTimeFormatter.ISO_WEEK_DATE -> " + offsetDateTimeString12);
        System.out.println("DateTimeFormatter.ISO_INSTANT -> " + offsetDateTimeString13);
        System.out.println("DateTimeFormatter.BASIC_ISO_DATE -> " + offsetDateTimeString14);
        System.out.println("DateTimeFormatter.RFC_1123_DATE_TIME -> " + offsetDateTimeString15);
    }
}
The output as below.
DateTimeFormatter.ISO_LOCAL_DATE -> 2022-05-14
DateTimeFormatter.ISO_OFFSET_DATE -> 2022-05-14+07:00
DateTimeFormatter.ISO_DATE -> 2022-05-14+07:00
DateTimeFormatter.ISO_LOCAL_TIME -> 13:48:18.4340401
DateTimeFormatter.ISO_OFFSET_TIME -> 13:48:18.4340401+07:00
DateTimeFormatter.ISO_TIME -> 13:48:18.4340401+07:00
DateTimeFormatter.ISO_LOCAL_DATE_TIME -> 2022-05-14T13:48:18.4340401
DateTimeFormatter.ISO_OFFSET_DATE_TIME -> 2022-05-14T13:48:18.4340401+07:00
DateTimeFormatter.ISO_ZONED_DATE_TIME -> 2022-05-14T13:48:18.4340401+07:00
DateTimeFormatter.ISO_DATE_TIME -> 2022-05-14T13:48:18.4340401+07:00
DateTimeFormatter.ISO_ORDINAL_DATE -> 2022-134+07:00
DateTimeFormatter.ISO_WEEK_DATE -> 2022-W19-6+07:00
DateTimeFormatter.ISO_INSTANT -> 2022-05-14T06:48:18.434040100Z
DateTimeFormatter.BASIC_ISO_DATE -> 20220514+0700
DateTimeFormatter.RFC_1123_DATE_TIME -> Sat, 14 May 2022 13:48:18 +0700

Convert OffsetDateTime to String using toString() method

In Java we can simply use the OffsetDateTime.toString() method to convert a OffsetDateTime object to String as the example Java code below.

ConvertOffsetDateTimeToStringExample5.java

import java.time.OffsetDateTime;

public class ConvertOffsetDateTimeToStringExample5 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.now();

        String offsetDateTimeString = offsetDateTime.toString();

        System.out.println(offsetDateTimeString);
    }
}
The output as below.
2022-05-14T13:48:56.311744700+07:00

Happy Coding 😊

Java Convert OffsetDateTime to Epoch Seconds

Java Convert OffsetDateTime to ZoneOffset

Java Convert OffsetDateTime to LocalDate

Java Convert OffsetDateTime to LocalTime

Java Convert OffsetDateTime to LocalDateTime

Java Convert OffsetDateTime to ZonedDateTime

Java Convert OffsetDateTime to Instant

Java Convert OffsetDateTime to Date

Java Convert OffsetDateTime to Calendar

Java Convert OffsetDateTime to OffsetTime

Java Convert String to OffsetDateTime

Java Convert Date to OffsetDateTime

Java Convert Calendar to OffsetDateTime