美文网首页
Java基础篇之java8新特性:日期时间

Java基础篇之java8新特性:日期时间

作者: writeanewworld | 来源:发表于2020-01-11 15:44 被阅读0次

1.前言

.Java8新日期对象 LocalDate
.日期格式化
1)、Java8之前SimpleDateFormat是非线程安全的
2)、Java8 引入线程安全的DateTimeFormatter
.日期差计算
1)、Duration

2.例子

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

public class Java8date {

    public static void main(String[] args) {
        LocalDate local = LocalDate.now();
        System.out.println(local);//2020-01-11

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);//2020-01-11T15:37:41.946

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s = formatter.format(localDateTime);
        System.out.println(s);//2020-01-11 15:37:41

        //构造自定义时间
        LocalDateTime localDateTime1 = LocalDateTime.of(2020,11,8,2,2,2);
        System.out.println(localDateTime1);//2020-11-08T02:02:02

        //日期差计算
        Duration duration = Duration.between(LocalDateTime.now(),localDateTime1);
        System.out.println(duration.toDays());//俩日期相差的天数
    }

}

相关文章

网友评论

      本文标题:Java基础篇之java8新特性:日期时间

      本文链接:https://www.haomeiwen.com/subject/vcxmactx.html