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.