The java.util.GregorianCalendar.clone() method of GregorianCalendar class is used to create a new object and copy all the contents of this GregorianCalendar instance into the new one.
Syntax:
public Object clone()
Parameters: This function does not accept any parameter.
Return Value: This function returns a copy of this object.
Examples:
Input: Mon Jul 23 14:35:27 UTC 2018
Output: Mon Jul 23 14:35:27 UTC 2018
Input: Current Date and Time is Mon Jul 23 14:35:27 UTC 2018
cal1.add((GregorianCalendar.MONTH), -7);
cal1.clone();
Output: Sat Dec 23 14:36:42 UTC 2017
Below programs illustrate the java.util.GregorianCalendar.clone() method:
Program 1:
// Java Program to illustrate GregorianCalendar.clone() // function import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Creating a new calendar GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); // Display the date and time System.out.println("Date and Time in" +" cal object : "+ cal.getTime()); GregorianCalendar newcalender = new GregorianCalendar(); // Cloning the object newcalender = (GregorianCalendar)cal.clone(); // Display date and time System.out.println("Date and Time in"+ " newcalender object : "+ newcalender.getTime()); } } |
Date and Time in cal object : Fri Aug 03 11:01:24 UTC 2018 Date and Time in newcalender object : Fri Aug 03 11:01:24 UTC 2018
Program 2:
// Java Program to illustrate // GregorianCalendar.clone() // function import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Creating a new calendar GregorianCalendar cal1, cal2; cal1 = (GregorianCalendar)GregorianCalendar. getInstance(); // Display the current date and time System.out.println("Current Date and Time : " + cal1.getTime()); // Modifying the current date and time cal1.add((GregorianCalendar.MONTH), 2); // Cloning the object cal2 = (GregorianCalendar)cal1.clone(); // Display date and time System.out.println("New Date and Time : " + cal2.getTime()); } } |
Current Date and Time : Fri Aug 03 11:01:27 UTC 2018 New Date and Time : Wed Oct 03 11:01:27 UTC 2018
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#clone()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.GregorianCalendar Class in Java
- GregorianCalendar setTimeZone() Method in Java
- GregorianCalendar getTimeZone() Method in Java
- GregorianCalendar getMinimum() Method in Java
- GregorianCalendar isLeapYear() Method in Java
- GregorianCalendar hashCode() Method in Java
- GregorianCalendar equals() Method in Java
- GregorianCalendar getMaximum() Method in Java
- GregorianCalendar getLeastMaximum() Method in Java
- GregorianCalendar getGreatestMinimum() Method in Java
- GregorianCalendar add() Method in Java
- GregorianCalendar getGregorianChange() Method
- LinkedList clone() Method in Java
- HashSet clone() Method in Java
- TreeSet clone() Method in Java
- HashMap clone() Method in Java
- Hashtable clone() Method in Java
- TreeMap clone() Method in Java
- EnumMap clone() Method in Java
- ArrayDeque clone() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

