Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to Convert Time Between Timezone in Java?

$
0
0

timezone.map - Crunchify

TimeZone
 represents a time zone offset, and also figures out daylight savings.

Typically, you get a 

TimeZone
 using 
getDefault
 which creates a 
TimeZone
 based on the time zone where the program is running. For example, for a program running in Japan, 
getDefault
 creates a
TimeZone
 object based on Japanese Standard Time.

You can also get a 

TimeZone
 using 
getTimeZone
 along 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 
TimeZone
 object 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.


Viewing all articles
Browse latest Browse all 1037

Trending Articles