【发布时间】:2016-09-18 03:50:58
【问题描述】:
我正在尝试以 (MM dd, yyyy, HH:mm [AM/PM]) 的格式转换回存储的日期/时间字符串,我按照 post 创建字符串日期/时间。我当前的代码正确地获取了存储的日期时间,但是当我尝试解析它并显示日期对象“d”时,我得到一个空输出。
Java 代码:
public void loadExamData(Cursor cursor) {
exam.setText(cursor.getString(1));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(cursor.getString(2));
} catch (ParseException ex) {
Logger.getLogger(edit_exam_form.class.getName()).log(Level.SEVERE, null, ex);
}
location.setText(cursor.getString(3));
}
【问题讨论】:
-
一种更简单的方法可能是存储一个 unix 时间戳......这样,当您检索它时,您可以对它做任何您想做的事情。
-
SimpleDateFormat.parse() 出错时返回 null。您确定您初始化 sdf 的格式字符串与来自光标的字符串匹配吗?