tags:

views:

51

answers:

2

I am trying to parse a timestamp into a human-readable date string, however, I keep getting 1/15/1970 as a return.

//Here is my formatter
DateTimeFormat Format = DateTimeFormat.getFormat("MM/dd/yyyy");

//Here is my timestamp - SHOULD BE AUGUST 16TH, 2010
String date = "1281966439";

//Here is where I create the date, and format it
int intDate = Integer.parseInt(date);
Date eventDate = new Date(intDate);
String eventDateFinal = Format.format(eventDate);

//In this alert, I get 1/15/1970
Window.alert(eventFinal);

I thought I might be using milliseconds instead of seconds, but when I feed it the value in milliseconds, I get an exception.

Anyone ever have this problem?

+1  A: 

You should be using milliseconds for the constructor Date(long date). And remember to use Long.parseLong(String s) instead.

Zach Scrivena
+1  A: 

The Date constructor takes a long, not an int, and it's number of milliseconds since the epoch.

//Here is my formatter
DateTimeFormat Format = DateTimeFormat.getFormat("MM/dd/yyyy");

//Here is my timestamp - SHOULD BE AUGUST 16TH, 2010
String date = "1281966439000";

//Here is where I create the date, and format it
long longDate = Long.parseLong(date);
Date eventDate = new Date(longDate);
String eventDateFinal = Format.format(eventDate);

//In this alert, I get 1/15/1970
Window.alert(eventFinal);
Paul Tomblin
Both of you were right, but +1 to Paul for including the correct code...
cinqoTimo