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;