Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local time of different time zones?

Tags:

I want to get local time of different time zones using Java code. Based on the time zone passed to the function I need that time zone's local time. How to achieve this?

like image 456
Raj Avatar asked Feb 06 '12 06:02

Raj


People also ask

How do I get a specific timezone offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

How do you find time zones with time?

The . getTimezoneOffset() method should work. This will get the time between your time zone and GMT. You can then calculate to whatever you want.

How do you convert UTC time to local time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

How do you change from one time zone to another?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.


2 Answers

java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT+1"); java.util.Calendar c = java.util.Calendar.getInstance(tz);  System.out.println(c.get(java.util.Calendar.HOUR_OF_DAY)+":"+c.get(java.util.Calendar.MINUTE)+":"+c.get(java.util.Calendar.SECOND)); 
like image 178
wangwei Avatar answered Sep 28 '22 05:09

wangwei


I'd encourage you to check out Joda Time, an alternative (but very popular) to the standard Java date and time API:

http://joda-time.sourceforge.net/index.html

Using Joda Time, I think this is what you what:

import org.joda.time.DateTime; import org.joda.time.DateTimeZone;  public class TimeZoneDemo {    public static void main(String[] args) {      DateTime now = new DateTime(System.currentTimeMillis(), DateTimeZone.forID("UTC"));     System.out.println("Current time is: " + now);   } } 

You just need to know the standard ID for the time zone in question, such as UTC.

like image 39
karlgold Avatar answered Sep 28 '22 05:09

karlgold