【发布时间】:2020-11-16 19:28:14
【问题描述】:
尝试使用 SimpleDateFormat.parse("yyyy-mm-dd") 将 yyyy-mm-dd 字符串(例如 2013-12-30)转换为日期对象。
2013 年 12 月 30 日的预期输出,2013 年 12 月 30 日星期一 00:00:00 EST 对象的接收输出。
试图找出为什么 SimpleDateFormat 返回不同的格式,但在尝试查看 java api 时不知所措。要求澄清正在发生的事情以及更好的方法。
注意:使用 java.utl.Date 卡住了。
...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateArray = new Date[rowCount];
try {
for(int index = 0; index < rowCount; index++){
dateArray[index] = simpleDateFormat.parse(fileArray[index][0]);
System.out.println(dateArray[index].toString());
}
} catch(ParseException err){
System.out.println("ERR: Data parse exception. Format is not correct.");
err.printStackTrace();
}
【问题讨论】:
-
如果我可能会问,你为什么使用旧的
SimpleDateFormat和Date类? -
不要直接打印
Date,而是使用SimpleDateFormat(format()方法)获取相同格式的字符串。 -
格式与
Date对象是分开的。使用simpleDateFormat.format(dateArray[index]);。 -
@MCEmperor:参加过时的 Java 大学课程。课程已更新,但作业未更新。只有少数分配有一个有一个或两个使用已弃用包的规范。在这种情况下,它将是 Date 类。
-
@Kayaman:感谢您的明确解释。我对 SimpleDataFormat 的工作原理有了更好的理解,同时也减慢了拼凑我一直在阅读的内容的速度。在这种情况下,我真的应该构建一个打印方法。谢谢。
标签: java date simpledateformat