Friday, May 13, 2016

Daylight Savings in Java : Find the Offset in GMT



The Code Snippet provides the Offset in GMT , When there is a 

Daylight savings for a respective time zone then the below code 

snippet provides the hours and minutes of how much the time 

should be offset in GMT Standard.

Code Snippet :

       public static String getCurrentTimezoneOffset(TimeZone timeZoneInstance) {
         Calendar _calenderInstance = GregorianCalendar.getInstance(timeZoneInstance);
         int offsetInMillis = timeZoneInstance.getOffset(_calenderInstance.getTimeInMillis());
         String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
         offset = (offsetInMillis >= 0 ? "+" : "-") + offset;
         return "GMT"+offset;

       }

No comments:

Post a Comment