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?
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.
The . getTimezoneOffset() method should work. This will get the time between your time zone and GMT. You can then calculate to whatever you want.
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.
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.
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With