TimeZone clone() Method in Java with Examples

The clone() method of TimeZone class in Java is used to create an identical copy of an existing this TimeZone.

Syntax:

time_zone.clone()

Parameters: The method does not take any parameters.

Return Value: The method returns an instance of TimeZone which is the copy of this TimeZone.

Below program illustrates the working of clone() Method of TimeZone:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate clone() method
  
import java.util.*;
  
public class TimeZoneDemo {
    public static void main(String args[])
    {
  
        // Creating an object of TimeZone class.
        TimeZone time_zone
            = TimeZone.getDefault();
        System.out.println("Original TimeZone: "
                           + time_zone);
  
        System.out.println();
  
        // Cloning and displaying the time zone
        System.out.println("Cloned TimeZone: "
                           + time_zone.clone());
    }
}

chevron_right


Output:

Original TimeZone:
sun.util.calendar.ZoneInfo[id=”Etc/UTC”,offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

Cloned TimeZone:
sun.util.calendar.ZoneInfo[id=”Etc/UTC”,offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate clone() method
  
import java.util.*;
  
public class TimeZoneDemo {
    public static void main(String args[])
    {
  
        // creating Timezone object whose id is Europe/Berlin
        TimeZone time_zone
            = TimeZone.getTimeZone("Europe/Berlin");
  
        System.out.println("Original TimeZone: "
                           + time_zone);
  
        System.out.println();
  
        // Cloning and displaying the time zone
        System.out.println("Cloned TimeZone: "
                           + time_zone.clone());
    }
}

chevron_right


Output:

Original TimeZone:
sun.util.calendar.ZoneInfo[id=”Europe/Berlin”,offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]

Cloned TimeZone:
sun.util.calendar.ZoneInfo[id=”Europe/Berlin”,offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.