Java GregorianCalendar from() Method



Description

The Java GregorianCalendar from(ZonedDateTime) method obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object.

Declaration

Following is the declaration for java.util.GregorianCalendar.from(ZonedDateTime zdt) method

public static GregorianCalendar from​(ZonedDateTime zdt)

Parameters

zdt − the zoned date-time object to convert.

Return Value

This method returns the gregorian calendar representing the same point on the time-line as the zoned date-time provided.

Exception

NullPointerException − if zdt is null.

IllegalArgumentException − if the zoned date-time is too large to represent as a GregorianCalendar.

Getting GregorianCalendar Instance from Current Time of ZonedDateTime Example

The following example shows the usage of Java Calendar from(ZonedDateTime) method. We're creating a Calendar instance of current date using a ZonedDateTime object.

package com.tutorialspoint;

import java.time.ZonedDateTime;
import java.util.GregorianCalendar;

public class GregorianCalendarDemo {
   public static void main(String[] args) {

      // create a new calendar
      GregorianCalendar cal1 = GregorianCalendar.from(ZonedDateTime.now());

      // print the current date and time
      System.out.println("" + cal1.getTime());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Fri Nov 18 10:29:07 IST 2022
java_util_gregoriancalendar.htm
Advertisements