The Java Calendar clone() returns a copy of this Calendar object. The operation performed on clone object are not reflected on the original calendar object.
DeclarationFollowing is the declaration for java.util.Calendar.clone() method
public Object clone()Parameters
NA
Return ValueThis method does not return any value.
ExceptionNA
Cloning a given Calendar Instance ExampleThe following example shows the usage of Java Calendar clone() method. We're creating a GregorianCalendar instance. We're creating a clone of it using clone() method and printing it.
package com.tutorialspoint; import java.util.Calendar; import java.util.GregorianCalendar; public class CalendarDemo { public static void main(String[] args) { // create new calendar at specific date. Calendar cal = new GregorianCalendar(2008, 05, 20); // print date for default value System.out.println("Past calendar : " + cal.getTime()); // create a clone of first cal Calendar cal2 = (Calendar) cal.clone(); // display the copy System.out.println("Cloned calendar : " + cal2.getTime()); } }Output
Let us compile and run the above program, this will produce the following result −
Past calendar : Fri Jun 20 00:00:00 IST 2008 Cloned calendar : Fri Jun 20 00:00:00 IST 2008Cloning a given GregorianCalendar Instance Example
The following example shows the usage of Java Calendar clone() method. We're creating a GregorianCalendar instance. We're creating a clone of it using clone() method. Clone calendar is modified and both the calendars are printed.
package com.tutorialspoint; import java.util.Calendar; import java.util.GregorianCalendar; public class CalendarDemo { public static void main(String[] args) { // create new calendar at specific date. Calendar cal = new GregorianCalendar(2008, 05, 20); // create a clone of first cal Calendar cal2 = (Calendar) cal.clone(); cal2.set(Calendar.YEAR, 2025); // print date for default value System.out.println("Past calendar : " + cal.getTime()); // display the clone calendar System.out.println("Cloned calendar : " + cal2.getTime()); } }Output
Let us compile and run the above program, this will produce the following result −
Past calendar : Fri Jun 20 00:00:00 IST 2008 Cloned calendar : Fri Jun 20 00:00:00 IST 2025Cloning a Current Dated Calendar Instance Example
The following example shows the usage of Java Calendar clone() method. We're creating a Calendar instance of current date. We're creating a clone of it using clone() method. Clone calendar is modified and both the calendars are printed.
package com.tutorialspoint; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { // create new calendar of current date Calendar cal = Calendar.getInstance(); // create a clone of first cal Calendar cal2 = (Calendar) cal.clone(); cal2.set(Calendar.YEAR, 2025); // print date for default value System.out.println("Past calendar : " + cal.getTime()); // display the clone calendar System.out.println("Cloned calendar : " + cal2.getTime()); } }Output
Let us compile and run the above program, this will produce the following result −
Past calendar : Fri Sep 23 15:14:08 IST 2022 Cloned calendar : Tue Sep 23 15:14:08 IST 2025
java_util_calendar.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4