Home » Java programming language

Java Calendar clone() Method with Example

Calendar Class clone() method: Here, we are going to learn about the clone() method of Calendar Class with its syntax and example.
Submitted by Preeti Jain, on January 23, 2020

Calendar Class clone() method

  • clone() method is available in java.util package.
  • clone() method is used to return the cloned object of this Calendar object.
  • clone() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • clone() method does not throw an exception at the time of cloning Calendar object.

Syntax:

    public Object clone();

Parameter(s):

  • It does not accept any parameter.

Return value:

In both the cases, the return type of the method is Object, it returns a copy to this object.

Example:

// Java Program to demonstrate the example of
// Object clone() method of Calendar

import java.util.*;

public class CloneOfCalendar {
    public static void main(String[] args) {

        // Instantiating two Calendar object
        Calendar curr_ca = Calendar.getInstance();
        Calendar clo_ca;

        // Display current calendar
        System.out.println("curr_ca: " + curr_ca.getTime());

        // By using clone() method is to copy the
        // object to another 
        clo_ca = (Calendar) curr_ca.clone();

        // Display cloned calendar
        System.out.println("curr_ca.clone(): " + clo_ca.getTime());
    }
}

Output

curr_ca: Thu Jan 23 11:42:29 GMT 2020
curr_ca.clone(): Thu Jan 23 11:42:29 GMT 2020


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.