UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset. For example, time in Copenhagen, Denmark is UTC + 1 means UTC time plus one hour. It is independent of Day light savings and should be used to store date and time in databases.

Conversion of Time Zones

Following example will showcase conversion of various timezones. We’ll print hour of the day and time in milliseconds. First will vary and second will remain same.

Example

Open Compiler

importjava.text.ParseException;importjava.util.Calendar;importjava.util.GregorianCalendar;importjava.util.TimeZone;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{Calendar date =newGregorianCalendar();

      date.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
      date.set(Calendar.HOUR_OF_DAY,12);System.out.println("UTC: "+ date.get(Calendar.HOUR_OF_DAY));System.out.println("UTC: "+ date.getTimeInMillis());

      date.setTimeZone(TimeZone.getTimeZone("Europe/Copenhagen"));System.out.println("CPH: "+ date.get(Calendar.HOUR_OF_DAY));System.out.println("CPH: "+ date.getTimeInMillis());

      date.setTimeZone(TimeZone.getTimeZone("America/New_York"));System.out.println("NYC: "+ date.get(Calendar.HOUR_OF_DAY));System.out.println("NYC: "+ date.getTimeInMillis());}}

Output

It will print the following result.

UTC: 12
UTC: 1511956997540
CPH: 13
CPH: 1511956997540
NYC: 7
NYC: 1511956997540

Available Time Zones

Following example will showcase the timezones available with the system.

Example

Open Compiler

importjava.text.ParseException;importjava.util.TimeZone;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{String[] availableIDs =TimeZone.getAvailableIDs();for(String id : availableIDs){System.out.println("Timezone = "+ id);}}}

Output

It will print the following result.

Timezone = Africa/Abidjan
Timezone = Africa/Accra
...
Timezone = VST

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *