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

9 messages · Page 1 of 1 (latest)

leaden rootBOT
#
Question of the Week #70

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

quiet veldtBOT
#

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);
📖 Sample answer from dan1st
quiet veldtBOT
#

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
#
import java.time.Clock;
import java.time.ZonedDateTime;

Clock clock = Clock.systemDefaultZone();
ZonedDateTime dateTime = ZonedDateTime.now(clock);
Submission from elspon
#

LocalDateTime.now()

#

Use this to get current date and time

Submission from ishaik921
#
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_
#
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
#

LocalDateTime.now();

Submission from roadgeek878