Lakshmanan Lakshmanan - 1 year ago 74
Android Question

Android Time Format issue - Its coming as A.M / P.M

I am getting issue when getting the Date Time format from the Moto X and Nexus 5X devices
Below is the code I am using.

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

SimpleDateFormat mSDF = null;
final String format = Settings.System.getString(fragment.getActivity()
.getContentResolver(), Settings.System.TIME_12_24);
boolean is24HourFormat = android.text.format.DateFormat

mSDF = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);

System.out.println("Time Format:" + format);
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);

String selectedTime = mSDF.format(mCalendar.getTime());

fragment.onTimeSelected(selectedTime, inputField);

On the selectedTime i am getting the time format like 10:40 A.M or P.M instead of AM or PM.

Which is making issue when i pass this to API, As if we sending like this it will cause issue in date formaat conversion in other mobile which has normal format.

So is there any way to get the Timeformat in AM , PM instead of A.M or P.M in above mentioned device?

Answer Source

I have resolve it by following way.

Replace , with empty

fragment.onTimeSelected(selectedTime.replace("\\.",""), inputField);

and during the Conversion, Please specify locale.

SimpleDateFormat dateFormat = new SimpleDateFormat(
       "yyyy-MM-dd HH:mm:ss",Locale.ENGLISH);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download