【问题标题】:JavaFX set automatical End-LocalDateTime after choosing Start-LocalDateTimeJavaFX 选择 Start-LocalDateTime 后自动设置 End-LocalDateTime
【发布时间】:2017-11-28 17:13:24
【问题描述】:

我想为一些依赖日期值的数据创建一个小型 GUI。所以我在 JavaFX 中使用LocalDateTimeTextField。所以我将使用以下代码获得选择的时间:

LocalDateTimeTextField tend;
LocalDateTimeTextField tbegin;

String en = tend.getLocalDateTime().withSecond(0).toString();
String ende = en.replaceAll("T", " ");
String endezeit = ende.substring(11, 16);
String be = tbegin.getLocalDateTime().withSecond(0).toString();
String begin = be.replaceAll("T", " ");
String beginzeit = begin.substring(11, 16);
String datum = begin.substring(0, 10);

作为输出我会得到:

System.out.println("Beginn:    "+datum + " " + beginzeit);
System.out.println("Ende:      "+datum + " " + endezeit);

开始时间:2017-03-08 00:00
结束:2017-03-08 23:59

因此,为了获得开始时间和结束时间,我必须手动选择两个日期。

是否有解决方案自动生成给定的结束时间,例如在选择的开始时间后 1 小时?这样我编辑时报的“工作量”就更少了,最好只选择一个日期。


感谢您的建议!他们工作正常:)

    LocalDateTime timebegin = tbegin.getLocalDateTime().withSecond(0);
    LocalDateTime timeend = timebegin.plusHours(23).plusMinutes(59).plusSeconds(59);
    LocalDate datum = timebegin.toLocalDate();
    LocalTime start = timebegin.toLocalTime().plusSeconds(0);
    LocalTime ende = timeend.toLocalTime();
    tend.setLocalDateTime(timeend);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

但现在我知道,我的问题没有正确表达,因为我仍然想更改应用程序中的 Enddate tend。但是没有机会,因为它在tbegin 之后固定为23:59:59h。但我的想法是为tbegin 设置一个日期,并在之后自动将tend 设置为23:59:59h,但也可能在之后更改为10:00:00h。但也许我必须创建一个按钮来执行这些临时步骤。所以要明白,我想要什么,这里有一些图片:

因此,在我单击按钮之前,它应该给我带有 Enddate 的字段。并且应该可以将结束日期编辑为其他日期/时间。

【问题讨论】:

    标签: java javafx time localdate


    【解决方案1】:

    也许我误解了这个问题,但是当tbegin.localDateTimeProperty() 发生变化时,您不能简单地拨打tend.setLocalDateTime(...) 吗?

    tbegin.localDateTimeProperty().addListener((obs, oldTime, newTime) -> 
        tend.setLocalDateTime(newTime.plusHours(1)));
    

    顺便说一句,您不应该依赖字符串表示来从对象中提取数据。如果toString() 方法的实现发生变化,您的代码将会中断。您还应该使用格式化 API 将日期和时间转换为字符串,而不是依赖 toString。例如,您应该这样做:

    LocalDateTime end = tend.getLocalDateTime();
    
    // if you need these:
    LocalDate endDate = end.toLocalDate();
    LocalTime endTime = end.toLocalTime();
    
    // to display:
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm");
    String stringEnd = formatter.format(end);
    

    【讨论】:

    • 感谢您的帮助:) 我从来没有真正意识到格式化程序的事实:) 非常感谢!现在我更新了我的问题,有一个新/旧问题:D
    【解决方案2】:

    要在 1 小时后获得 LocalDateTime,请使用 plusHours 方法:

    LocalDateTime dt = LocalDateTime.now();
    // new date 1 hour after dt
    LocalDateTime newDt = dt.plusHours(1);
    

    要控制输出格式,请使用DateTimeFormatter:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
    System.out.println(formatter.format(dt));
    

    此代码将输出(提醒这是我时区的当前日期/时间):

    25.06.2017 17:42:50

    对于其他情况,您不需要使用replacesubstring。也只需使用格式化程序:

    DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("HH:mm");
    DateTimeFormatter fmt2 = DateTimeFormatter.ISO_DATE;
    
    System.out.println(fmt1.format(dt));
    System.out.println(fmt2.format(dt));
    

    输出将是:

    17:42
    2017-06-25

    查看javadoc 了解有关输出模式的更多详细信息。


    当您致电timebegin.plusHours(23).plusMinutes(59).plusSeconds(59) 时,您将在日期中添加一个句点。如果timebegin 是在 10:00,则结果将是第二天09:59:59

    如果您想在23:59:59 将时间设置为准确,您应该这样做:

    LocalDateTime timeend = timebegin.with(LocalTime.of(23, 59, 59));
    

    这会将timeend 设置为23:59:59,无论timebegin 是什么时间。

    【讨论】:

    • 非常感谢!这真的很有帮助:) 但现在我遇到了一个问题,即无法编辑结束日期,因为它被设置为开始日期后的 23:59:59h。因此我更新了我的问题:D 也许你有更多的想法:D 谢谢你:)
    • @Franky 我不确定我理解你想要什么,但无论如何我已经更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2019-09-10
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2016-04-10
    • 2018-01-03
    • 1970-01-01
    相关资源
    最近更新 更多