有没有办法用java 8流更改dateFormat?
问题描述:
我想将日期格式从 dd / mm / yyyy
更改为 yyyy / mm / dd
在Java8流中只有一行
I want to change date format from "dd/mm/yyyy
" to "yyyy/mm/dd
" with one line in java8 stream
List<String[]> date = new ArrayList<>();
String[] a= {"12/2/2018","a1","a2"};
String[] b= {"13/3/2018","b1","b2"};
String[] c= {"14/4/2018","c1","c2"};
date.add(a)`
date.add(b);
date.add(c);
我希望输出为
{{"2018/2/12","a1","a2"},{"2018/2/13","b1","b2"},{"2018/2/14","c1","c2"}}
答
我希望您的意思是 yyyy / MM / dd
coz m是分钟,M是月...
I hope you mean yyyy/MM/dd
coz m is for minutes and M for month...
从流API中考虑地图
public static void main(String[] args) {
List<String[]> date = new ArrayList<>();
String[] a= {"12/2/2018","a1","a2"};
String[] b= {"13/3/2018","b1","b2"};
String[] c= {"14/4/2018","c1","c2"};
date.add(a);
date.add(b);
date.add(c);
List<String[]> even = date.stream().map(
s -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
LocalDate localDate = LocalDate.parse(s[0], formatter);
DateTimeFormatter formatterNew = DateTimeFormatter.ofPattern("yyyy/MM/dd");
return new String[]{formatterNew.format(localDate), s[1],s[2]};
}
).collect(Collectors.toList());
even.forEach(x-> System.out.println(Arrays.toString(x)));
}
将打印出来
[2018/02/12,a1,a2]
[2018/02/12, a1, a2]
[2018/03/13,b1,b2]
[2018/03/13, b1, b2]
[2018/04/14,c1,c2]
[2018/04/14, c1, c2]