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:
xxxxxxxxxx
1
import java.util.Date;
2
3
public class Example {
4
private static Date date = new Date(); // create Date object
5
6
public static void main(String[] args) {
7
System.out.println(date.toString()); // print Date object
8
}
9
}
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:
xxxxxxxxxx
1
import java.util.Date;
2
3
public class Example {
4
private static Date date = new Date();
5
6
public static void main(String[] args) {
7
System.out.println(date.toString());
8
}
9
}
Output:
xxxxxxxxxx
1
Mon May 24 17:23:44 CEST 2021