【发布时间】:2017-08-17 16:14:48
【问题描述】:
我有一个存储在List<Date> 中的日期列表,并从方法getDates 中获取数据。现在我想将其转换为 ArrayList<String> 但我收到了这个错误:
ArrayList 不能应用于 java.util.List。
因为我想从列表中更改日期格式。我试过这个Convert ArrayList of String type into Date type? 并转换日期,第一个日期必须存储为字符串,但我的情况是我的日期存储在列表中。问题是,我仍然无法将 List 转换为字符串。
这是我的代码:
List<Date> dates = getDates(date1, date2);
ArrayList<String> dateStringList = new ArrayList<>();
dateStringList.add(dates);
有没有办法将List<Date> 中的数据转换为ArrayList<String>?
试了很多方法还是报错。
【问题讨论】:
-
遍历日期列表,将每个日期转换为字符串,然后将字符串添加到您的 dateStringList 中。谷歌“convert Date to String Java”会给你几十个答案
-
将你的 dateStringList 实例化为 List 而不是 ArrayList
-
是的,有很多关于“将日期转换为 Java 字符串”的答案,但它与我的情况不同。我已经编辑了我的问题@GabeSechan