0

I want to make a real-time date function to record the user exact log in time and date whenever the user logged in successful into the system from the Java UI. Is that any method to generate a date rather than input the time and date manually every-time user log in into the system? Thanks for the help.

3 Answers3

1

From Java 8

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDateTime = now.format(formatter);
Steklorez
  • 109
  • 1
  • 4
  • 1
    It didn't work for me. – Wen Jin TIng Sep 05 '18 at 14:15
  • It works on my Java and just produced a `formatDateTime` of `2018-09-05 19:04:16`. What happens on your computer? Are you seeing any error messages? – Ole V.V. Sep 05 '18 at 17:05
  • To control what you get I recommend specifying time zone, for example `LocalDateTime.now(ZoneId.of("Australia/Yancowinna"))` or even `ZonedDateTime.now(ZoneId.of("Australia/Yancowinna"))`. – Ole V.V. Sep 05 '18 at 17:07
  • For the code in the answer to work you need `import java.time.LocalDateTime;` and `import java.time.format.DateTimeFormatter;`. You IDE should be able to figure that out for you (at least my Eclipse does; it’s very commonplace on Stack Overflow to leave out the import statements from the code presented). – Ole V.V. Sep 05 '18 at 17:11
  • **No.** `LocalDateTime` is exactly the **wrong class to use here**. That class does *not* represent a moment, lacking any concept of time zone or offset-from UTC, as explained in the class doc. Instead, use `Instant.now()`. – Basil Bourque Sep 06 '18 at 05:20
-1

You can use new Date(). This returns the current date/time.

moritz.vieli
  • 1,747
  • 1
  • 14
  • 17
  • The `Date` class is long outdated and poorly designed and was replaced by [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) four and a half years ago. I recommend using `Instant.now()` or looking into [the answer by Steklorez](https://stackoverflow.com/a/52186961/5772882). – Ole V.V. Sep 05 '18 at 17:05
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 06 '18 at 05:21
-1

In Java, many useful supporting commands require certain predefined processes. These are stored in what are called “library packages” or simply “packages”. You are allowed to use the default package without any prior mention in Java.

Date date = new Date();
String str = String.format("Current Date/Time : %tc", date );
System.out.printf(str);
RogerSK
  • 393
  • 1
  • 18
  • It didn't work for me. – Wen Jin TIng Sep 05 '18 at 14:15
  • Whenever you use the Java default package, always remember to import it. For these codes, you should import like this: `import java.util.Date;` – RogerSK Sep 05 '18 at 14:17
  • 1
    Thank you very much, now it worked, – Wen Jin TIng Sep 05 '18 at 14:22
  • The `Date` class is long outdated and poorly designed and was replaced by [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) four and a half years ago. I recommend using `Instant.now()` or looking into [the answer by Steklorez](https://stackoverflow.com/a/52186961/5772882). – Ole V.V. Sep 05 '18 at 17:03
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 06 '18 at 05:21