22

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 :)

Daniel Piskorz
  • 390
  • 1
  • 2
  • 11

5 Answers5

44

It's probably better to use Java 8 types (java.time) in a new application. You can first create a DateTimeFormatter:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");

And then get the current time and format it:

Label timeLabel = new Label(LocalTime.now().format(dtf));
wypieprz
  • 7,981
  • 4
  • 43
  • 46
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • 4
    Good answer. I would normally specify time zone, for example `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. – Ole V.V. Jun 22 '17 at 14:15
  • That should have been “…the choice of time zone…”, of course. @OleVV – Ole V.V. Jun 23 '17 at 08:47
7
LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm"))

(this will use the default locale, see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ofPattern-java.lang.String- for more).

But better practice would be to put the formatter into a static final field. This way it's only created once instead of every time the line is exectuted. It nearly certainly doesn't really matter for this application, but it's better to use good habits from the beginning.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
2

Try something like this:

Label timeLabel = new Label(String.format("%tR", LocalTime.now()));

See: http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
0

You can create a LocalTime instance in several ways. The first way is to create a LocalTime instance that represents the exact time of now. Here is how that looks:

LocalTime localTime = LocalTime.now();

Another way to create a LocalTime object is to create it from a specific amount of hours, minutes, seconds and nanoseconds. Here is how that looks:

LocalTime localTime2 = LocalTime.of(21, 30, 59, 11001);

There are also other versions of the of() method that only takes hours and minutes, or hours, minutes and seconds as parameters.

You can access the hours, minutes, seconds and nanosecond of a LocalTime object using these methods:

  • getHour()
  • getMinute()
  • getSecond()
  • getNano()

check this out, it helped me alot- http://tutorials.jenkov.com/java-date-time/localtime.html

Kobi Lehrer
  • 151
  • 5
0
String expectedDepartureTime = "1500";
String expectedArrivalTime = "0100";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HHmm");
LocalTime edt = LocalTime.parse(expectedDepartureTime, dtf);
LocalTime eta = LocalTime.parse(expectedArrivalTime, dtf);
System.out.println(eta.isBefore(edt) ? "You have a red eye flight": "You don't have a red eye flight");
Martin
  • 448
  • 3
  • 10