3

India Standard Time (IST) is 5:30 hours (5 hours 30 minutes) ahead of Greenwich Mean Time. We show it as GMT+5.5.

Is there any way to find this value (in above case, value = 5.5) by time zone?

In my android app, I can get device time zone. But how can I get this value using device time zone?

Bishan
  • 15,211
  • 52
  • 164
  • 258
  • Tip: Using decimal fractions for spans of time does not work well. I suggest you learn about the `ZoneOffset`, `ZoneId` for offset and time zone work, and learn about `Duration`, and `Period` classes for working with spans-of-time. – Basil Bourque May 17 '21 at 00:07

6 Answers6

2
double timezoneval = 0.0;
        Calendar cal = Calendar.getInstance();
        cal.set(mn.Year(), mn.Month(), mn.Day(), mn.Hour(), mn.minute(),
                (int) mn.second());
        TimeZone tz = TimeZone.getDefault();
        boolean t = tz.inDaylightTime(cal.getTime());
        if (t)
            timezoneval = ((TimeZone.getDefault().getRawOffset())
                    / (60 * 60 * 1000D) + (TimeZone.getDefault()
                    .getDSTSavings() / (60 * 60 * 1000D)));
        else
            timezoneval = ((TimeZone.getDefault().getRawOffset()) / (60 * 60 * 1000D));

You can try this code. This will give you the value of time zone. If you want different timezone value; just change TimeZone tz = TimeZone.getDefault(); to

TimeZone tz = TimeZone.getTimeZone("Asia/Kolkata");
        TimeZone.setDefault(tz);

It will work for you.

Kumar Shorav
  • 531
  • 4
  • 16
  • "Asia/Kolkata" can be replaced by different valid timezone string. – Kumar Shorav Mar 21 '13 at 09:41
  • can i use `tz.getRawOffset()` instead of `TimeZone.getDefault().getRawOffset()` ? – Bishan Mar 21 '13 at 10:43
  • what is `mn` in your code ? `cal.set(mn.Year(), mn.Month(), mn.Day(), mn.Hour(), mn.minute(), (int) mn.second());` – Bishan Mar 21 '13 at 10:52
  • sorry...you can just set calender ; dats all....mn is object of my own implemented class. That is not for your use. Sorry for not to mention.Thanks – Kumar Shorav Mar 21 '13 at 11:20
  • It is safer to use TimeZone.getDefault().getRawOffset() if you have to work with other timezone very frequently. at the same time you have to set TimeZone tz = TimeZone.getTimeZone("Asia/Kolkata"); TimeZone.setDefault(tz); . Other wise you can use tz.getRawOffset(); – Kumar Shorav Mar 21 '13 at 11:22
1

Check the Timezone class. The raw offset is what you want, in millisecond. You can easily convert it to value in hours.

long rawOffset = TimeZone.getDefault().getRawOffset();

See http://developer.android.com/reference/java/util/TimeZone.html#getRawOffset()

Frank Du
  • 516
  • 4
  • 12
1

tl;dr

how can a get this value using device time zone

ZonedDateTime.
now(
    ZoneId.systemDefault()  // Or: ZoneId.of("Asia/Kolkata") 
)
.getOffset()
.toString()

See this code run live at IdeOne.com.

+05:30

java.time

Solution using java.time, the modern API:

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
        int seconds = zdt.getOffset().getTotalSeconds();
        System.out.println(seconds);

        // If required, convert it into hours
        double hours = seconds / 3600.0;
        System.out.println(hours);
    }
}

Output:

19800
5.5

Learn more about java.time, the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Try:

TimeZone.getDefault();

getDefault() returns the timezone details of the current device. You can then call getOffset() to get the difference between UTC and this time zone.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
0

try this code

 public class Sample {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        String dateStr = "2010-06-14 02:21:49-0400";
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
        TimeZone tz = TimeZone.getDefault();
        sdf.setTimeZone(tz);
        Date date = sdf.parse(dateStr);

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
        String newDateStr = sdf.format(date);

        System.out.println(newDateStr);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

output should like this

2010-06-14 08:21:49 AM

-1

This is actually what i need.

double rawOffset = TimeZone.getDefault().getRawOffset();

rawOffset = rawOffset / (60 * 60 * 1000);

System.out.println("rawOffset : " + rawOffset);
Bishan
  • 15,211
  • 52
  • 164
  • 258