【发布时间】:2020-06-26 10:01:39
【问题描述】:
我希望日期格式为 yyyy-MM-dd。我的代码是:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd", Locale.ENGLISH);
cal.setTime(sdf.parse(row.getCell(67).toString()));
单元格中的值为:06/02/2020 但是,我得到的错误是:
java.text.ParseException: Unparseable date: "null"
at java.text.DateFormat.parse(Unknown Source) ~[na:1.8.0_251]
感谢您的帮助....
但是,我希望日期格式为 yyyy-MM-dd。
这是完整的代码:
if(row.getCell(67).equals("null"))
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1988);
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date dateRepresentation = cal.getTime();
rfx.setRv_rc_date(dateRepresentation);
}
else
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd", Locale.ENGLISH);
cal.setTime(sdf.parse(row.getCell(67).toString()));
System.out.println("******************************************************");
System.out.println(cal);
System.out.println("*******************************************************");
}
【问题讨论】:
-
请检查您的数据并调试您的代码,对我来说这看起来好像
row.getCell(67).toString()返回null -
你的
sdf中的“-”在哪里? -
另外,尽量避免
java.util的过时日期时间API,改用java.time。 -
请再次查看我刚刚更新的答案
-
我收到了这个错误:java.text.ParseException: Unparseable date: "30-janv.-2020"