选择 Start-LocalDateTime 后,JavaFX 自动设置 End-LocalDateTime
JavaFX set automatical End-LocalDateTime after choosing Start-LocalDateTime
我想为一些沉迷于日期值的数据创建一个小型 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);
Beginn: 2017-03-08 00:00
Ende: 2017-03-08 23:59
因此,为了获得开始时间和结束时间,我必须手动选择这两个日期。
是否有解决方案,自动生成给定的结束时间,例如在选择的开始时间后 1 小时?这样我就有更少的 "work" 来编辑时报,最好的情况下只需要选择一个日期。
感谢您的建议!他们工作得很好:)
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");
但现在我知道,我的问题表达不当,因为我仍然想更改应用程序中的结束日期 tend
。但是没有机会,因为在tbegin
之后固定为23:59:59h。但我的想法是用日期设置 tbegin
并在之后自动将 tend
设置为 23:59:59h,但也可能在之后更改为 10:00:00h。但也许我必须创建一个按钮来执行这些临时步骤。所以为了理解,我想要什么,这里有一些图片:
所以它应该在我单击按钮之前为我提供带有结束日期的字段。并且应该可以将结束日期编辑为其他 date/time.
也许我误解了这个问题,但是你不能在 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);
要获得一个 LocalDateTime
1 小时后的另一个,请使用 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));
此代码将输出(提醒它是我所在时区的当前 date/time):
25.06.2017 17:42:50
对于其他情况,您不需要使用replace
和substring
。也只需使用格式化程序:
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));
这将在 23:59:59
设置 timeend
,无论 timebegin
的时间是什么。
我想为一些沉迷于日期值的数据创建一个小型 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);
Beginn: 2017-03-08 00:00
Ende: 2017-03-08 23:59
因此,为了获得开始时间和结束时间,我必须手动选择这两个日期。
是否有解决方案,自动生成给定的结束时间,例如在选择的开始时间后 1 小时?这样我就有更少的 "work" 来编辑时报,最好的情况下只需要选择一个日期。
感谢您的建议!他们工作得很好:)
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");
但现在我知道,我的问题表达不当,因为我仍然想更改应用程序中的结束日期 tend
。但是没有机会,因为在tbegin
之后固定为23:59:59h。但我的想法是用日期设置 tbegin
并在之后自动将 tend
设置为 23:59:59h,但也可能在之后更改为 10:00:00h。但也许我必须创建一个按钮来执行这些临时步骤。所以为了理解,我想要什么,这里有一些图片:
所以它应该在我单击按钮之前为我提供带有结束日期的字段。并且应该可以将结束日期编辑为其他 date/time.
也许我误解了这个问题,但是你不能在 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);
要获得一个 LocalDateTime
1 小时后的另一个,请使用 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));
此代码将输出(提醒它是我所在时区的当前 date/time):
25.06.2017 17:42:50
对于其他情况,您不需要使用replace
和substring
。也只需使用格式化程序:
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));
这将在 23:59:59
设置 timeend
,无论 timebegin
的时间是什么。