zoukankan      html  css  js  c++  java
  • Java8新特性一点通 | 回顾字符转日期&JoinArray使用

    StringToDate日期转换

    • Convert string to date in ISO8601 format

      • 利用LocalDate.parse(CharSequence text) 直接以ISO8601方式格式化
     String originalDate = "2018-08-07";
            LocalDate localDate = LocalDate.parse(originalDate);
            System.out.println("Date:"+localDate);
    
    Output:
    
    Date:2018-08-07
    • Convert string to date in custom formats
      • 利用DateTimeFormatter.ofPattern(String pattern)LocalDate.parse(CharSequence text, DateTimeFormatter formatter)结合对日期以指定格式格式化
    String originalDate1 = "2018-08-07 10:47:00";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
            LocalDate localDate1 = LocalDate.parse(originalDate1,formatter);
            System.out.println("custom date:"+localDate1);
    
    Output:
    
    custom date:2018-08-07

    Join Array使用

    对现有数组添加字符串组成新的字符串的几个方法:
    - String.join(CharSequence delimiter, CharSequence… elements) 场景:可以对现有数组添加分隔符组成新的字符串

    String joinedString = String.join(",", "hello", "world", "!");
            System.out.println("示例1:" + joinedString);
    
    Output:
    
    示例1:hello,world,!
    • String.join(CharSequence delimiter, Iterable elements) 场景:可以对现有列表添加分隔符组成新的字符串
    List<String> strList = Arrays.asList("hello", "world", "!");
            String joinedString1 = String.join(",", strList);
            System.out.println("示例2:" + joinedString1);
    Output:
    
    示例2:hello,world,!
    • StringJoiner(CharSequence delimiter) 场景:添加分隔符
     StringJoiner stringJoiner1 = new StringJoiner(",");
            stringJoiner1.add("hello").add("world").add("!");
            System.out.println("示例3:" + stringJoiner1.toString());
    
    Output:
    
    示例3:hello,world,!
    • StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 场景:添加分隔符以及前后缀
    StringJoiner stringJoiner2 = new StringJoiner(",", "[", "]");
            stringJoiner2.add("hello").add("world").add("!");
            System.out.println("示例4:" + stringJoiner2.toString());
    
    Output:
    
    示例4:[hello,world,!]
    • Collectors.joining 场景:在lambda表达式里面用,针对添加分隔符和前后缀的场景
      List<String> strList1 = Arrays.asList("hello", "world", "!");
            String joinedString3 = strList1.stream().collect(Collectors.joining(",","[","]"));
            System.out.println("示例5:"+joinedString3);
    
    Output:
    
    示例5:[hello,world,!]
    • StringUtils.join() 场景:跟以上用法差不多,使用工具类去把数据和列表组成单独的字符串
    String[] strArray = {"hello", "world", "!"};
            String joinedString4 = StringUtils.join(strArray, ",");
            System.out.println("示例6:" + joinedString4);
    
    Output:
    
    示例6:hello,world,!
  • 相关阅读:
    ssm集成--多模块
    ssm项目集成--单模块
    shiro的学习以及shiro集成spring
    RecyclerView的使用。
    RecyclerView 显示不全的问题.
    Android 中使用数据库作为存储,并随机发布的Demo。
    Fragment的两种加载方式。
    gradle生成的eclipse的web项目,在发布后,缺少jar包的情况。
    gradle 4,构建java web程序
    android 下载远程服务器文件。
  • 原文地址:https://www.cnblogs.com/evan-liang/p/12233929.html
Copyright © 2011-2022 走看看