我在Java / Android中遇到时间戳问题
Date inputDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.e("DateH:D", hour+" "+day);
try {
inputDate = sdf.parse(hour + " " + day);
Date currentDate = new Date();
Log.e("InputDate", inputDate.toString());
Log.e("InputDate",inputDate.getTime()+"");
Log.e("CurrentDate", currentDate.toString());
Log.e("CurrentDate",currentDate.getTime()+"");
if (!inputDate.after(currentDate) ){
//TODO change this string
hourField.setError("Date from past");
return false;
}
} catch (ParseException e) {
Log.e("DateParser" , e.getLocalizedMessage);
return false;
}
输出示例如下:
DateH:D: 12:33 15/09/16
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/InputDate: -61640134020000
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/CurrentDate: Thu Sep 15 11:38:43 CEST 2016
E/CurrentDate: 1473932323198
因此,日期的非时间戳表示是正确的,但时间戳是错误的。怎么可能?我做错了什么?
最佳答案
使用yy
解析两位数的年份。
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yy");