So, I'm using SimpleDateFormat to convert a String which is "HH:MM AA" to "HH:MM", The problem is my code works fine on eclipse but when I run this on Android Studio it shows wrong output. Here are my both codes.


So, I'm using SimpleDateFormat to convert a String which is "HH:MM AA" to "HH:MM", The problem is my code works fine on eclipse but when I run this on Android Studio it shows wrong output. Here are my both codes.


 
    
     
    
    Try this code..
 String date="2:30 PM";
    SimpleDateFormat sdf5 = new SimpleDateFormat("hh:mm aa");
    SimpleDateFormat sdf6 = new SimpleDateFormat("HH:mm");
    try {
        String str=sdf6.format(sdf5.parse(date));
        Log.d("DATE TO:",str);
    } catch (ParseException e) {
        e.printStackTrace();
    }
The problem is you are using "MM" as minutes but it should be "mm". "MM" is for months.
The part "HH" is fine when you want the 24 hour values back.
try soemthing like this:
public static String getTimeFormatted(String time){
    String s = "";
    try{
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa", Locale.US);
        Date d = sdf.parse(time);
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.US);
        s = formatter.format(d);
    }
    catch(Exception ex){
        s = ex.getMessage();
    }
    return s;
}
 
    
    try this
  public String changeDateFormatFromAnother(String date){
        @SuppressLint("SimpleDateFormat") DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        @SuppressLint("SimpleDateFormat") DateFormat outputFormat = new SimpleDateFormat("dd MMMM yyyy");
        String resultDate = "";
        try {
            resultDate=outputFormat.format(inputFormat.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultDate;
    }
 
    
        String newTimeString = LocalTime
            .parse("2:30 PM", DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH))
            .toString();
    System.out.println(newTimeString);
This prints
14:30
Even on Android you should consider not fighting with the age-old and notoriously troublesome SimpleDateFormat class. java.time, the modern Java date and time API, is much nicer to work with. A LocalTime is a time of day without date and without time zone, so seems to match your requirements very neatly.
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
org.threeten.bp with subpackages.HH:mm. Uppercase HH for hour of day and lowercase mm for
minute of hour. So you get the expected 14:30.HH:MM, that is, uppercase MM. This is for month. Since the string you parsed didn’t have a month in it, it defaulted to January, which in turn was rendered as 01 in your result, so you got 14:01. It’s very typical for SimpleDateFormat that it agrees to print a month that was never there, just pretends that everything is fine, doesn’t inform you of an error. This is just one of the many reasons I recommend you avoid that class.java.time.java.time was first described.java.timeto Java 6 and 7 (ThreeTen for JSR-310).