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:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println( "Date and Time in"
+ " cal object : " + cal.getTime());
GregorianCalendar newcalender =
new GregorianCalendar();
newcalender = (GregorianCalendar)cal.clone();
System.out.println( "Date and Time in" +
" newcalender object : " + newcalender.getTime());
}
}
|
Output:
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:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
GregorianCalendar cal1, cal2;
cal1 = (GregorianCalendar)GregorianCalendar.
getInstance();
System.out.println( "Current Date and Time : "
+ cal1.getTime());
cal1.add((GregorianCalendar.MONTH), 2 );
cal2 = (GregorianCalendar)cal1.clone();
System.out.println( "New Date and Time : "
+ cal2.getTime());
}
}
|
Output:
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()
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
03 Aug, 2018
Like Article
Save Article