Calendar.clone()

Date

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());
}
}
Output
Mon Jul 28 10:11:12 IST 2025
Wed May 28 10:11:12 IST 2025
Date & time tutorials


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer