Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat format wrong values

The following code:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
System.out.println(sdf.format(new Date(1293253200))); // 12/25/2010 05:00 GMT
System.out.println(sdf.format(new Date(1293339600))); // 12/26/2010 05:00 GMT
System.out.println(sdf.format(new Date(1293426000))); // 12/27/2010 05:00 GMT

prints:

01/16
01/16
01/16

Using a default DateFormat via SimpleDateFormat.getDateInstance(); prints these dates as 16-Jan-1970. What is going on?

like image 696
Finbarr Avatar asked Aug 27 '26 08:08

Finbarr


1 Answers

You are mixing milliseconds and seconds. 1293253200 is indeed 16. January 2010. You have to multiply with 1000 to get the dates you wanted:

Date date = new Date(1293253200L*1000L);
Sat Dec 25 06:00:00 CET 2010
like image 99
mhaller Avatar answered Aug 29 '26 22:08

mhaller