Reputation: 33
I want to create a small GUI for some Data in addiction to date values. So I'm working with LocalDateTimeTextField
in JavaFX. So I will get the chosen Time with the following code:
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);
As output I will get:
System.out.println("Beginn: "+datum + " " + beginzeit);
System.out.println("Ende: "+datum + " " + endezeit);
Beginn: 2017-03-08 00:00
Ende: 2017-03-08 23:59
So for getting the Time for the Begining and the End I have to choose both Dates manually.
Is there a solution, to generate automatically a given Endtime, for example 1 Hour after the choosen starting Time? So that I have less "work" to edit the Times and in best case only have to choose one date.
Thank you for your suggestions! They´re working fine:)
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");
But now I knew, that my question was not right couched, because I still want to change the Enddate tend
in the application. But there is no chance, because it's fixed to 23:59:59h after the tbegin
. But my idea was to set the tbegin
with a date and automatically set the tend
to 23:59:59h after, but with also changing maybe to 10:00:00h after. But maybe I have to create a Button, which do these interim step. So to understand, what I want, here some pictures:
So it should give me the field with the Enddate before I'm clicking the Button. And it should be possible, to edit the enddate to some other date/time.
Upvotes: 1
Views: 679
Reputation:
To get a LocalDateTime
1 hour after another, use the plusHours
method:
LocalDateTime dt = LocalDateTime.now();
// new date 1 hour after dt
LocalDateTime newDt = dt.plusHours(1);
And to control the output format, use a DateTimeFormatter
:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
System.out.println(formatter.format(dt));
This code will output (reminding that it's the current date/time in my timezone):
25.06.2017 17:42:50
For the other cases, you don't need to use replace
and substring
. Just use the formatters as well:
DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("HH:mm");
DateTimeFormatter fmt2 = DateTimeFormatter.ISO_DATE;
System.out.println(fmt1.format(dt));
System.out.println(fmt2.format(dt));
The output will be:
17:42
2017-06-25
Check the javadoc for more details about the output patterns.
When you call timebegin.plusHours(23).plusMinutes(59).plusSeconds(59)
, you're adding a period to the date. If timebegin
is at 10:00, the result will be the next day at 09:59:59
.
If you want to set the time to be exactly at 23:59:59
, you should do:
LocalDateTime timeend = timebegin.with(LocalTime.of(23, 59, 59));
This will set timeend
at 23:59:59
, no matter what's the time in timebegin
.
Upvotes: 1
Reputation: 209418
Maybe I am misunderstanding the question, but can't you simply call tend.setLocalDateTime(...)
whenever tbegin.localDateTimeProperty()
changes?
I.e.
tbegin.localDateTimeProperty().addListener((obs, oldTime, newTime) ->
tend.setLocalDateTime(newTime.plusHours(1)));
As an aside, you should not rely on string representations in order to extract data from an object. If the implementation of the toString()
method changes, your code will break. You should also use the formatting API to convert dates and times to strings, instead of relying on toString
. You should do, for example:
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);
Upvotes: 3