-3

I'm trying to compare two different times to see which comes before the other, but I'm having trouble coming up with an else if statement that compares their suffixes (am and pm) and outputs the order of the first time relative to the other time.

EDIT: the date type of suffix1 and suffix2 are strings, the data type of time1 and time2 are int

So far, I have this piece of code that checks if the periods are equal:

if (suffix1.equals(suffix2))
  {
       if (time1 > time2)
       {
          System.out.print("After");
       }
       .....
  }

is there a way to see which times' suffix comes before the other?

JURO312
  • 69
  • 5

2 Answers2

1

I would think you’re after something like the following:

    String timeString1 = "2:00 PM";
    String timeString2 = "9:00 AM";

    DateTimeFormatter timeFormatParser 
            = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
    LocalTime time1 = LocalTime.parse(timeString1, timeFormatParser);
    LocalTime time2 = LocalTime.parse(timeString2, timeFormatParser);
    if (time1.isAfter(time2)) {
        System.out.println("After");
    }

With the strings in my example the code does print After. If your original time strings have a different format, I hope you can modify the format pattern string accordingly, otherwise follow up in a comment. Or maybe better, edit your question to specify your strings precisely. The letters you can use are documented here. Please be aware that the format pattern is case sensitive.

My DateTimeFormatter expects AM or PM in uppercase. If your strings have them in lowercase, there are a couple of options:

  1. The simple: timeString1 = timeString1.toUpperCase(Locale.ENGLISH); and similarly for timeString2.
  2. The more general but also more complex: Use a DateTimeFormatterBuilder to build a non-case-sensitive DateTimeFormatter.

PS Before the modern Java date and time API that I am using came out in 2014, one would often use a class called SimpleDateFormat to parse into a Date object. Nowadays stay away from that. The newer classes have shown to be considerably nicer to work with and more programmer friendly.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Use Date to represent time in application: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

The string representation, should be used only when displaying the time. When you use dates you can compare them like this myDate.compareTo(anotherDate).

You can print the date in whichever form you want, here is how: Change date format in a Java string

MForm
  • 237
  • 2
  • 4
  • 2
    This was the correct answer in, like, 1998. There are now far better ways of representing time than the `Date` class. – Dawood ibn Kareem Jul 07 '17 at 00:26
  • 1
    The `Date` class is, well, dated. Both `Date` and `Calendar` are now legacy, supplanted by the java.time classes. Avoid this legacy classes as they are confusing, poorly designed, and flawed. They've been replaced for a reason, actually *many* good reasons. – Basil Bourque Jul 07 '17 at 04:02