Java program to get current date/time
www.igiftedia.com
Here's a Java program to get the current date and time:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = now.format(formatter); System.out.println("Current Date and Time: " + formattedDateTime); } }
This program uses the LocalDateTime
class to get the current date and time. The LocalDateTime.now()
method returns an instance of LocalDateTime
representing the current date and time.
We then use a DateTimeFormatter
to format the date and time as a string. In this example, we are using the format "yyyy-MM-dd HH:mm:ss", which will produce a string that looks like "2023-02-16 12:34:56".
Finally, we print the formatted date and time to the console using System.out.println()
.
This program can be customized to use different date/time formats or to perform additional date/time calculations.