Home » Android » Android:Changing time format as per the Device's Current time format

Android:Changing time format as per the Device's Current time format

Posted by: admin June 15, 2020 Leave a comment

Questions:

How to get current time format of the android device? i.e. If the android device’s current time format is changed from 12hr to 24 hr format and vice versa, then how to check that setting programatically?

I want to change the time shown in my app widget to be of same format as the device’s and whenever the time format is changed then my widget should also show that time format. Any pointers will be helpful.

thanks.

How to&Answers:

You can use TIME_12_24.

int time_format = 0;
try {
    time_format = Settings.System.getInt(getContentResolver(), Settings.System.TIME_12_24);
} catch (SettingNotFoundException e) {
    e.printStackTrace();
}
Log.i(TAG,"Time format: "+time_format);

time_format will have 12 or 24 based on the settings.

Make sure you have added this permission to manifiest file.

<user-permission android:name="android.permission.WRITE_SETTINGS" />

EDIT :
You can also use DateFormat.is24HourFormat(). It doesn’t require an extra permission.

Answer:

check, http://developer.android.com/reference/android/text/format/DateFormat.html

DateFormat::getDateFormat()

Date date = new Date(location.getTime());
dateFormat.format(date);

Answer:

The simple way would be to use to use android native android.text.format.DateFormat class’s is24HourFormat(this) static method as below.

if (android.text.format.DateFormat.is24HourFormat(YourActivityName.this)) {
          // 24 hour format
}
else
{
    // 12 hour format
}

Answer:

Please check this…

http://developer.android.com/reference/java/text/SimpleDateFormat.html

How do you format date and time in Android?

24 hour clock not working in dateformat android