Pick up current machine date

8

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);
    
asked by anonymous 21.10.2015 / 07:39

2 answers

7
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.

    
21.10.2015 / 07:52
8

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(); 
    
21.10.2015 / 07:54