Over a million developers have joined DZone.

Local time of any country based on GMT with Java

DZone's Guide to

Local time of any country based on GMT with Java

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

In a recent change request for one application i was working on, it was requested to update the time stamp a certain task was done based on the location of the user. We have a separate Country table stored which has the GMT action and the GMT off set defined in minutes.

I was searching for a way to get the local time of a certain country using the Calendar object. There were many sites that gave various information which led to me finding a work around by testing with various parameters. This is what i came up with in the end.

Following i have encapsulated the final outcome into a method so it would be easier to understand and use if anyone ever needs it;

 public static final String GMT_STR = "GMT";
	public static final String COLON_STR = ":";
	public static final String ZERO = "0";
	 * This method will take as parameters the GMT off set in minutes
	 * as well as the GMT actions i.e + or - and based on that will
	 * output the calendar object representing that specific time in that
	 * location. The GMT is defined in minutes so that the calculation is
	 * simplified.
	 * @param gmtOffSetHrs 
	 * @param gmtAction
	 * @return
	public static Calendar getLocalTime(int gmtOffSetMins,
			String gmtAction) {

		// This will get the quotient part of the division
		int gmtHours = (gmtOffSetMins / 60);
		// This will get the remainder of the division which will give us the
		// minutes portion
		Integer gmtMins = Integer.valueOf((gmtOffSetMins % 60));
		if (gmtMins < 10) {
			gmtMins = Integer.valueOf(gmtMins + "0");
		 * This will produce a String in the format of GMT+5:30
		String gmtString = null;
		if (gmtMins == 0) {
			gmtString = GMT_STR + gmtAction + gmtHours + COLON_STR + gmtMins+ ZERO;
		} else {
			gmtString = GMT_STR + gmtAction + gmtHours + COLON_STR + gmtMins;

		 * This calendar object will give you the local time of the specific country
		 * based on the gmt offset hours and the action passed. 
		return Calendar.getInstance(TimeZone.getTimeZone(TimeZone.getTimeZone(


Thats all you need. The returned calendar will have the local time of that specific country depending on the GMT minutes and action you passed. Note that i have given the GMT in minutes to ease the calculation. Normally you will get the GMT in hours so you will need to convert that to minutes.

Ofcourse when daylight savings come into play you will need to change this accordingly. For our application this was not needed so we didnt do that change. To handle daylight savings we will have to keep the day light saving changes somewhere and deduct or add appropriately to the base GMT.

If you guys know anyway of doing this in a much simpler way or you have any suggestions for improvements pls do leave by a comment which is as always much appreciated.

Cheers Guys!!!!

From http://dinukaroshan.blogspot.com/2011/06/local-time-of-any-country-based-on-gmt.html

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}