2017-06-25 27 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我不知道我明白你想要什麼,但無論如何我已經更新了答案。 –