Week 70 — How can one get the current date and time in a Java application?

Question of the Week #70
How can one get the current date and time in a Java application?
9 Replies
Eric McIntyre
Eric McIntyre8mo ago
Java allows obtaining the current time in milliseconds since the UNIX epoch (1.1.1970) using the method System.currentTimeMillis(). However, the java.time API provides many other ways of working with times and dates which also includes options for getting the current date/time. For example, it is possible to obtain an Instant representing the current timestamp (without any timezone information) using the method Instant.now(). Similarly, one can get the current date and time (using the system timezone) using LocalDateTime.now() (or LocalDate.now() for the date and LocalTime.now() for the time). If additional timezone information is needed, one can use ZonedDateTime.now() which returns the current date and time including information about the system timezone.
long milliTime = System.currentTimeMillis();
Instant currentInstant = Instant.now();
LocalDateTime currentDateTime = LocalDateTime.now();
ZonedDateTime currentZonedDateTime = ZonedDateTime.now();

System.out.println("time in milliseconds since the UNIX epoch: " + milliTime);
System.out.println("current Instant: " + currentInstant);
System.out.println("current datetime: " + currentDateTime);
System.out.println("current ZonedDateTime: " + currentZonedDateTime);
long milliTime = System.currentTimeMillis();
Instant currentInstant = Instant.now();
LocalDateTime currentDateTime = LocalDateTime.now();
ZonedDateTime currentZonedDateTime = ZonedDateTime.now();

System.out.println("time in milliseconds since the UNIX epoch: " + milliTime);
System.out.println("current Instant: " + currentInstant);
System.out.println("current datetime: " + currentDateTime);
System.out.println("current ZonedDateTime: " + currentZonedDateTime);
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre8mo ago
Using the Date class to get current date and time in Java: import java.util.Date; import java.util.Date; public class CurrentDateTimeExample { public static void main(String[] args) { Date date = new Date(); System.out.println(date); // Prints current date and time in a specific format } } /*This code only prints the date in Day_Month_Day_Number and time in HH:MM:SS formats */
Submission from tgamerboy_02
Eric McIntyre
Eric McIntyre8mo ago
import java.time.Clock;
import java.time.ZonedDateTime;

Clock clock = Clock.systemDefaultZone();
ZonedDateTime dateTime = ZonedDateTime.now(clock);
import java.time.Clock;
import java.time.ZonedDateTime;

Clock clock = Clock.systemDefaultZone();
ZonedDateTime dateTime = ZonedDateTime.now(clock);
Submission from elspon
Eric McIntyre
Eric McIntyre8mo ago
LocalDateTime.now()
Eric McIntyre
Eric McIntyre8mo ago
Use this to get current date and time
Submission from ishaik921
Eric McIntyre
Eric McIntyre8mo ago
import java.util.Calendar;

public class Main {
public static void main(String[] args) {
Calendar curr = Calendar.getInstance();
System.out.println(curr.getTime());
}
}
import java.util.Calendar;

public class Main {
public static void main(String[] args) {
Calendar curr = Calendar.getInstance();
System.out.println(curr.getTime());
}
}
Calender package helps us find the current date and time. You must handle the changes if you want time in local time zone on your side. Date package is deprecated it was used previously to find current date and time.
Submission from sk001
Eric McIntyre
Eric McIntyre8mo ago
public class CurrentDateTime {
public static void main(String[] args) {
// Get the current date and time
LocalDateTime currentDateTime = LocalDateTime.now();

// Define a format for the date and time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// Format the current date and time using the defined format
String formattedDateTime = currentDateTime.format(formatter);

// Print the formatted date and time
System.out.println("Current Date and Time: " + formattedDateTime);
}
}
public class CurrentDateTime {
public static void main(String[] args) {
// Get the current date and time
LocalDateTime currentDateTime = LocalDateTime.now();

// Define a format for the date and time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// Format the current date and time using the defined format
String formattedDateTime = currentDateTime.format(formatter);

// Print the formatted date and time
System.out.println("Current Date and Time: " + formattedDateTime);
}
}
We use LocalDateTime.now() method to obtain the current date and time and boom! We find ourselves obtaining the current date and time in a Java application.
Submission from jaymoney95
Eric McIntyre
Eric McIntyre8mo ago
LocalDateTime.now();
Submission from roadgeek878
Eric McIntyre
Eric McIntyre8mo ago
Numerous ways:
// JDK 1.0+
new java.util.Date();
// UNIX timestamp
java.lang.System.currentTimeMillis();
// Java 8+
java.time.LocaleDateTime.now(); // No timezone info
java.time.ZonedDateTime.now(); // At default timezone
java.time.ZonedDateTime.now(ZoneOffset.UTC); // At UTC
java.time.ZonedDateTime.now(someTimeZone); // At the given timezone or offset
java.time.Instant.now(); // Calendar & timezone agnostic
// JDK 1.0+
new java.util.Date();
// UNIX timestamp
java.lang.System.currentTimeMillis();
// Java 8+
java.time.LocaleDateTime.now(); // No timezone info
java.time.ZonedDateTime.now(); // At default timezone
java.time.ZonedDateTime.now(ZoneOffset.UTC); // At UTC
java.time.ZonedDateTime.now(someTimeZone); // At the given timezone or offset
java.time.Instant.now(); // Calendar & timezone agnostic
⭐ Submission from dangerously_casual
Want results from more Discord servers?
Add your server