Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current time zone in android application [duplicate]

Tags:

java

android

How can I get the current time zone in my Android application? I tried to use this

Calendar cal = Calendar.getInstance( userConfig.locale); TimeZone tz = cal.getTimeZone();    

But I am not getting the timezone from it. How can I display the timezone?

like image 270
Ashish Augustine Avatar asked Feb 28 '12 13:02

Ashish Augustine


People also ask

How do I get time zone on my Android?

According to http://developer.android.com/reference/android/text/format/Time.html you should be using Time. getCurrentTimezone() to retrieve the current timezone of the device.

How do I change the TimeZone in android programmatically?

Call TimeZone. getAvailableIDs() to get the list of known timezones for your system. This permission is only allowed for system apps. on android api 26 and 27 receive this error: setTimeZone: Neither user 10087 nor current process has android.

How can I find the difference between two time in Android?

I need to Calculate the TotalTime by subtracting the EndTime with StartTime. The Format of StartTime and EndTime is as like follows: StartTime = "08:00 AM"; EndTime = "04:00 PM"; TotalTime in Hours and Mins Format.


2 Answers

Use this

Calendar cal = Calendar.getInstance(); TimeZone tz = cal.getTimeZone(); Log.d("Time zone","="+tz.getDisplayName()); 

or you can also use the java.util.TimeZone class

TimeZone.getDefault().getDisplayName() 
like image 159
John Avatar answered Sep 20 '22 01:09

John


String timezoneID = TimeZone.getDefault().getID(); System.out.println(timezoneID); 

In my Console, it prints Asia/Calcutta

And any Date Format, I set it Like....

SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy"); sdf2.setTimeZone(TimeZone.getTimeZone(timezoneID)); 
like image 38
Samir Mangroliya Avatar answered Sep 24 '22 01:09

Samir Mangroliya