I'm creating my JavaFX application and I need to use time label every time new list cell is created. I need to put the string with current time in HH:MM
format directly into Label constructor which takes String
as a parameter.
I found and used java.util.Date
's:
Label timeLabel = new Label(new SimpleDateFormat("HH:MM").format(new Date()));
but it shows the wrong time zone, so I'm going to use java.time
and LocalTime
class.
Is there any way to achieve same string result in one line? Thank You for your help :)
LocalTime.now(ZoneId.systemDefault())
. One, this makes it clear that getting the current time is a time zone dependent operation. Two, it tells the reader and yourself that you have thought about the choice of locale and made a decission. – Mincing