Java Date clone() Method



Description

The Java Date clone() returns a shallow copy of this Date object.

Declaration

Following is the declaration for java.util.Date.clone() method

public Object clone()

Parameters

NA

Return Value

This method does not return any value.

Exception

NA

Getting Clone of a Given Date Example

The following example shows the usage of Java Date clone() method. We're creating a Date instance and then its clone is created using clone() method. Both the dates are printed.

package com.tutorialspoint;

import java.util.Date;

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

      // create a date
      Date date = new Date(122, 10, 4);

      // clone it to a second date
      Object date2 = date.clone();

      // print the results
      System.out.println("Original Date:" + date.toString());
      System.out.println("Cloned date :" + date2.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Date:Fri Nov 04 00:00:00 IST 2022
Cloned date :Fri Nov 04 00:00:00 IST 2022

Modifying Clone of a Given Date Example

The following example shows the modification done to cloned date instance created using Java Date clone() method. We're creating a Date instance and then its clone is created using clone() method. Both the dates are printed.

package com.tutorialspoint;

import java.util.Date;

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

      // create a date
      Date date = new Date(122, 10, 4);

      // clone it to a second date
      Object dateObj = date.clone();

      // get the date instance
      Date date2 = (Date) dateObj;

      // update the month
      date2.setMonth(8);

      // print the results
      System.out.println("Original Date:" + date.toString());
      System.out.println("Cloned date :" + date2.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Date:Fri Nov 04 00:00:00 IST 2022
Cloned date :Sun Sep 04 00:00:00 IST 2022

Modifying Clone of a Given Date Example

The following is another example showing the modification done to cloned date instance created using Java Date clone() method. We're creating a Date instance and then its clone is created using clone() method. Both the dates are printed.

package com.tutorialspoint;

import java.util.Date;

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

      // create a date
      Date date = new Date(122, 10, 4);

      // clone it to a second date
      Date date2 = (Date) date.clone();

      // update the month
      date2.setMonth(8);

      // print the results
      System.out.println("Original Date:" + date.toString());
      System.out.println("Cloned date :" + date2.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Original Date:Fri Nov 04 00:00:00 IST 2022
Cloned date :Sun Sep 04 00:00:00 IST 2022
java_util_date.htm
Advertisements