Java .clone()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 16, 2023
Contribute to Docs

The .clone() method of the Calendar class is used to return a copy of a Calendar object.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

result = myCalendar.clone()

Returns a copy of the myCalendar Calendar object.

Example

This example creates a calendar, displays it, clones it and displays its clone.

import java.util.*;
public class CalendarCloneExample {
public static void main(String args[])
{
// Create a calendar object
Calendar original_calendar = new GregorianCalendar(1971, 8, 13);
// Print original calendar
System.out.println(original_calendar.getTime());
// Cloning the original
Calendar copy_calendar = (Calendar) original_calendar.clone();
// Print copy calendar
System.out.println(copy_calendar.getTime());
}
}

Output for the above code will be:

Mon Sep 13 00:00:00 UTC 1971
Mon Sep 13 00:00:00 UTC 1971

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours