选择12:00时比较时间不正确 [英] Comparing time is incorrect when picking 12:00

查看:181
本文介绍了选择12:00时比较时间不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个预订系统,我不想让用户在开始时间11:00和结束时间09:00(过去)预订(我使用24小时制)。
我有两个充满字符串的组合框,作为开始和结束时间(09:00,10:00,11:00,12:00,13:00 ......)

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....)

我有这段代码:

 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");
        }

这完全有效,除非我选择开始/结束时间为12:00 。程序输出与输出的输出相反,我不确定原因。

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.

如果我选择开始时间14:00和结束时间12:00程序将输出fail2 (好的输出),

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

如果我选择开始时间09:00和结束时间12:00程序将输出fail2(应该是test1),

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

如果我选择开始时间12:00和结束时间10:00程序将输出test1(应该是fail2),

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

如果我选择开始时间12:00和结束时间15:00,程序将输出test1(良好输出)

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

此类问题仅在我选择12:00时发生..

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

推荐答案

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");
    }
}

让我们试一试:

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

这打印:

fail2
test1
fail2
test1

我相信这与你的意图一致。请注意 LocalTime 解析字符串而无需显式格式化程序。此外,如果您相信您的组合框仅包含有效的时间字符串,则可以省略尝试 - catch 构造 DateTimeParseException 是未经检查的例外。

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.

如果 startTime 并且 endTime JComboBox ,我相信你甚至可以填 LocalTime 对象进入他们。然后,当用户从每个用户中选择一个时,您不需要解析。你的 JComboBox 将调用 LocalTime.toString(),它将返回一个类似 09的字符串: 00 ,反过来组合框将显示并让用户选择。

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);

展开:

我正在使用 LocalTime 来自 java.time ,现代Java日期和时间API。 java.time 通常比旧的和过时的日期和时间类更好,例如 SimpleDateFormat 日期等等。

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.

链接: Oracle教程:日期时间,解释如何使用 java.time

这篇关于选择12:00时比较时间不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆