【发布时间】:2013-03-20 11:15:40
【问题描述】:
通常,我是这样做的:
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d = f.parse("2012-12-21");
Calendar c = Calendar.getInstance();
c.setTime(d);
bean.setDate(c);
但我刚刚找到了可行的解决方案:
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
f.parse("2012-12-21");
bean.setDate(f.getCalendar());
为什么在doc 中没有指定parse()(例如)记住解析后的值而不是简单地返回它?这是一个不好的方法吗?感觉自己被背叛了这么多年……
【问题讨论】:
-
它可能有效,但 API 声明 parse 返回解析后的日期。我宁愿依赖公共指定的 API,也不愿依赖可能改变的实现细节。
标签: java date calendar date-format date-parsing