Java OffsetDateTime.now() Method with Examples

Tags: Java OffsetDateTime Java 8

In this Java core tutorial we learn how to use the java.time.OffsetDateTime.now() method to instantiate new OffsetDateTime object which represents the current date and time in Java programming language.

Table of contents

  1. Get OffsetDateTime as Current Date Time from System Clock and Default Time Zone
  2. Get OffsetDateTime as Current Date Time from Specified Time Zone
  3. Get OffsetDateTime as Current Date Time from Specified Clock

Get OffsetDateTime as Current Date Time from System Clock and Default Time Zone

In Java we can use the OffsetDateTime.now() static method to get a new OffsetDateTime object from system clock in default time zone as the following Java code.

OffsetDateTimeNowExample1.java

import java.time.OffsetDateTime;

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

        System.out.println(offsetDateTime);
    }
}
The output as below.
2022-05-08T15:36:52.233193500+07:00

Get OffsetDateTime as Current Date Time from Specified Time Zone

In Java we can use the OffsetDateTime.now(ZoneId zone) static method to get a new OffsetDateTime object from system clock in a specified time zone as the following Java code.

OffsetDateTimeNowExample2.java

import java.time.OffsetDateTime;
import java.time.ZoneId;

public class OffsetDateTimeNowExample2 {
    public static void main(String... args) {
        ZoneId systemZone = ZoneId.systemDefault();
        ZoneId parisZone = ZoneId.of("Europe/Paris");
        ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");

        OffsetDateTime offsetDateTime1 = OffsetDateTime.now(systemZone);
        OffsetDateTime offsetDateTime2 = OffsetDateTime.now(parisZone);
        OffsetDateTime offsetDateTime3 = OffsetDateTime.now(tokyoZone);

        System.out.println(offsetDateTime1);
        System.out.println(offsetDateTime2);
        System.out.println(offsetDateTime3);
    }
}
The output as below.
2022-05-08T15:37:13.258285300+07:00
2022-05-08T10:37:13.258285300+02:00
2022-05-08T17:37:13.259342800+09:00

Get OffsetDateTime as Current Date Time from Specified Clock

In Java we can use the OffsetDateTime.now(Clock clock) static method to get a new OffsetDateTime object from a specified clock as the following Java code.

OffsetDateTimeNowExample3.java

import java.time.Clock;
import java.time.OffsetDateTime;
import java.time.ZoneId;

public class OffsetDateTimeNowExample3 {
    public static void main(String... args) {
        Clock utcClock = Clock.systemUTC();
        Clock systemClock = Clock.systemDefaultZone();
        Clock losAngelesClock = Clock.system(ZoneId.of("America/Los_Angeles"));

        OffsetDateTime offsetDateTime1 = OffsetDateTime.now(utcClock);
        OffsetDateTime offsetDateTime2 = OffsetDateTime.now(systemClock);
        OffsetDateTime offsetDateTime3 = OffsetDateTime.now(losAngelesClock);

        System.out.println(offsetDateTime1);
        System.out.println(offsetDateTime2);
        System.out.println(offsetDateTime3);
    }
}
The output as below.
2022-05-08T08:37:36.293999500Z
2022-05-08T15:37:36.293999500+07:00
2022-05-08T01:37:36.293999500-07:00

Happy Coding 😊

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.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