Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain user date time settings in android

I have a Calendar object, and i know how to format it using android.text.format() How can i get the phone settings for date and time to format the object to format set by the user. Like 24 hour clock , mmm dd yyyy...

like image 551
Deepak Avatar asked Feb 02 '26 15:02

Deepak


1 Answers

What I wanted was to provide the date and time formatted according to the user's preferences and locale, in separate TextViews.

I have done this using static methods in the DateFormat class:

DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); // Gets system date format
DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(getApplicationContext()); // Gets system time format

txt1.setText(dateFormat.format(CalendarInstance.getTime())); // format() Returns a string according to the user's preferences (12-hr vs 24-hr) and locale
txt2.setText(timeFormat.format(CalendarInstance.getTime()));
like image 183
Deepak Avatar answered Feb 05 '26 05:02

Deepak