-3

I am trying to compare my string time in format of (HH:SS)

String timestart= 08:00 String timeend= 21:00

I am trying to build my code and logic but I am not sure whether my comparison of time is correct I wanted to check whether this two time strings are under the condition of timestart will be greater than or equal 06:00 am and timeend less than or equal 22:00

code

int endday = Integer.parseInt(time_to); 
int startday = Integer.parseInt(time_start);

if ((startday >= '06:00') && (startday  <= '22:00')) &&
((endday  >= '06:00') && (endday  <= '22:00'))  {
System.out.print("passed");

}else{

System.out.print("no");

}
skomisa
  • 16,436
  • 7
  • 61
  • 102
ROD BAL
  • 31
  • 5
  • 1
    `Time` is not an `int`. `parseInt` is wrong here. – Johannes Kuhn Mar 22 '19 at 02:37
  • 1
    Additionally, single quotation marks are exclusively for `Characters`. Not `String`s. (Error). There's more. Even if that were a String (can't possibly be an integer with the `:` char) you can't compare `Integers` to `Strings` like that. (Error) Please try compiling your code and look for compiling errors. – FailingCoder Mar 22 '19 at 02:40
  • 1
    Possible duplicate of [Time comparison](https://stackoverflow.com/questions/2309558/time-comparison) – vs97 Mar 22 '19 at 02:41
  • 3
    It's not really, his actual problem is the compiling errors. There are... 6. – FailingCoder Mar 22 '19 at 02:44
  • Agree, @FailingCoder. It’s still a very helpful link. – Ole V.V. Mar 22 '19 at 10:41

2 Answers2

1

You can use java 8 Date/Time api,

`DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
String time_start = "08:00";
String time_to = "22:00";
if (LocalTime.parse(time_start, formatter).
                    isAfter(LocalTime.parse("06:00", formatter)) && 
                    LocalTime.parse("21:00", formatter).
                    isBefore(LocalTime.parse(time_to, formatter)))  {
    System.out.print("passed");
}else{
    System.out.print("no");
}`

Output passed

readmore

Vimukthi
  • 846
  • 6
  • 19
1

The answer by Vimukthi_R is helpful in showing you the right library class and methods to use and is almost correct. So my version is similar, but I would like to show you a few twists.

    LocalTime minTime = LocalTime.of(6, 0);
    LocalTime maxTime = LocalTime.of(22, 0);

    String timeStart = "08:00";
    String timeEnd = "21:00";
    LocalTime startDay = LocalTime.parse(timeStart);
    LocalTime endDay = LocalTime.parse(timeEnd);

    if (startDay.isBefore(minTime)
            || endDay.isBefore(startDay)
            || endDay.isAfter(maxTime)) {
        System.out.print("no");
    } else {
        System.out.print("passed");
    }

Since you want to accept start and end times in the interval frmo 6 to 22 inclusive, isBefore and isAfter don’t really give you the exact answer. We would have needed methods isBeforeOrOn and isOnOrAfter, but they don’t exist. Rather than writing auxiliary methods for that purpose, I am turning the whole logic upside-down: if the start time is before 6 or the end time after 22, the validation fails.

For defining 6 and 22 as LocalTime objects parsing strings works, but my taste is more for the of method (please make your own pick).

For parsing we don’t need a formatter since your strings are in the standard ISO 8601 format that LocalTime parses as its default. One may of course prefer the formatter since it makes the format explicit in the code. Again pick your own preference.

I was assuming that end time could not be before start time, which simplified the condition to three sub-conditions instead of four. If you didn’t want this, I am trusting you to write out the four subconditions.

I am not sure whether my comparison of time is correct

No, your comparison was not correct. In Java you can use >= and <= for comparing numbers, but not strings or or other objects like LocalTime. Also, as Johannes Kuhn said in a comment, you cannot parse a string like 08:00 into an integer. FallingCoder said the rest in a comment:

Additionally, single quotation marks are exclusively for Characters. Not Strings.

Link: Oracle tutorial: Date Time explaining how to use java.time.

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