Java Utility Library

Java GregorianCalendar - clone() Method



The java.util.GregorianCalendar.clone() method returns a copy of the given GregorianCalendar object.

Syntax

public Object clone()

Parameters

No parameter is required.

Return Value

Returns a copy of the given object.

Exception

NA

Example:

In the example below, the java.util.GregorianCalendar.clone() method is used to create a copy of the given GregorianCalendar object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar object with specified date
    GregorianCalendar Cal1 = new GregorianCalendar(2015, 10, 25);

    //printing the Calendar
    System.out.println("Original Calendar: " + Cal1.getTime());

    //creating a clone of the Calendar
    GregorianCalendar Cal2 = new GregorianCalendar();
    Cal2 = (GregorianCalendar)Cal1.clone();

    //printing the Calendar
    System.out.println("Cloned Calendar: " + Cal2.getTime());
  }
}

The output of the above code will be:

Original Calendar: Wed Nov 25 00:00:00 GMT 2015
Cloned Calendar: Wed Nov 25 00:00:00 GMT 2015

❮ Java.util - GregorianCalendar