I need when I run the code it takes the current date of the system and assigns a Date
.
Example:
Date x = new Date(now);
I need when I run the code it takes the current date of the system and assigns a Date
.
Example:
Date x = new Date(now);
Date x = new Date();
The default constructor of Date
creates the Date
object with the current date and time of the system clock, as reported by the operating system.
Just do not pass any parameters. So:
Date now = new Date();
As can be seen here , this is the same as calling the constructor by passing the value of System.currentTimeMillis
:
Date now = new Date(System.currentTimeMillis());
However, the java.util.Date
class may have problems with time zone. In most cases we do not want to worry if Java is running on a machine in Brazil or Japan.
So if you're using Java 8, you'd prefer the new date and hour where the LocalDateTime
class exists:
LocalDateTime now = LocalDateTime.now();