我正在尝试将时间戳解析为一个人类可读的日期字符串,但是,我一直把它作为一个回归的1/15/17。
//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);
我以为我可能会使用毫秒而不是秒,但是当我以毫秒为单位提供它时,我得到一个例外。
有人遇到过这个问题吗?
答案 0 :(得分:2)
Date构造函数采用long,而不是int,它是自纪元以来的毫秒数。
//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);
答案 1 :(得分:1)
您应该使用毫秒作为构造函数Date(long date)。请记住使用Long.parseLong(String s)代替。