我正在使用on方法,但没有获得正确的时间戳
long dateToTimestamp = Common.dateToTimestamp("Mon, 14 Dec 2015 10:20:37 GMT", "EE, dd MMM yyy HH:mm:dd zz");
public static final long dateToTimestamp(String date, String format) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
format);
dateFormat.setTimeZone(getDefaultTimeZone());
Date parsedDate = dateFormat.parse(date);
Timestamp timestamp = new Timestamp(
parsedDate.getTime());
return timestamp.getTime() / 1000;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static final TimeZone getDefaultTimeZone() {
Calendar cal = Calendar.getInstance();
return cal.getTimeZone();
}
但没有得到正确的时间戳。
试试这个,对我有效,
String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date mDate = sdf.parse(givenDateString);
long timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}