Comparing time is incorrect when picking 12:00

人盡茶涼 提交于 2019-12-17 03:21:38

问题


I am creating a booking system and I don't want to allow users to book with starting time 11:00 and end time 09:00 (past)(I am using 24hour clock). I have two combo boxes filled with Strings that act as start and end time (09:00,10:00,11:00,12:00,13:00....)

I have this code:

 String start = (String) startTime.getSelectedItem();
        String end = (String) endTime.getSelectedItem();
        try {
            if(new SimpleDateFormat("hh:mm").parse(start).before(new SimpleDateFormat("hh:mm").parse(end))){
                System.out.println("test1");// future date - good

            }else{
                System.out.println("fail2");// old date - bad
            }
        } catch (ParseException ex) {
                System.out.println("error");
        }

This works perfectly except when I pick start/end time to be 12:00. Program outputs opposite of what it is supposed to output and I am unsure why.

If I pick start time 14:00 and end time 12:00 the program will output fail2(good output),

If I pick start time 09:00 and end time 12:00 the program will output fail2(should be test1),

if I pick start time 12:00 and end time 10:00 the program will output test1(should be fail2),

if I pick start time 12:00 and end time 15:00 the program will output test1(good output)

This type of problem only occurs when I pick 12:00..


回答1:


public static void checkTimes(String start, String end) {
    try {
        if (LocalTime.parse(start).isBefore(LocalTime.parse(end))) {
            System.out.println("test1");// future date - good
        } else {
            System.out.println("fail2");// old date - bad
        }
    } catch (DateTimeParseException dtpe) {
        System.out.println("error");
    }
}

Let’s try it:

    checkTimes("14:00", "12:00");
    checkTimes("09:00", "12:00");
    checkTimes("12:00", "10:00");
    checkTimes("12:00", "15:00");

This prints:

fail2
test1
fail2
test1

I believe this agrees with what you had intended. Note that LocalTime parses your strings without the need for an explicit formatter. Furthermore, if you trust that your combobox only contains valid time strings, you can leave out the try-catch construct since DateTimeParseException is an unchecked exception.

If startTime and endTime are JComboBox, I believe you can even fill LocalTime objects into them. Then you don’t need to parse when the user selects one from each. Your JComboBox will call LocalTime.toString(), which will return a string like 09:00, which in turn the combo box will display and let the user select.

    LocalTime[] times = { LocalTime.of(9, 0), LocalTime.of(10, 0), LocalTime.of(11, 0), 
                          LocalTime.of(12, 0), LocalTime.of(13, 0), LocalTime.of(14, 0) };
    JComboBox<LocalTime> combo = new JComboBox<>(times);

Unfolded:

I am using LocalTime from java.time, the modern Java date and time API. java.time is generally much nicer to work with than the old and outdated date and time classes like SimpleDateFormat, Date and more.

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




回答2:


You should use HH:mm.

hh ranges 01-12, while HH ranges 1-23.

Update

Check the doc of SimpleDateFormat:

H: Hour in day (0-23)

h: Hour in am/pm (1-12)




回答3:


String start = (String) startTime.getSelectedItem();
        String end = (String) endTime.getSelectedItem();
        SimpleDateFormat timer = new SimpleDateFormat("HH:mm");

        try {
            Date startD = timer.parse(start);
            Date endD = timer.parse(end);
            if(startD.before(endD)){
                System.out.println("test1");// future date - good
            }else{
                System.out.println("fail2");// old date - bad
            }
        } catch (ParseException ex) {
                System.out.println("error");
    }

Changing my code to this fixed this issue..



来源:https://stackoverflow.com/questions/50042873/comparing-time-is-incorrect-when-picking-1200

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!