Java OffsetDateTime.of() Method with Examples
Tags: Java OffsetDateTime Java 8
In this Java core tutorial we learn how to use the java.time.OffsetDateTime.of() static methods to create new OffsetDateTime objects in Java programming language.
Table of contents
- Create OffsetDateTime object from LocalDate LocalTime and ZoneOffset
- Create OffsetDateTime object from LocalDateTime and ZoneOffset
- Create OffsetDateTime object from Year Month Day Hour Minute Second Nano of Second and ZoneOffset
Create OffsetDateTime object from LocalDate LocalTime and ZoneOffset
In Java we can use the OffsetDateTime.of(LocalDate date, LocalTime time, ZoneOffset offset) method to instantiate a new OffsetDateTime object from given LocalDate, LocalTime and ZoneOffset objects as following example Java code.
OffsetDateTimeOfExample1.java
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class OffsetDateTimeOfExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
ZoneOffset zoneOffset = ZoneId.of("Australia/Sydney").getRules().getOffset(Instant.now());
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDate, localTime, zoneOffset);
System.out.println(offsetDateTime);
}
}
2022-05-08T15:59:42.536030600+10:00
Create OffsetDateTime object from LocalDateTime and ZoneOffset
In Java we can use the OffsetDateTime.OffsetDateTime.of(LocalDateTime dateTime, ZoneOffset offset) method to instantiate a new OffsetDateTime object from given LocalDateTime and ZoneOffset objects as following example Java code.
OffsetDateTimeOfExample2.java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class OffsetDateTimeOfExample2 {
public static void main(String... args) {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneOffset zoneOffset = ZoneId.of("Australia/Sydney").getRules().getOffset(Instant.now());
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, zoneOffset);
System.out.println(offsetDateTime);
}
}
2022-05-08T16:00:42.062347400+10:00
Create OffsetDateTime object from Year Month Day Hour Minute Second Nano of Second and ZoneOffset
In Java we can use the OffsetDateTime.of( int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) method to instantiate a new OffsetDateTime object from given year, month, day, hour, minute, second, nano of second and ZoneOffset objects as the following example Java code.
OffsetDateTimeOfExample3.java
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class OffsetDateTimeOfExample3 {
public static void main(String... args) {
int year = 2022;
int month = 7;
int dayOfMonth = 11;
int hour = 7;
int minute = 20;
int second = 30;
int nanoOfSecond = 999;
ZoneOffset zoneOffset = ZoneId.of("Europe/Paris").getRules().getOffset(Instant.now());
OffsetDateTime offsetDateTime = OffsetDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond, zoneOffset);
System.out.println(offsetDateTime);
}
}
2022-07-11T07:20:30.000000999+02:00
Happy Coding 😊
Related Articles
Java OffsetDateTime.now() 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.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