user393964
user393964

Reputation:

Does a new Date Object in java contain the current date? Or is it empty?

Date now = new Date();

    if (now.getTime() - leasedDate.getTime() > 21 * 1000 * 60 * 60 * 24)
        throw new TooLate();
    leased.remove(x);

I'm looking at some code examples and above is a part of it. There's something I don't understand. Does the date object called "now" have the current date and hour in it? Because I thought it should be empty when it's initialised so I don't understand how now.getTime() can work.

Thanks!

Upvotes: 1

Views: 2552

Answers (4)

iryndin
iryndin

Reputation: 570

Yes,

Date now = new Date();

contains the current system time (the exact time of the object creation in RAM).

Upvotes: 0

rsp
rsp

Reputation: 23373

The Date object contains a long which represents the time in milliseconds since 1970. The default constructor initialised it from System.currentTimeMillis().

Upvotes: 5

Petar Minchev
Petar Minchev

Reputation: 47383

Quote from Java Docs - new Date() - Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

So the answer to your question is: Yes, it contains the current date.

Upvotes: 11

Related Questions