Home > Mobile >  LocalTime does not parse 00:00 correctly
LocalTime does not parse 00:00 correctly

Time:10-31

I am trying to parse "Mon 00:00-23:59" using LocalTime.

LocalTime start = LocalTime.parse("00:00", DateTimeFormatter.ofPattern("HH:mm"));
LocalTime end = LocalTime.parse("23:59", DateTimeFormatter.ofPattern("HH:mm"));

But, start.isBefore(end) returns true, how is this possible?

I need to know if the start time is later than or equal to the end time.

CodePudding user response:

Your issue has nothing to do with parsing.

And, the behavior you see is a feature, not a bug.

LocalTime represent the time of day for a single imaginary generic 24-hours-long day. That day starts at 00:00:00.0. So all other values are after that zero time value.

So this should return true:

LocalTime.parse( "00:00" ).isBefore( LocalTime.parse( "23:59" ) )  // true.

Be aware that in our politically-defined timekeeping systems days are not necessarily 24-hours long, nor do they necessarily start at 00:00. Some days may be 23, 23.5, 25, or other number of hours long. And some dates in some time zones may start at a time such as 01:00.

By the way, notice there is no need to specify a DateTimeFormatter when the input complies with ISO 8601 standard for date-time textual values.

CodePudding user response:

Note that

  • The modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards. Your time strings are in ISO 8601 format.
  • The local time, 00:00 corresponds to 12:00 AM whereas 23:59 corresponds to 11:59 PM, it's obvious that 00:00 is before 23:59. So, the results that you have got is correct.

To avoid confusion, I recommend you attach a date with the local time i.e. use LocalDateTime.

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);

        LocalTime start = LocalTime.parse("00:00");
        LocalTime end = LocalTime.parse("23:59");

        System.out.println(start.format(dtf));
        System.out.println(end.format(dtf));

        LocalDate today = LocalDate.now();

        LocalDateTime ldtStart = start.atDate(today);
        LocalDateTime ldtEnd = end.atDate(today);
        System.out.println(ldtStart);
        System.out.println(ldtEnd);

        // Notice the result when you add a minute to ldtEnd
        System.out.println(ldtEnd.plusMinutes(1));
    }
}

Output:

12:00 AM
11:59 PM
2021-10-30T00:00
2021-10-30T23:59
2021-10-31T00:00

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8 APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time.

  • Related