Java8常用日期格式化&字符串日期相互转换

 LocalDateTime localDateTime=LocalDateTime.now();
//格式化日期时间类型为字符串
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String ss = dateTimeFormatter.format(localDateTime).toString();
System.out.println(ss);
//日期时间字符串类型转 LocalDateTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime yy = LocalDateTime.parse("2017-09-28 21:22:33", df);
System.out.println(yy);
//日期字符串转日期类型
LocalDate localDate=LocalDate.parse("2019-12-10",DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(localDate.toString());


  
  //带时区时间字符串转换标准字符串
  DateTimeFormatter dateTimeFormatter_ = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
  LocalDateTime strr = LocalDateTime.parse("2020-01-02T08:28:43.785",DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
  String time = dateTimeFormatter_.format(strr);
  System.out.println("格式化后标准时间:"+time);

posted @ 2020-01-01 14:06  洞玄巅峰  阅读(18098)  评论(0编辑  收藏  举报