File lastModified() returns Wed Dec 31 19:00:00 EST 1969

I was doing some testing with files like this:

    public Date findFileDate(){
    File file = new File(filePath);
    Date date = new Date(file.lastModified());
    return date;
}

When I print date it says: Wed Dec 31 19:00:00 EST 1969. After some research I found that is my "time since the Unix Epoch" according to my time zone, but I am confused why I would get this output when no file exists at my filePath. Why would it not return null or 0?

Jon Skeet
people
quotationmark

No, file.lastModified() is returning 0. That's the Unix epoch

In your particular time zone (Eastern US by the looks of it), local time at the Unix epoch was 5 hours behind UTC, so it was 7pm on December 31st 1969.

To confirm this, just separate your Date declaration and assignment into two:

long lastModifiedMillis = file.lastModified();
Date date = new Date(lastModifiedMillis);

Now if you examine lastModifiedMillis I'm sure you'll find a value of 0, as documented:

Returns
A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs

people

See more on this question at Stackoverflow