0

I am trying to use Simple date Format for change the data, but am getting the parse exception

java.text.ParseException: Unparseable date: "11/08/2016 02:00:00 PM" (at offset 20)
at java.text.DateFormat.parse(DateFormat.java:579)

My input date is "11/08/2016 02:00:00 PM"

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa",Locale.getDefault());
try
{
  expiryDate = df.parse(mPayRespo.get(position).getVoucherExpDate());
}
catch(ParseException pe){
             pe.printStackTrace();
}

In my settings Date and time is set as Automatic. Am getting this issue only in SAMASUNG S6

Can any one please help me

Binil Surendran
  • 2,524
  • 6
  • 35
  • 58

3 Answers3

1

Try this code Convert 12 Hour date/time format to 24 hour date/time format

String input = "23/12/2014 10:22:12 PM";
  //Format of the date defined in the input String
  DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");
  //Desired format: 24 hour format: Change the pattern as per the need
  DateFormat outputformat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
  Date date = null;
  String output = null;
  try{
     //Converting the input String to Date
     date= df.parse(input);
     //Changing the format of date and storing it in String
     output = outputformat.format(date);
     //Displaying the date
     System.out.println(output);
  }catch(ParseException pe){
     pe.printStackTrace();
   }

output

12-23-2014 22:22:12

this should work.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
1

The date time parse exception arise in Samsung device as their 12-hour format is a.m./p.m. instead of AM/PM. Try the workaround solution that I found:

 public Date getDateFormatCrntDateTime(String date) throws ParseException {
        String AMPM=date.substring(19);
        String hour=date.substring(11,13);
        Log.e(TAG,"AM PM is: "+AMPM);
        Log.e(TAG," Hour is: "+hour);

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH : mm");
    Date currentDate=df.parse(date);
    Log.w(TAG,"date of device before format is: "+currentDate);

    if (AMPM.equalsIgnoreCase("AM")) {
        if (hour.equalsIgnoreCase("12")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(currentDate);
            calendar.add(Calendar.HOUR_OF_DAY, -12);
            currentDate = calendar.getTime();
        }
    }
    if (AMPM.equalsIgnoreCase("PM")){
        if (hour.equalsIgnoreCase("12")){
        }
        else{
            Calendar cal=Calendar.getInstance();
            cal.setTime(currentDate);
            cal.add(Calendar.HOUR_OF_DAY,12);
            currentDate=cal.getTime();
        }

    }
    Log.e(TAG,"Date of device is: "+currentDate);
    return currentDate;
}

Change the Simple date format and substrings accordingly

Tarun Kumar
  • 498
  • 5
  • 16
0

it should be

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a",Locale.getDefault());

notice a instead of aa

Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69