-1

I have a specific requirement in which i have to get the difference between the system timezone and any other remote timezone and then ADD the current time to the date if System timezone is AHEAD of remote timezone and SUBTRACT if it is BEHIND.

Also Daylight Saving Time(DST) needs to be taken into account as the code could be DEPLOYED in ANY TIMEZONE.

I tried the following,

long now = System.currentTimeMillis();
long diffGMT = TimeZone.getTimeZone(ZoneId.systemDefault()).getOffset(now) - 
TimeZone.getTimeZone("GMT").getOffset(now);
        
long diffAEST = TimeZone.getTimeZone(ZoneId.systemDefault()).getOffset(now) - 
TimeZone.getTimeZone("AEST").getOffset(now);
        
System.out.println("diffGMT = " + diffGMT);
System.out.println("diffAEST = " + diffAEST);

But both the values returned are POSITIVE.

My system time is IST, and i tried GMT, which is BEHIND and AEST(Australia) which is AHEAD.

AM i DOING SOMETHING WRONG HERE ???

Once i get the desired positive or negative time, then i could add or subtract hours as per below code,

long totalSecs = (calculatedValueInMillis)/1000;
long hours = (totalSecs / 3600);
long mins = (totalSecs / 60) % 60;
long secs = totalSecs % 60;
VinodKhade
  • 147
  • 1
  • 2
  • 10
  • `TimeZone.getTimeZone("AEST")` resolves to **GMT**, since there is no time zone named `AEST`. Use `Australia/Sydney` instead. – Andreas Sep 15 '20 at 07:48
  • Why are you mixing old and new API? `TimeZone.getTimeZone(ZoneId.systemDefault())` is just weird. Either use old API, i.e. `TimeZone.getDefault()`, or use the new API, e.g. `ZoneId.of("Australia/Sydney").getRules().getOffset(now).getTotalSeconds()` – Andreas Sep 15 '20 at 07:57
  • @Andreas - Yes..Thank you for the suggestions. Yes....you are right....Australia/Sydney is giving negative output.... – VinodKhade Sep 15 '20 at 08:09
  • 1
    Does https://stackoverflow.com/questions/63814519/find-if-system-timezone-is-ahead-or-behind-utc-in-java/63814593#63814593 answer your question partly? – Arvind Kumar Avinash Sep 15 '20 at 08:14
  • @Andreas - I am not able to implement your suggestion. Could you please give the code in more detail as to how i could calculate the difference in time between system default timezone and "Australia/Sydney" timezone using the new API. How can i calculate long diffAEST in the above code using new API. ?? – VinodKhade Sep 15 '20 at 10:31

1 Answers1

0

The problem is that TimeZone.getTimeZone(String ID) "returns the GMT zone if the given ID cannot be understood". It does not throw an exception.

Since there is no time zone named AEST, TimeZone.getTimeZone("AEST") resolves to GMT.

Solution: Use Australia/Sydney instead.


The code in the question is mixing time APIs: TimeZone.getTimeZone(ZoneId.systemDefault())

Since the rest of the code is using the old Date API, it should have just used TimeZone.getDefault().

Alternatively, it can use the newer Time API, e.g. like this:

Instant now = Instant.now();
System.out.println(Date.from(now)); // Show default time zone

int offsetLocal = ZoneId.systemDefault().getRules().getOffset(now).getTotalSeconds();
int offsetSydney = ZoneId.of("Australia/Sydney").getRules().getOffset(now).getTotalSeconds();
int offsetGMT = 0; // aka ZoneOffset.UTC.getTotalSeconds()
//                 // aka ZoneId.of("GMT").getRules().getOffset(now).getTotalSeconds()

long diffGMT = offsetLocal - offsetGMT;
long diffAEST = offsetLocal - offsetSydney;
System.out.println("diffGMT = " + diffGMT);
System.out.println("diffAEST = " + diffAEST);

long hours = (diffAEST / 3600);
long mins = (diffAEST / 60) % 60;
long secs = diffAEST % 60;
System.out.printf("diffAEST = %02d:%02d:%02d%n", hours, Math.abs(mins), Math.abs(secs));

To test that running with system time IST, add this line first, if needed:

TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));

Output

Tue Sep 15 19:44:36 IST 2020
diffGMT = 19800
diffAEST = -16200
diffAEST = -4:30:00
Andreas
  • 154,647
  • 11
  • 152
  • 247