Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to different timezone [duplicate]

Tags:

I am trying to obtain a simple conversion from one time zone into another using Java Date and Calendar. I am trying to run the following code

    Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));     Date date = instance.getTime();     System.out.println(date);      GregorianCalendar instance2 = new GregorianCalendar(TimeZone.getTimeZone("Europe/Athens"));     instance2.setTime(instance.getTime());     System.out.println(instance2.getTime()); 

but that still returns the same date, rather than +1 hour... The whole problem seems trivial but i cannot find any simple answer to this. Thanks in advance for your help.

like image 234
Bober02 Avatar asked Nov 02 '12 15:11

Bober02


People also ask

How do I convert a date to a different time zone?

To convert a date to another time zone: Use the toLocaleString() method to get a string that represents the date according to the provided time zone. Pass the result to the Date() constructor. The returned Date object will have its date and time set according to the provided time zone.

Which function changes date from one timezone to another?

So, we need to use the SimpleDateFormat. setTimeZone() method to convert the date time to the given timezone.

How do you convert date to UTC format?

The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.


2 Answers

When you print the date using System.out.println(date); or System.out.println(instance2.getTime());, the Date returned by instance2.getTime() is TimeZone independent and always prints the date in local timezone.

Instead you may want to use DateFormat/SimpleDateFormat:

  DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");   formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));   System.out.println(formatter.format(date));    formatter.setTimeZone(TimeZone.getTimeZone("Europe/Athens"));   System.out.println(formatter.format(instance2.getTime())) 
like image 85
Yogendra Singh Avatar answered Oct 12 '22 16:10

Yogendra Singh


The accepted answer is correct. The java.util.Date class has no time zone assigned, yet it's toString implementation confusingly applies the JVM's current default time zone.

Avoid java.util.Date & .Calendar

This is one of many reasons to avoid the notoriously troublesome java.util.Date, .Calendar, and SimpleDateFormat classes bundled with Java. Avoid them. Instead use either:

  • Joda-Time
  • The new java.time package bundled with Java 8 and inspired by Joda-Time.

Joda-Time

Some example code in Joda-Time 2.3 follows. Search StackOveflow for many more examples and much discussion.

DateTimeZone timeZoneLondon = DateTimeZone.forID( "Europe/London" ); DateTimeZone timeZoneAthens = DateTimeZone.forID( "Europe/Athens" );  DateTime nowLondon = DateTime.now( timeZoneLondon ); DateTime nowAthens = nowLondon.withZone( timeZoneAthens ); DateTime nowUtc = nowLondon.withZone( DateTimeZone.UTC ); 

java.time

Java 8 and later has a new java.time package built-in. This package was inspired by Joda-Time. While they share some similarities and class names, they are different; each has features the other lacks. One notable difference is that java.time avoids constructors, instead uses static instantiation methods.

In the case of this Question, they work in the same fashion. Specify a time zone, and call a now method to get current moment, then create a new instance based on the old immutable instance to adjust for time zone.

Note the two different time zone classes. One is a named time zone including all the rules for Daylight Saving Time and other such anomalies plus an offset from UTC while the other is only the offset.

ZoneId zoneMontréal = ZoneId.of("America/Montreal");  ZonedDateTime nowMontréal = ZonedDateTime.now ( zoneMontréal );  ZoneId zoneTokyo = ZoneId.of("Asia/Tokyo");  ZonedDateTime nowTokyo = nowMontréal.withZoneSameInstant( zoneTokyo );  ZonedDateTime nowUtc = nowMontréal.withZoneSameInstant( ZoneOffset.UTC ); 

Actually the java.util.Date class does have a time zone buried within its source code. But the class ignores that time zone for most practical purposes. So, as shorthand, it’s often said that j.u.Date has no time zone assigned. Confusing? Yes. Avoid the mess that is j.u.Date and go with Joda-Time and/or java.time.

like image 24
Basil Bourque Avatar answered Oct 12 '22 16:10

Basil Bourque