【发布时间】:2019-02-11 19:19:39
【问题描述】:
我想将字符串更改为我正在使用SimpleDateFormat 类的日期格式。我从字符串列表中将字符串作为String+Integer.toString(int) 传递,并将SimpleDateFormat pattern 作为输入传递。
注意:如果我传递像“2019 年 1 月 9 日”这样的实际字符串,则不是 String+Integer.toString(int),而是成功地将字符串转换为日期。我尝试了很多不同的东西。
dateList 是“MMM dd”甲酸盐日期的列表。
通过执行dateList.get(5)+Integer.toString(year) 在该格式上添加年份,这给了我解析异常Jan 09 2019,将字符串转换为日期。
finalDatesInMMMDDYYYYFormat 是另一个列表,我以 MMM dd yyyy 格式保存日期。
Utils.parseDate 是我在 Utils 类中写的一个方法,其中我提到了 try-catch 块。
int year = 2019;
private List<String> dateList = new ArrayList<>();
private List<Date> finalDatesInMMMDDYYYYFormat = new ArrayList<>();
final String testString = dateList.get(5)+Integer.toString(year);
finalDatesInMMMDDYYYYFormat.add(Utils.parseDate(testString, new SimpleDateFormat("MMM dd yyyy")));
预期:将字符串改成日期并添加到finalDatesInMMMDDYYYYFormat
实际:获取解析异常。
【问题讨论】:
标签: java string parsing arraylist simpledateformat