JDK1.8新增时间日期API

JDK1.8新增时间日期API

(1)

LocalDate 表示年月日

LocalTime 表示时分秒

LocalDateTime 表示年月日时分秒

通过一个静态方法now()获取当前时间

通过of方法可以指定日期

(2)举例

package 日期;

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

public class Test {
    public static void main(String[] args) {
        LocalDate now1 = LocalDate.now();
        System.out.println(now1.getYear());
        System.out.println(now1);
        
        LocalTime now2 = LocalTime.now();
        System.out.println(now2);
        
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String format = now.format(f);
        System.out.println(format);

        LocalDate of = LocalDate.of(2010, 12, 3);
        //判断一个日期是在另一个日期之前或之后
        boolean after = now1.isAfter(of);
        System.out.println(after);
        boolean before = now1.isBefore(of);
        System.out.println(before);

        //判断两个日期是否相同
        boolean equal = now1.isEqual(of);
        System.out.println(equal);
        //判断是不是闰年
        boolean leapYear = of.isLeapYear();
        System.out.println(leapYear);
    


        // 添加年月日时分秒的方法 plus系列的方法 都会返回一个新的LocalDateTime的对象
        //每次加完时间量,都会返回一个新的日期对象
        LocalDateTime localDateTime = now.plusDays(10);
        System.out.println(localDateTime);
        LocalDateTime localDateTime1 = now.plusYears(2);
        System.out.println(localDateTime1);


        //减去年月日时分秒的方法 minus 系列的方法 注意都会返回一个新的LocalDateTime的对象
        LocalDateTime localDateTime2 = now.minusYears(2);
        System.out.println(localDateTime2);
        
        Instant now3 = Instant.now();
        long epochSecond = now3.getEpochSecond();//获取的秒值,从计算机元年到当前时刻
        System.out.println(epochSecond);
        long l = now3.toEpochMilli();//获取的是毫秒值
        System.out.println(l);
        
        
         Instant start = Instant.now();
        for (int i = 0; i < 100000; i++) {
            System.out.println(' ');
        }
        Instant end = Instant.now();
        //Duration 可以计算两个时间的间隔
        Duration between = Duration.between(start, end);
        long l1 = between.toMillis();
        System.out.println(l);

        
        
          LocalDate birthday = LocalDate.of(1995, 10, 10);
        LocalDate now4 = LocalDate.now();
        //Period 计算两个日期的间隔
        Period between1 = Period.between(birthday, now4);
        int years = between1.getYears();
        int months = between1.getMonths();
        int days = between1.getDays();
        System.out.println(years);
        System.out.println(months);
        System.out.println(days);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值