有没有办法用java 8流更改dateFormat? [英] Is there a way to change dateFormat with java 8 stream?

查看:61
本文介绍了有没有办法用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]

这篇关于有没有办法用java 8流更改dateFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆