Create a Copy of a TimeZone Object in Java



In order to get a copy of a TimeZone object in Java, we use the clone() method. The clone() method creates of a copy of the TimeZone.

Declaration −The java.util.TimeZone.clone() method is declared as follows −

public Object clone()

Let us see a Java program which creates a copy of the TimeZone object using the clone() method −

Example

 Live Demo

import java.util.*;
public class Example {
   public static void main( String args[] ) {
      // creating object of TimeZone
      TimeZone obj = TimeZone.getDefault();
      System.out.println("Initial object: \n" + obj);
      // copying the TimeZone object
      Object copy = obj.clone();
      System.out.println("Copied object: \n" + copy);
   }
}

Output

Initial object: sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Copied object: sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Updated on: 2020-06-26T09:17:25+05:30

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements