Rahman
Rahman

Reputation: 3785

Difference of time from different timezone

Can anyone please tell me why following code is returning 10.5 hours whereas the actual difference between IST and EST is 9.5 hours.

Using getRawOffset() :

System.out.println(TimeZone.getDefault().getRawOffset() - TimeZone.getTimeZone("EST").getRawOffset()); 

Using getOffset() :

TimeZone current = TimeZone.getDefault();
TimeZone db = TimeZone.getTimeZone("EST");
long now = System.currentTimeMillis();  
System.out.println(db.getOffset(now) - current.getOffset(now));

My JVM is in IST timezone

Upvotes: 0

Views: 116

Answers (2)

Prim
Prim

Reputation: 2968

IST is UTC+5:30 and EST is UTC-5:00

So there are 10:30 between us. Maybe you want EDT and not EST

See : https://www.timeanddate.com/worldclock/converter.html?iso=20170630T090000&p1=tz_ist&p2=tz_est&p3=tz_et

By the way, in Java 8 the code would be :

    final LocalDateTime ist = LocalDateTime.now(ZoneId.of("IST", ZoneId.SHORT_IDS));
    final LocalDateTime est = LocalDateTime.now(ZoneId.of("EST", ZoneId.SHORT_IDS));
    System.out.println(Duration.between(ist, est)); // Prints PT-10H-30M0.001S

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140299

Owing to... reasons, "EST" doesn't observe daylight savings changes (similarly, HST and MST don't, whereas PST (and others) do).

Use "America/New_York" instead.

Note the statement in the Javadoc

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

"IST" can refer to "Irish Standard Time", "Israel Standard Time" and "Indian Standard Time". Never use a 3-letter abbreviation to refer to a time zone.

Upvotes: 2

Related Questions