1

In this code show wrong time in case of 12:01:00 it shows 12:00 AM but right time is 12:00 PM:

private static final String sourceFormat = "hh:mm:ss";
private static final String targetFormat = "hh:mm a";

public static String convertTimeFormat(String dateStr) {

    if (dateStr.equals("")) {
        return "";
    }
    Log.d("date", dateStr + "---" + sourceFormat + "---" + targetFormat);
    SimpleDateFormat form = new SimpleDateFormat(sourceFormat);
    Date date = null;
    try {
        date = form.parse(dateStr);
    } catch (Exception e) {
        e.printStackTrace();
    }
    SimpleDateFormat postFormater = new SimpleDateFormat(targetFormat);
    String newDateStr = postFormater.format(date);
    Log.d("Lead Response", newDateStr);
    return newDateStr;
}

Thank you very much for your time and assistance in this matter.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Akshay Barodia
  • 486
  • 4
  • 11

1 Answers1

2
private static final String sourceFormat = "hh:mm:ss";

change to

private static final String sourceFormat = "HH:mm:ss";

It's working I checked in My IDE

private static final String sourceFormat = "HH:mm:ss";
    private static final String targetFormat = "hh:mm a";

public static String convertTimeFormat(String dateStr)  {

        if (dateStr.equals("")) {
            return "";
        }
        Log.d("date", dateStr + "---" + sourceFormat + "---" + targetFormat);

        **SimpleDateFormat form = new SimpleDateFormat(sourceFormat, Locale.US);**


        Date date = null;
        try {
            date = form.parse(dateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        **SimpleDateFormat postFormater = new SimpleDateFormat(targetFormat, Locale.US);**

        String newDateStr = postFormater.format(date);
       Log.d("Lead Response", newDateStr);
        return newDateStr;
    }
Parth Suthar
  • 123
  • 4
Venki WAR
  • 1,997
  • 4
  • 25
  • 38