【发布时间】:2020-06-26 15:15:46
【问题描述】:
当我将特定格式应用于日期时出现解析异常。
这是我的代码:
System.out.println("*********************1**********************");
if(row.getCell(67).toString().equals("null"))
{
System.out.println("*********************2**********************");
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("*******************************************************");
}
这是错误:
java.text.ParseException:无法解析的日期:“30-janv.-2020”
【问题讨论】:
-
我建议你不要使用
SimpleDateFormat和Calendar。这些类设计不佳且早已过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time API 的LocalDate和DateTimeFormatter。
标签: java date datetime calendar apache-poi