In this section, you will see how to convert time into another TimeZone.
Example, Java date conversion TimeZone.
In this example, you will see how to convert local time into another the time zone. Here first of all , we have to create a object of GregorianCalendar class by using GregorianCalendar() constructor. It constructs a default GregorianCalendar using the current time in the default time zone with the default locale. The set(int field, int value) method of Calendar class is used for setting time fields with values.
In next step, we again creates a object of GregorianCalendar with specified TimeZone, in which we wants to change. The getTimeZone(string str) method of TimeZone class gets the time for the specific ID. Here, we are passing ID Japan. So this example converts the local time into Japan time Zone.
Code:import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; public class ConvertTimeZone { public static void main(String arr[]) { Calendar localTime = new GregorianCalendar(); localTime.set(Calendar.HOUR, 5); localTime.set(Calendar.MINUTE, 12); localTime.set(Calendar.SECOND, 7); int hour = localTime.get(Calendar.HOUR); int minutes = localTime.get(Calendar.MINUTE); int second = localTime.get(Calendar.SECOND); System.out.println("Local TimeZone : "); System.out.println("Hour : Minutes : Second "); System.out.println(+hour + " : " + minutes + " : " + second); Calendar japanTimeZone = new GregorianCalendar(TimeZone .getTimeZone("japan")); japanTimeZone.setTimeInMillis(localTime.getTimeInMillis()); hour = japanTimeZone.get(Calendar.HOUR); minutes = japanTimeZone.get(Calendar.MINUTE); second = japanTimeZone.get(Calendar.SECOND); System.out.println("Japan TimeZone : "); System.out.println("Hour : Minutes : Second "); System.out.println(+hour + " : " + minutes + " : " + second); } }Output
Local TimeZone :
Hour : Minutes : Second 5 : 12 : 7 Japan TimeZone : Hour : Minutes : Second 11 : 42 : 7 |
[ 0 ] Comments