Home » Java programming language

Java GregorianCalendar clone() Method with Example

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

GregorianCalendar Class clone() method

  • clone() method is available in java.util package.
  • clone() method is used to returns a copy or clone of this GregorianCalendar object or in other words we can say it returns a shallow copy of this GregorianCalendar object.
  • clone() method is a non-static method, it is accessible with class object only and if we try to access the method with class name then we will get an error.
  • clone() method does not throw an exception at the time of cloning GregorianCalendar object.

Syntax:

    public Object clone();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Object, it returns a cloned GregorianCalendar object.

Example:

// Java program to demonstrate the example 
// of Object clone () method of 
// GregorianCalendar

import java.util.*;

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

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

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

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

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

Output

curr_ca: Sat Feb 15 07:03:02 GMT 2020
curr_ca.clone(): Sat Feb 15 07:03:02 GMT 2020


Comments and Discussions!

Load comments ↻





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