11

说明: jdk1.8 严格区分LocalDate(日期) LocalDateTime(时间日期) LocalTime(时间)

public static void main(String[] args) {
    //localDateTime 获取当前时间
    DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("yy-MM-dd");
    LocalDate localDateTime=LocalDate.now();
    localDateTime.format(dateTimeFormatter);
    System.out.println(localDateTime);

    //字符转成LocalDateTime  
    String time="2018-05-18 23:24:25";
    DateTimeFormatter dateTimeFormatter2=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//严格遵守大小写
    LocalDateTime localDate=LocalDateTime.parse(time,dateTimeFormatter2);
    LocalDate localDate1= LocalDate.of(localDate.getYear(),localDate.getMonth(),localDate.getDayOfMonth());
    System.out.println(localDate1);//2018-05-18

    //date 转LocalDateTime
    java.util.Date date = new java.util.Date();
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant, zone);
    System.out.println(localDateTime2);


    // LocalDateTime 转date
    LocalDateTime localDateTime3 = LocalDateTime.now();
    Instant instan3 = localDateTime3.atZone(zone).toInstant();
    java.util.Date date3 = Date.from(instant);
    System.out.println(date3);
}
上一篇:java8 localDateTime与 Date互相转化


下一篇:听说你还在用Date?