DateTimeFormatter 和 LocalDateTime 日期转换

package com.hr.util.test.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterTest {

public static void main(String[] args) {
    //将字符串转换为Date类型
    String dateStr = "2020-08-18";
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate parse = LocalDate.parse(dateStr, timeFormatter);
    System.out.println(parse);

    //Date转换为字符串
    LocalDateTime date = LocalDateTime.now();
    System.out.println(date);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
    String format = date.format(formatter);
    System.out.println(format);
}

}

上一篇:TroubleShooting Hyper-V 虚拟机因磁盘配置问题无法启动


下一篇:高性能算力,“加速”智能公共服务创新