Getting current timezone in android application

How to get the current time zone in an Android app? I tried using this

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

But I do not get a time zone from him. How to display the time zone?

+42
java android
Feb 28 '12 at 13:26
source share
4 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() 
+81
Feb 28 2018-12-12T00:
source share
 String timezoneID = TimeZone.getDefault().getID(); System.out.println(timezoneID); 

My console printed Asia/Calcutta

And any date format I set it as ....

 SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy"); sdf2.setTimeZone(TimeZone.getTimeZone(timezoneID)); 
+21
Feb 28 '12 at 13:29
source share

I needed an offset that not only included the daylight, but also as a number. Here is the code I used if someone is looking for an example.

I get a response from "11" that I expect in NSW, Australia in the summer. I also needed it as a string, so I could send it to the server so you don't need the last line.

 TimeZone tz = TimeZone.getDefault(); Date now = new Date(); int offsetFromUtc = tz.getOffset(now.getTime()) / 3600000; String m2tTimeZoneIs = Integer.toString(offsetFromUtc); 
+9
Nov 20 '13 at 1:10
source share

To display the current time, you must execute the code snippet below in the user interface thread. This worked for me:

 Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { runOnUiThread(new Runnable(){ public void run() { dateTxt.setText(new Date().toString()); } }); } }, 0, 1000); 
-four
Feb 28 2018-12-12T00:
source share



All Articles