5

I found some similar questions, such as:

How to get the timezone offset in GMT(Like GMT+7:00) from android device?

How to find out GMT offset value in android

But all these answers(+12:00) are incorrect for New Zealand Daylight Saving Time now.

When I did debug, I got this from Google Calendar event object:

"dateTime" -> "2016-11-06T10:00:00.000+13:00"

So how to get the correct offset which should be +13:00?

Thanks.

Community
  • 1
  • 1
J.W
  • 671
  • 7
  • 23

3 Answers3

9

To get the current offset from UTC in milliseconds (which can vary according to DST):

return TimeZone.getDefault().getOffset(System.currentTimeMillis());

To get a RFC 822 timezone String instead, you can simply create a SimpleDateFormat instance:

return new SimpleDateFormat("Z").format(new Date());

The format is (+/-)HHMM

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
0

So, I tried to get gmt offset through Calendar and SimpleDateFormat but both returns 0. I found the solution using deprecated methods in Date class. So, this code works for me.

private double getOffset() {                                       
    Calendar calendar = Calendar.getInstance(Locale.ENGLISH);      
    int defHour = calendar.get(Calendar.HOUR_OF_DAY);              
    int defMinute = calendar.get(Calendar.MINUTE) + (defHour * 60);
    Date date = new Date(System.currentTimeMillis());              
    int curHour = date.getHours();                                 
    int curMinute = date.getMinutes() + (curHour * 60);            
    double offset = ((double) curMinute - defMinute) / 60;         
    return offset > 12? -24 + offset : offset;                     
}                                             

Then you can format a result

pacholik
  • 8,607
  • 9
  • 43
  • 55
Up.Reckless
  • 106
  • 1
  • 9
0

This code return me GMT offset.

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
Date currentLocalTime = calendar.getTime();
DateFormat date = new SimpleDateFormat("Z");
String localTime = date.format(currentLocalTime);

It returns the time zone offset like this: +0530

Sebastian
  • 27
  • 11