Thursday, May 14, 2009

How to get other TimeZone time by JAVA

You can get any timezone time just you should have the timezone ID. Here I gave two methods.
1. How to get all timezone ID.
2. How to get time of any timezone ID.
Both methods are in the same java file (OtherTimeZoneTime.java)


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class OtherTimeZoneTime {

public static void main(String[] args) {
String timeZoneId = "America/Chicago";
getTime(timeZoneId);
// getAllTimeZone();
}

public static void getAllTimeZone() {
String[] zoneIds = TimeZone.getAvailableIDs();
for (int i = 0; i < zoneIds.length; i++) {
TimeZone tz = TimeZone.getTimeZone(zoneIds[i]);
System.out.println(tz.getID() + " " + tz.getDisplayName());
}
}

public static void getTime(String timeZoneId) {
Calendar calTZ = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
calTZ.setTimeInMillis(new Date().getTime());
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, calTZ.get(Calendar.YEAR));
cal.set(Calendar.MONTH, calTZ.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, calTZ.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, calTZ.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, calTZ.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, calTZ.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, calTZ.get(Calendar.MILLISECOND));
System.out.println(cal.getTime());
}
}

Some timeZoneId :
Asia/Calcutta
Europe/Vatican
Asia/Istanbul
Australia/Darwin
Australia/Melbourne
Australia/Sydney
America/Cordoba
America/Fortaleza
America/Godthab
America/Jujuy
America/Maceio

7 comments:

  1. Thanks Binod. It worked fine and I implemented in my project. Very nice tutorial and easy solution. Plesae keep it up.

    ReplyDelete
  2. I checkd your code and worked fine. how I can get Japan time by using this code. What would be the timezoneID for Japan?

    ReplyDelete
  3. Hello Binod.
    Really nice blog.
    I need ur help.
    I want to get the server time. Like anybody visiting a site must be shown the current server time. I tried using get date() but it gives the current time of users country.

    So could u please help me in getting the current server time irrespective of from where the user surfs the site.

    Waiting 4 ur reply.

    ReplyDelete
  4. Hi Download Manager,
    On your request I wrote code to get current server time on client page and Time would be updated on every second.

    http://binodsuman.blogspot.com/2009/05/get-current-server-time-on-client-page.html

    Thanks,

    Binod Suman
    http://binodsuman.blogspot.com

    ReplyDelete
  5. This is what I excallty searching. Using this way we can find out any timezone time by Java. Thanks.

    ReplyDelete
  6. Helpful code to find the other timezone time using java. I visited some more blog on this site and got very usefule. Really appriciable.

    ReplyDelete
  7. Hi,
    I need to show in hh:mm:ss format.
    How to show like that?....

    ReplyDelete

Please put your feedback or your any question related to this blog. Thanks.