Java OffsetDateTime.format() Method with Examples
Tags: Java OffsetDateTime Java 8 DateTimeFormatter
In this Java core tutorial we learn how to use the java.time.OffsetDateTime.format(DateTimeFormatter formatter) method to format a OffsetDateTime object to a date time String in Java programming language.
How to use OffsetDateTime.format() method
In Java with a given OffsetDateTime object we can use the OffsetDateTime.format(DateTimeFormatter formatter) method to convert a OffsetDateTime object to date time String in specified format.
For example, in the following Java program we use format() method with custom date time format patterns to convert OffsetDateTime object to a specified date time format String.
OffsetDateTimeFormatExample1.java
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class OffsetDateTimeFormatExample1 {
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-MM-yyyy HH:mm:ss X");
String offsetDateTimeString2 = offsetDateTime.format(dateTimeFormatter2);
DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("dd-MM-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-MM-yyyy HH:mm:ss X -> " + offsetDateTimeString2);
System.out.println("dd-MM-yyyy HH:mm:ss O -> " + offsetDateTimeString3);
}
}
dd-MM-yyyy HH:mm:ss Z -> 11-05-2022 22:58:17 +0700
dd-MM-yyyy HH:mm:ss X -> 11-05-2022 22:58:17 +07
dd-MM-yyyy HH:mm:ss O -> 11-05-2022 22:58:17 GMT+7
We also can use format() method with predefined DateTimeFormatter constants as below.
OffsetDateTimeFormatExample2.java
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class OffsetDateTimeFormatExample2 {
public static void main(String... args) {
OffsetDateTime offsetDateTime = OffsetDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String offsetDateTimeString = offsetDateTime.format(dateTimeFormatter);
System.out.println(offsetDateTimeString);
}
}
2022-05-11T22:58:44.0705987+07:00
Happy Coding 😊
Related Articles
Java Compare two OffsetDateTime Values
Java OffsetDateTime.atZoneSameInstant() Method with Examples
Java OffsetDateTime.atZoneSimilarLocal() 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.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.now() Method with Examples
Java OffsetDateTime.of() Method with Examples
Java OffsetDateTime.ofInstant() Method with Examples
Java OffsetDateTime.parse() 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.format() Method with Examples
Java OffsetDateTime.atZoneSameInstant() Method with Examples
Java OffsetDateTime.atZoneSimilarLocal() Method with Examples