How to find out GMT offset value in android

霸气de小男生 提交于 2019-12-06 01:06:35

This is what works awesome

   public double getOffset(){
        TimeZone timezone = TimeZone.getDefault();
        int seconds = timezone.getOffset(Calendar.ZONE_OFFSET)/1000;
        double minutes = seconds/60;
        double hours = minutes/60;
        return hours;
  }

First get the epoch system time

System.currentTimeMillis()

Then use a date object, set the time zone to GMT and initialize with the long valye

dateObj.setTimeZone(TimeZone.getTimeZone("GMT"))

To get time in GMT use below function where dateInString is the value of date,and format is date format as yyyyMMddHH

   public static long getDate(String dateInString,String format){
       long date = 0;
       SimpleDateFormat dateFormat = new SimpleDateFormat(format);

       try {
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date d = dateFormat.parse(dateInString);
        date = d.getTime();
       } catch (ParseException e) {
        e.printStackTrace();
       }

       return date;
   }
duggu

Use below method to get UTC :-

public int GetUnixTime()
{
    Calendar calendar = Calendar.getInstance();
    long now = calendar.getTimeInMillis();
    int utc = (int)(now / 1000);
    return (utc);

}

after you get UTC now compared it to the Epoch in this site http://www.xav.com/time.cgi.

see this below link :-

How can I get the current date and time in UTC or GMT in Java?

If you store a map between timezones and their GMT offsets in your app, you can call TimeZone.getDefault() to get the device's timezone and do a quick lookup to return the GMT offset. That way you don't have to rely on potentially tricky date/time calculations and can be sure you have the correct value.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!