-6

How do you compare "Friday 2:00 PM" against "Thursday 4:00 PM" in Java?

Input array: Friday 2:00 PM Thursday 2:00 AM Saturday 4:00 PM

Output: Thursday 2:00 AM Friday 2:00 PM Saturday 4:00 PM

Amit Garg
  • 541
  • 5
  • 12
  • 2
    Why do you put Thursday before Friday? What if they are on different weeks? – Iłya Bursov May 01 '17 at 15:59
  • This link should help you figure it out: https://www.google.ca/search?q=how+to+compare+day+of+the+week+and+time+in+java&oq=how+to+compare+day+of+the+week+and+time+in+java&aqs=chrome..69i57.5385j0j7&sourceid=chrome&ie=UTF-8 – Sebastien May 01 '17 at 16:00
  • 1
    Parse the `Strings` into either a `Calendar` object or `LocalDateTime` object then use `.compareTo()`... – brso05 May 01 '17 at 16:01
  • 4
    Start with the [Java Time API](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). Here's the [Oracle tutorial](https://docs.oracle.com/javase/tutorial/datetime/). Note that LocalDateTime instances are Comparable. – Andy Thomas May 01 '17 at 16:07
  • @Lashane - The input data is for just 1 week always, so that's why need to be sorted that way. – Amit Garg May 01 '17 at 16:33
  • Possible duplicate of [Time comparison](http://stackoverflow.com/questions/2309558/time-comparison) – Andrew Myers May 01 '17 at 20:11
  • Which day is the first day of your week? The international standard says the week begins on Monday, but not all countries observe it, – Ole V.V. May 02 '17 at 04:20
  • @AmitGarg Please take more care in drafting your post. As it reads now your Question does not make sense. And post further information as edits to the Question rather than as Comments. Do not make your readers work so hard as to trawl through the threads of comments to understand your issue. – Basil Bourque May 02 '17 at 05:30

1 Answers1

0

My solution would be to build a little class to represent the times. We could call it TimeOfWeek or WeeklyRecurringEvent or whatever fits your domain, please try to choose wisely. The class would have two instance fields, a DayOfWeek for the weekday and a LocalTime for the time. DayOfWeek and LocalTime are in java.time (introduced in Java 8 and backported to Java 6 and 7). My class would have a constructor accepting a String argument like "Friday 2:00 PM" and would parse it into the two instance fields. I believe the same DateTimeFormatter could be used for both, so this could be a class constant (private static final field). DayOfWeek does not have a parse method, so you would need something like DayOfWeek.from(yourDateTimeFormatter.parse(yourString)).

Your class should implement Comparable so you can sort your objects by their natural order. In your compareTo method compare day of week first, then time if day is the same. DayOfWeekare sorted naturally from Monday to Sunday; if your week begins on another day than Monday, you will need to handle it specially.

Your class could also have a ZoneId field for time zone, but assuming all your times are in the same time zone, there’s probably no reason.

You may also see if you can find a class that fits your requirements somewhere out there; use your search engine. I don’t know of one, but you can never tell.

The classes in java.time are very often good for funny requirements about day and time. You can obviously have a date without a time or a time without a date. You can have a day of the year without a year (good for remembering your anniversary) and as mentioned a day of week without a week, and more. Unfortunately there isn’t a class to match your requirement of day of week and time exactly, which is why I suggest putting it together from two of the existing classes.

Edit: For Android programmers, if you want to use the java.time classes on Android, get and use the ThreeTenABP (the classes were first described in JSR-310, so that’s ThreeTen for JSR-310 and ABP for Android Backport).

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161