Calendar.clone()
Calendar.clone() Copy the calendar object .
Returns the copy of the cloned object.
The method Set() is used to assign calendar fields .
Examples with output
Let us create one calendar object and we will generate one more by using clone().
package my_proj;
import java.util.Calendar;
public class my_first {
public static void main(String[] args) {
Calendar my_cal_d1 = Calendar.getInstance();// created calendar
my_cal_d1.set(2025,4,28,10,11,12);
Calendar my_cal_d2 = (Calendar) (Calendar) my_cal_d1.clone();
System.out.println( my_cal_d1.getTime());
System.out.println( my_cal_d2.getTime());
}
}
Output
Wed May 28 10:11:12 IST 2025
Wed May 28 10:11:12 IST 2025
The cloned object doesn't change even if the original object changes its value.
package my_proj;
import java.util.Calendar;
public class my_first {
public static void main(String[] args) {
Calendar my_cal_d1 = Calendar.getInstance();// created calendar
my_cal_d1.set(2025,4,28,10,11,12);
Calendar my_cal_d2 = (Calendar) (Calendar) my_cal_d1.clone();
my_cal_d1.set(2025,6,28,10,11,12); // Changed Month value
System.out.println( my_cal_d1.getTime());
System.out.println( my_cal_d2.getTime());
}
}
OutputMon Jul 28 10:11:12 IST 2025
Wed May 28 10:11:12 IST 2025
« Date & time tutorials
« Java
This article is written by plus2net.com team.
plus2net.com