-4

I am making an ajax call and getting a JSON response. In the response I am getting time in following format:

12:00 pm //typeof of this is String.

I need to compare this time with the other times returned and find the earliest time.

What would be the best way of doing this? I was thinking of converting it to 24 Hour format in int and then directly compare, but how can we convert this to 24 hour format in the best way?

user2696258
  • 1,159
  • 2
  • 14
  • 26
  • cut off `am/pm` part, split remaining string into `hours/minutes`, if PM add 12 to `hours`, do `hours*60+minutes`, compare (don't forget 24=00)... that's it – Wh1T3h4Ck5 Jul 27 '17 at 11:07

1 Answers1

-2

Try this:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
   public static void main(String [] args) throws Exception {
       SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
       SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
       Date date = parseFormat.parse("12:00 PM");
       System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
   }
}

That is what it prints:

12:00 PM = 00:00
Fotis Grigorakis
  • 363
  • 1
  • 3
  • 16