1

I have a method which takes the argument of two time strings Eg: t1 - 09:00 and t2 - 15:00 and a Date argument.

I want to achieve to a solution where the given time from date is with or within the passed timeframe or not.

Thanks in advance

public class CheckTime {
    private boolean isOpen;

    public boolean isValid(String time1, String time2, Date date) {
        ----Logic goes here------
        return isOpen;
    }

    public static void main(String[] args) {
        new CheckTime().isValid("09:00","15:00",
                            new Date(System.currentTimeMillis()));
    }
}
  • 1
    Convert the time values to `LocalTime` and use those to compare with the `Date` value (which I'd convert to a `LocalDateTime` object) - You've actually asked about 2 questions - convert `String` to time value and compare said value with a `Date` object - I'd focus on figuring out how to achieve one of those things – MadProgrammer Jul 26 '17 at 04:24
  • Most of the Date methods are deprecated according to the Oracle documentation for both [SE 7](https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html) and [SE 8](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html). As such, [Oracle recommends using Calendar and its methods instead](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#getHours--). –  Jul 26 '17 at 04:40
  • @hwdbc Since Java 8 - It would be HIGHLY recommend to make use of the new Time API - because `Calendar` is a ... good example of good intentions done bad – MadProgrammer Jul 26 '17 at 04:43
  • @MadProgrammer Do you have some evidence for your statement? At the very least, I have no idea which API you're referring to. Also, looking [at this documentation](https://docs.oracle.com/javase/7/docs/api/java/sql/Time.html) I see mostly deprecated methods. –  Jul 26 '17 at 04:48
  • 1
    @hwdbc Well, [there's one](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html), [there's another](https://docs.oracle.com/javase/tutorial/datetime/), [there's another](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html), [there's another](http://javarevisited.blogspot.com.au/2017/04/5-reasons-why-javas-old-date-and-Calendar-API-bad.html) and if you need [more evidence](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/) – MadProgrammer Jul 26 '17 at 04:49
  • @MadProgrammer Thanks for clarifying. I'm of the opinion that it's more helpful for our OP to see answers that reference sources. We both know there are multiple ways to handle date and time, but other readers may not. And yeah, I think I have to agree with you at this point. Calendar is not what they should be getting involved with. –  Jul 26 '17 at 04:53
  • Great to hear that it is a easy solution using Java 8, but unfortunately my requirement to run this logic is with java 7 :( – Harish ThulasiRam Jul 26 '17 at 07:23

1 Answers1

0

The first thing I would do is get the values into some format that can easily be compared. To my mind, that's using Java 8's Time API (or JodaTime or such API if you're still using an earlier version of Java)

Date now = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());

String startValue = "09:00";
LocalTime startTime = LocalTime.parse(startValue, DateTimeFormatter.ISO_LOCAL_TIME);

From this, it becomes a simple matter of comparing the values

System.out.println(ldt.toLocalTime().equals(startTime) ||
                                     ldt.toLocalTime().isAfter(startTime));

I'll leave it to you to figure out the "before" comparison ;)

Ekaba Bisong
  • 2,918
  • 2
  • 23
  • 38
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Or `!ldt.toLocalTime().isBefore(startTime)` – shmosel Jul 26 '17 at 04:37
  • @shmosel I'm assuming that there is an additional comparison which needs to be made against the "end time", although in this example, I've done a inclusive check, you'd only have to drop the `equals` to make exclusive of the `startTime` – MadProgrammer Jul 26 '17 at 04:39
  • @shmosel Tired - I'm assuming `startTime > now && endTime < now` - so it would need to be a range check – MadProgrammer Jul 26 '17 at 04:40
  • Great to hear that it is a easy solution using Java 8, but unfortunately my requirement to run this logic is with java 7 :( – Harish ThulasiRam Jul 26 '17 at 07:23
  • @HarishThulasiRam There are two libraries, JodaTime, which is the precursor to the Time API and a backport of the current Time API for earlier versions of Java - use either, seriously, you’ll be so much happier – MadProgrammer Jul 26 '17 at 07:30