• 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,!
  • 相关阅读:
    .NET基础知识-类型、方法与继承
    .NET基础知识-string、StringBuilder、字符串操作
    .NET基础知识-装箱与拆箱
    .NET基础知识-值类型与引用类型
    Python之多进程、多线程、协程、同步/异步、并发/并行
    设计模式之工厂模式
    设计模式之单例模式
    python模块之logging
    python模块之hashlib
    python模块之pickle&json&shelve
  • 原文地址:https://www.cnblogs.com/evan-liang/p/12233929.html
Copyright © 2020-2023  润新知