问题描述
时间戳格式化后的日期变成了1970年。。。
出现问题的原因
我们的时间戳是秒级的
为什么秒级
的就不能转换成功
源码给予的答案:
注意代码的注释:{@code Long} (milliseconds) object
/**
* <p>Formats a {@code Date}, {@code Calendar} or
* {@code Long} (milliseconds) object.</p>
* @since 3.5
* @param obj the object to format
* @return The formatted value.
*/
String format(Object obj) {
if (obj instanceof Date) {
return format((Date) obj);
} else if (obj instanceof Calendar) {
return format((Calendar) obj);
} else if (obj instanceof Long) {
return format(((Long) obj).longValue());
} else {
throw new IllegalArgumentException("Unknown class: " +
(obj == null ? "<null>" : obj.getClass().getName()));
}
}
因为在源码中要求到:必须是毫秒–>{@code Long} (milliseconds) object
正确示范
//date是秒级的,所以需要 * 1000
//若是毫秒级,就不用做这步操作
String lessonTime = simpleDateFormat.format(Long.valueOf(date * 1000));