TimeZonerepresents a time zone offset, and also figures out daylight savings.
Typically, you get a
TimeZoneusing
getDefaultwhich creates a
TimeZonebased on the time zone where the program is running. For example, for a program running in Japan,
getDefaultcreates a
TimeZoneobject based on Japanese Standard Time.
You can also get a
TimeZoneusing
getTimeZonealong with a time zone ID. For instance, the time zone ID for the U.S. Pacific Time zone is “America/Los_Angeles”. So, you can get a U.S. Pacific Time
TimeZoneobject with:
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
List of all Java Examples.
Java Example:
package com.crunchify.tutorials; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; /** * @author Crunchify.com */ public class CrunchifyConvertTime { public static void main(String[] args) { Calendar localTime = Calendar.getInstance(); int hour = localTime.get(Calendar.HOUR); int minute = localTime.get(Calendar.MINUTE); int second = localTime.get(Calendar.SECOND); // // Print the local time // System.out.printf("Local time : %02d:%02d:%02d\n", hour, minute, second); // // Create a calendar object for representing a Singapore time zone. // Calendar indiaTime = new GregorianCalendar( TimeZone.getTimeZone("Asia/Singapore")); indiaTime.setTimeInMillis(localTime.getTimeInMillis()); hour = indiaTime.get(Calendar.HOUR); minute = indiaTime.get(Calendar.MINUTE); second = indiaTime.get(Calendar.SECOND); // // Print the local time in Germany time zone // System.out.printf("India time : %02d:%02d:%02d\n", hour, minute, second); } }
Output:
Local time : 01:45:53 India time : 04:45:53
The post How to Convert Time Between Timezone in Java? appeared first on Crunchify.