Home » Java programming language

Java Date clone() Method with Example

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

Date Class clone() method

  • clone() method is available in java.util package.
  • clone() method is used to returns a copy or clone of this Date object or in other words, we can say it returns a shallow copy of this Date object.
  • clone() method is a non-static method, it is accessible with the class object only 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 Date 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 Date object.

Example:

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

import java.util.*;

public class CloneOfDate {
    public static void main(String[] args) {
        // create two Date object
        Date this_date = new Date(2016, 8, 20);

        // Display Date object
        System.out.println("this_date: " + this_date.toString());

        // By using clone() method is to clone
        // this date
        Object cloned_date = this_date.clone();

        // Display cloned date
        System.out.println("this_date.clone(): " + cloned_date.toString());
    }
}

Output

this_date: Wed Sep 20 00:00:00 GMT 3916
this_date.clone(): Wed Sep 20 00:00:00 GMT 3916


Comments and Discussions!

Load comments ↻





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