【发布时间】:2012-11-02 22:42:05
【问题描述】:
我从数据库中获取日期,其中包含日期和时间戳,在 UI 中它同时显示日期和时间戳,但我只想显示日期 (yyyy-MM-dd)。我已完成更改,但无法正确设置。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String startDate = dateFormat.format(dataObj.getReportStartDate());
//The above startDate will be of the format : 2012-11-08
我希望将 String 格式的 startDate 设置为 Date 数据类型。
dataIndexObj.setReportStartDate(startDate);
// Here the setReportStartDate is of the type date(), I cannot change its type to string.
我也尝试过 parse(startDate) 。它没有用。知道怎么做吗?
【问题讨论】:
-
“它没有用”是什么意思?是否存在编译错误,是否在运行时抛出异常,它没有按照您的预期执行吗?
-
我收到错误,因为无法从字符串转换为日期。我希望将 mm-dd-yyyy 格式设置回数据类型为 Date 的变量。
-
您可以将 java.util.Date 转换为格式化的字符串,但您不能告诉 Date 对象如何在内部对其进行格式化。
标签: java string date time timestamp