Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Date(0) is not 1/1/1970

Tags:

java

I was expecting this to print 1970-01-01-00:00:00, but it prints 1970-12-31-19:00:00

What am I misinterpreting about how Date is counted from? It is one year off. I am running this on Windows 7 with JDK 1.6

System.out.println(new SimpleDateFormat("YYYY-MM-dd-HH:mm:ss").format(new Date(0)));
like image 470
EdgeCase Avatar asked Oct 25 '11 20:10

EdgeCase


People also ask

How do you equal a date in Java?

The equals() method of Java Date class checks if two Dates are equal, based on millisecond difference. Parameters: The function accepts a single parameter obj which specifies the object to be compared with. Return Value: The function gives 2 return values specified below: true if the objects are equal.

How do you check if the date is in YYYY MM DD format in Java?

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); formatter. setLenient(false); try { Date date= formatter. parse("02/03/2010"); } catch (ParseException e) { //If input date is in different format or invalid. }

What is date default value in Java?

An initial date can be set via the constructor or by calling setValue(LocalDate) . The default value is null.


1 Answers

What am I misinterpreting about how Date is counted from?

new Date(0) is January 1, 1970 in UTC. Your timezone is not UTC.

It is one year off.

No, it's not. The printed value is only 5 hours behind. Let me guess - you're somewhere in the eastern part of the US?

like image 85
Matt Ball Avatar answered Oct 21 '22 04:10

Matt Ball