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?
Asked
Active
Viewed 4.5k times
7 Answers
16
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));

Aliaksei Kliuchnikau
- 13,589
- 4
- 59
- 72

wangwei
- 196
- 5
-
This code failed on me so instead I used cal.add(Calendar.MILLISECOND, rawOffset) and did required timezone calculations myself to be independent from timezone IDs. See @Subhamay answer. – Constantin Zagorsky Aug 04 '20 at 12:24
-
Echoing @Constain Zagorsky's comment, this solution does give the correct, adjusted HOUR, MINUTE & SECOND - but milliseconds (`c.getTimeInMillis()`) always returns GMT. – Woodchuck Feb 17 '23 at 19:00
15
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.

karlgold
- 7,970
- 2
- 29
- 22
-
2Thanks for the Response . I am writing a small class and I don't want to import joda package for this small class. Can you suggest any other way ? Thanks again – Raj Feb 06 '12 at 07:30
11
Java 1.8 provides you with some new classes in package java.time
:
package learning.java8;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.Test;
public class JavaTimeLT {
@Test
public void zonedDataTimeExample() {
final ZoneId zoneId = ZoneId.of("Europe/Zurich");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), zoneId);
System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
}

Jens Piegsa
- 7,399
- 5
- 58
- 106

user667
- 469
- 4
- 9
4
I wrote the following program to get time for all the Timezones available, see if this helps...
String[] zoneIds = TimeZone.getAvailableIDs();
for (int i = 0; i < zoneIds.length; i++) {
TimeZone tz = TimeZone.getTimeZone(zoneIds[i]);
System.out.print(tz.getID() + " " + tz.getDisplayName());
Calendar calTZ = new GregorianCalendar(tz);
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());

Shashank Kadne
- 7,993
- 6
- 41
- 54
3
In Java 8, you can use the ZonedDateTime.now(ZoneId zone)
method:
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
LocalTime localTime = zonedDateTime.toLocalTime();
System.out.println(localTime);

Robby Cornelissen
- 91,784
- 22
- 134
- 156
3
check this. hope it will help.
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
Calendar cal = Calendar.getInstance();
int LocalOffSethrs = (int) ((cal.getTimeZone().getRawOffset()) *(2.77777778 /10000000));
int ChinaOffSethrs = (int) ((tz.getRawOffset()) *(2.77777778 /10000000));
TimeZone tz1 = TimeZone.getTimeZone("US/Central");
String ss =cal.getTimeZone().getDisplayName();
System.out.println("Local Time Zone : " + ss);
System.out.println("China Time : " + tz.getRawOffset());
System.out.println("Local Offset Time from GMT: " + LocalOffSethrs);
System.out.println("China Offset Time from GMT: " + ChinaOffSethrs);
cal.add(Calendar.MILLISECOND,-(cal.getTimeZone().getRawOffset()));
//cal.add(Calendar.HOUR,- LocalOffSethrs);
cal.add(Calendar.MILLISECOND, tz.getRawOffset());
Date dt = new Date(cal.getTimeInMillis());
System.out.println("After adjusting offset Acctual China Time :" + dt);

Subhamay
- 197
- 1
- 5
1
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Seoul"));
GregorianCalendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
System.out.println(dateFormat.format( cal.getTime()));

Bae Cheol Shin
- 1,498
- 1
- 11
- 10