2017-06-25 25 views
1

后,我想创建成瘾日期值的一些数据小GUI。所以我正在使用JavaFX中的LocalDateTimeTextField。所以我会选择的时间用下面的代码:JavaFX的设置自然而然最终LocalDateTime选择启动LocalDateTime

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年3月8日00:00
恩德:2017- 03-08 23:59

Choosen Time

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

有没有一种解决方案,已选定的开始时间后,自动生成一个给定的结束时间,例如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"); 

但现在我知道,我的问题是不正确的措辞,因为我还是想改变结束日期tend在应用程序中。但是没有机会,因为它是固定在23:59:在tbegin后59H。但我的想法是设置tbegin与日期自动设置tend到23:59:59H后,但也可能改变为10:00:00H后。但是也许我必须创建一个Button,它可以完成这些临时步骤。可以这么理解,我想要的,这里的一些图片:

Before/After clicking the Button

所以应该给我场与结束日期,我点击该按钮之前。而且应该可以将enddate编辑为其他日期/时间。

回答

3

也许我误解了这个问题,但是不能只是在tbegin.localDateTimeProperty()发生变化时致电tend.setLocalDateTime(...)

I.e.

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

谢谢你的帮助:)我从来没有真正意识到格式化的事实:)非常感谢!现在我已经更新了我的问题,并带有一个新旧问题:D – Franky

1

陆续获得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)); 

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

2017年6月25日十七点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年6月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)); 

这将设置timeend23:59:59,不管什么在timebegin的时间。

+0

非常感谢!这真的有助于:)但现在我有问题,不可能编辑结束日期,因为它被设置为开始日期之后的23:59:59h。因此,我更新了我的问题:D也许你有更多的想法:D谢谢:) – Franky

+0

@Franky我不知道我明白你想要什么,但无论如何我已经更新了答案。 –