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: 
" + obj);       // copying the TimeZone object       Object copy = obj.clone();       System.out.println("Copied object:
" + 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: 26-Jun-2020

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements