【发布时间】:2014-06-13 11:01:16
【问题描述】:
我的代码有什么问题,我无法从我想要解析的日期时间获得确切的输出?
String convertDate="03/19/2014 5:30:10 PM";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.CANADA);
Date myDate=new Date();
try {
myDate=df.parse(convertDate);
final Calendar c = Calendar.getInstance();
c.setTime(myDate);
System.out.println("Year = " + c.get(Calendar.YEAR));
System.out.println("Month = " + (c.get(Calendar.MONTH)));
System.out.println("Day = " + c.get(Calendar.DAY_OF_MONTH));
} catch (ParseException e1) {
e1.printStackTrace();
}
我的输出是
- 04-28 03:07:15.322: I/System.out(25095): Year = 2015
- 04-28 03:07:15.322: I/System.out(25095): Month = 6
- 04-28 03:07:15.322: I/System.out(25095): Day = 3
应该是
- Year = 2014
- Month = 03
- Day = 19
【问题讨论】:
-
记住 c.get(Calendar.MONTH) 是从零开始的(例如,一月是第 0 个月,等等)。
标签: java android datetime calendar simpledateformat