-1

I'm currently working on a JavaFX project and having issues while displaying current date in a text area. I set the date and time format by using the following code:

SimpleDateFormat Date = new SimpleDateFormat("dd/MM/yy hh/mm");
Date date = new Date();

For example, I want the current date and time to be displayed as 15/05/2021 15:03 but the textarea shows this:

SAT May 15 15:03:21 PDT 2021

Is there anyway to set date format to "dd/MM/yy hh/mm"?

*NOTE: I imported both java.text.SimpleDateFormat and java.util.Date.

llgx 10
  • 1
  • 1
  • 3
  • Try to avoid the old Date & Time API. Use the `java.time` API added in Java 8 instead. – Slaw May 15 '21 at 08:40

1 Answers1

1

The java.util.Datejava.util.Calendar, and java.text.SimpleDateFormat classes were rushed too quickly when Java first launched and evolved. The classes were not well designed or implemented. Improvements were attempted, thus the deprecations you’ve found. Unfortunately the attempts at improvement largely failed. You should avoid these classes altogether. They are supplanted in Java 8 by new classes (Basil Bourque).

Use this instead:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime now = LocalDateTime.now();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy hh:mm");

TextArea textArea = new TextArea(formatter.format(now));
James_D
  • 201,275
  • 16
  • 291
  • 322
anko
  • 1,628
  • 1
  • 6
  • 14