EN
Java - print current date and time
0
points
In this article, we would like to show you how to print the current date and time in Java.
Quick solution:
import java.util.Date;
public class Example {
private static Date date = new Date(); // create Date object
public static void main(String[] args) {
System.out.println(date.toString()); // print Date object
}
}
Practical example
To print the current date and time in Java you need to:
- Create a new
Date
object without any arguments - this will assign the current date and time to the new object, - use
toString()
method to print the new object.
Example code:
import java.util.Date;
public class Example {
private static Date date = new Date();
public static void main(String[] args) {
System.out.println(date.toString());
}
}
Output:
Mon May 24 17:23:44 CEST 2021