2

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.

@Override
 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
    .is24HourFormat(getActivity());

  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?

Lakshmanan
  • 1,671
  • 2
  • 26
  • 42
  • just replace(".","") in your time string – Sathish Kumar J Jun 07 '16 at 10:38
  • Have you managed to solve this? I'm having the same issue but it only happens on Nexus 5X – Vincent Paing Jun 15 '16 at 06:17
  • @Vincent_Paing, yes we have resolved it by following. Please do this, Replace , with empty fragment.onTimeSelected(selectedTime.replace("\\.",""), inputField); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss",Locale.ENGLISH); – Lakshmanan Jun 15 '16 at 14:46

2 Answers2

1

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);
Lakshmanan
  • 1,671
  • 2
  • 26
  • 42
0

Change

fragment.onTimeSelected(selectedTime, inputField);

to

fragment.onTimeSelected(selectedTime.replace(".",""), inputField);
dave
  • 1,410
  • 1
  • 16
  • 42