Java Utility Library

Java GregorianCalendar - from() Method



The java.util.GregorianCalendar.from() method is used to obtain an instance of GregorianCalendar with the default locale from a ZonedDateTime object.

Syntax

public static GregorianCalendar from(ZonedDateTime zdt)

Parameters

zdt Specify the zoned date-time object to convert.

Return Value

Returns the gregorian calendar representing the same point on the time-line as the zoned date-time provided.

Exception

  • Throws NullPointerException, if zdt is null.
  • Throws IllegalArgumentException, if the zoned date-time is too large to represent as a GregorianCalendar.

Example:

In the example below, the java.util.GregorianCalendar.from() method is used to obtain an instance of GregorianCalendar with the default locale from a ZonedDateTime object.

import java.util.*;
import java.time.ZonedDateTime; 

public class MyClass {
  public static void main(String[] args) {
    //creating a ZonedDateTime object
    ZonedDateTime zdt = ZonedDateTime.parse("2015-10-05T08:20:10+05:30[GMT]");  
    
    //printing the ZonedDateTime object
    System.out.println("ZonedDateTime is: " + zdt); 

    //creating an instance of GregorianCalendar
    //from ZonedDateTime object
    GregorianCalendar Cal = new GregorianCalendar();
    Cal.from(zdt);

    //printing the time from GregorianCalendar object
    System.out.println("GregorianCalendar date is: " + Cal.getTime()); 
  }
}

The output of the above code will be:

ZonedDateTime is: 2015-10-05T08:20:10Z[GMT]
GregorianCalendar date is: Wed Sep 09 05:22:18 UTC 2020

❮ Java.util - GregorianCalendar