• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Use ZonedDateTime now() Method in Java

August 31, 2023 //  by Talha Malik

A ZonedDateTime is an object in Java that is used to provide information about the date-time and the time zone. The class keeps a record of the date and time. The time is stored in nanoseconds which is highly precise whereas the time zone has a zone offset that handles the local date times. This class also transforms the local timeline of LocalDateTime to the instant timeline of Instant.

ZonedDateTime now() returns the time and date that is in use currently. This article discusses the ZonedDateTime now() in detail and explains its two methods with examples.

How to Use ZonedDateTime now() Class?

The now() function from the ZonedDateTime class gets the present date-time from the system clock using the default time zone. The offset and zone are configured according to the time zone within the clock. The class contains information of current date and time with a precision of nanoseconds.

Syntax

ZonedDateTime now()

Parameters

There is no parameter for the now() function.

Return Value

The function returns the present time and date in accordance with the system’s clock.

Example

The following Java code explains the ZonedDateTime.now() method:

import java.time.*;
public class func_ZonedDateTime {
public static void main(String[] args)
{
ZonedDateTime ZDT
= ZonedDateTime.now();

System.out.println("The zoned date time will be : "
+ ZDT);
}
}

In the above code,

  • java.time is imported which contains classes that enable it to work with date and time. 
  • The code defines a main class func_ZonedDateTime with a main method.
  • Inside the main method, ZonedDateTime ZDT = ZonedDateTime.now() creates an instance ZonedDateTime that contains an object ZDT
  • System.out.println utilizes the system’s default time zone to print the current date and time.

Output

The following output displays the present date and time fetched through the function ZonedDateTime now():

The ZonedDateTime now() function contains two methods. Let us explain each method in detail.

Method 1: Using now(ZoneId zone)

The first method is to use now(ZoneId zone). This method returns the present date and time in a specified zone. The function creates a ZonedDateTime object that has information about the present date and time of the specified zone.

Syntax

public static ZonedDateTime now(ZoneId zone)
  • The parameter is ‘ZoneId zone’ which indicates the time zone for which the current date and time from a ZonedDateTime object is fetched.
  • The method returns the present date and time of a particular zone.

Code:

The following code explains how ZonedDateTime.now() can get the date and time of the specified zone:

import java.time.*;
public class func_ZonedDateTime {
    public static void main(String[] args)
    {
        ZoneId zID = ZoneId.of("Asia/Karachi");
        ZonedDateTime ZDT
            = ZonedDateTime.now(zID);
        System.out.println("The zoned date time will be : "
                          + ZDT);
    }
}

In the above code:

  • java.time is imported which contains classes that enable it to work with date and time. 
  • The code defines a main class func_ZonedDateTime with a main method.
  • Inside the main method, ZonedDateTime zID = ZoneId.of(“Asia/Karachi”) creates a ZonedId object zID which stores the zone ID of the specified region.
  • The zID containing the region’s ID is then given to the ZonedDateTime.now() function. 
  • The current date and time based on the time zone of the specified region gets printed through System.out.println.

Output

The following output displays the present date and time of a specified zone fetched through the function ZonedDateTime now(ZoneId zone):

Method 2: Using now(Clock clock)

The second way the ZonedDateTime now() Method can be used is by providing it the parameters Clock clock that returns the present date and time based on a specified clock.

Syntax

public static ZonedDateTime now(Clock clock)
  • The parameter provided to the function is “Clock clock” that specifies the clock being used
  • The function returns the current date and time based on a particular clock

Code

Let us see the following code to understand the method better:

import java.time.*;
public class func_ZonedDateTime {
    public static void main(String[] args)
    {
       Clock clc = Clock.systemUTC();
        ZonedDateTime ZDT
            = ZonedDateTime.now(clc);
        System.out.println("The zoned date time will be : "
                          + ZDT);
    }
}

In the above code:

  • java.time is imported which contains classes that enable it to work with date and time. 
  • The code defines a main class func_ZonedDateTime with a main method.
  • A clock instance named clc is created having the value of Clock.systemUTC() which represents the current instant in Coordinated Universal Time (UTC).
  • Inside the main method, ZonedDateTime ZDT = ZonedDateTime.now() creates a ZonedDateTime object ZDT which takes the parameter clc.
  • The current date and time based on the UTC time zone gets printed through System.out.println.

Output

The following output displays the present date and time according to a specified clock(in this case UTC) and is fetched through the function ZonedDateTime now(Clock clock):

Conclusion

The ZonedDateTime now() class in Java programming returns the present date and time according to the system’s default time zone. The class contains two methods that take the parameters of clock and zone. The method taking clock as a parameter will return the current time and date according to the clock specified and the method with zone as its parameter gives the present time and date based on a specified region. This article discussed the ZonedDateTime now() Method in Java and discussed its two methods along with the examples in detail.

Category: Java

Previous Post: « How to Use the java.util.ArrayList Class
Next Post: What is Variable Hiding and Variable Shadowing in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact