3

How to find out the values of GMT for user for example it is +05:30 for India. How do calculate this +05:30 value in Android ?

I need this because I am using a java library in my app which has a function with this +05:30 field and I want to generate this field by calculation so that I wont have to fill up individual values for countries.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
user3671032
  • 211
  • 2
  • 16
  • Use calendar.getTimeInMillis(); returns the UTC time and compare it with GMT using Epoch http://www.xav.com/time.cgi – Moonhead Jun 23 '14 at 05:22
  • I am looking for an offline solution,if its possible – user3671032 Jun 23 '14 at 05:27
  • There is no +5.5 for [time zone offsets](http://en.wikipedia.org/wiki/List_of_tz_database_time_zones) that I know of. Offsets are hours + colon + minutes, using double-digits with a padding zero if needed. India time is `+05:30` from UTC. Ignoring the padding zero will break various date-time libraries and interoperability. – Basil Bourque Jun 23 '14 at 05:36
  • possible duplicate of [java joda-time get date time](http://stackoverflow.com/questions/21952989/java-joda-time-get-date-time) and many others. Search for "Asia/Kolkata" or "joda DateTimeZone". – Basil Bourque Jun 23 '14 at 05:42

5 Answers5

8

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;
  }
user3671032
  • 211
  • 2
  • 16
0

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"))
Aadi Droid
  • 1,689
  • 4
  • 22
  • 46
0

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;
   }
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

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?

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

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.

Eric Brynsvold
  • 2,880
  • 3
  • 17
  • 22