Create a copy of a TimeZone object in Java

Java 8Object Oriented ProgrammingProgramming

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]
raja
Published on 08-Jan-2019 07:29:32
Advertisements