1

我想为一些沉迷于日期值的数据创建一个小的 GUI。所以我正在使用LocalDateTimeTextFieldJavaFX。因此,我将使用以下代码获得选择的时间:

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. 但我的想法是设置tbegin一个日期并在之后自动设置tend为 23:59:59h,但也可能在之后更改为 10:00:00h。但也许我必须创建一个按钮来执行这些临时步骤。所以要理解,我想要什么,这里有一些图片:

单击按钮之前/之后

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

4

2 回答 2

3

也许我误解了这个问题,但是您不能在更改tend.setLocalDateTime(...)时简单地打电话吗?tbegin.localDateTimeProperty()

IE

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);
于 2017-06-25T19:51:50.727 回答
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

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

DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("HH:mm");
DateTimeFormatter fmt2 = DateTimeFormatter.ISO_DATE;

System.out.println(fmt1.format(dt));
System.out.println(fmt2.format(dt));

输出将是:


2017-06-25 17:42

检查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:59timebegin

于 2017-06-25T20:48:41.437 回答