java8新时间类

1.LocalDate

	@Test
    public void localDateTest(){
        //获取当前日期
        LocalDate now = LocalDate.now();
        System.out.println("现在"+now);
        //自定义日期
        LocalDate localDate= LocalDate.of(2020,2,3);
        System.out.println("自定义"+localDate);

        System.out.println("年份"+localDate.getYear()+
        					"月份"+localDate.getMonthValue()+
        					"本月天"+localDate.getDayOfMonth());
        System.out.println("本年天"+localDate.getDayOfYear()+
        					"获取周几(枚举)"+ localDate.getDayOfWeek());

        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        System.out.println(localDate.format(dateTimeFormatter));

        System.out.println("是否在某个日期之前"+localDate.isBefore(now));
    }

在这里插入图片描述

2.LocalTime

 	@Test
    public void localTimeTest(){
        //现在
        LocalTime now = LocalTime.now();
        System.out.println("现在"+now);
        //自定义
        LocalTime localTime = LocalTime.of(20, 20, 20);
        System.out.println("自定义"+localTime);
        System.out.println("小时"+localTime.getHour()+
        		"分钟"+localTime.getMinute()+
        		"秒"+localTime.getSecond());
    }

在这里插入图片描述

3.LocalDateTime

	@Test
    public void localDateTime(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println("现在"+now);
        //自定义
        LocalDateTime ldt = LocalDateTime.of(2020, 2, 3, 12, 12, 12);
        System.out.println(ldt);
        System.out.println("设置年"+ldt.withYear(2019)+
        				   "设置月"+ldt.withMonth(12)+
        				   "设置天"+ldt.withDayOfMonth(12));
        System.out.println("设置小时"+ldt.withHour(1)+
        				   "设置分钟"+ldt.withMinute(1)+
        				   "设置秒"+ldt.withSecond(1));

        System.out.println("下一年"+ldt.plusYears(1)+"上一年"+ldt.minusYears(1));
    }

在这里插入图片描述

	@Test
    public void locaDateTime2(){
        //自定义进行时间偏移
        LocalDateTime now = LocalDateTime.now();
        //获取下周一
        System.out.println("下周一"+now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));
        //获取本周一(方式1)
        System.out.println("本周一"+now.with(l->{
            LocalDateTime ldt= (LocalDateTime) l;
            //获取int值
            int day=ldt.getDayOfWeek().getValue();
            return ldt.plusDays(1-day);
        }));
        //获取本周一(方式2,下周一减一周)
        System.out.println("本周一"+
        		now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusWeeks(1));
        //上周一(方式1)
        System.out.println("上周一"+now.with(l->{
            LocalDateTime ldt=(LocalDateTime) l;
            int value = ldt.getDayOfWeek().getValue();
            return ldt.plusDays(1-value).minusWeeks(1);
        }));
        //上周一(方式2)
        System.out.println("上周一"+
        		now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusWeeks(2));
    }

在这里插入图片描述

4.Instant

	@Test
    public void instantTest(){
        //时间戳
        Instant ins=Instant.now();
        //毫秒值
        System.out.println(ins.toEpochMilli());
    }

在这里插入图片描述

5.Period Duration

	/**
     * 用于计算时间的数量
     */
    @Test
    public void countOfTime(){
        //日期间隔
        Period period = Period.between(LocalDate.of(1990, 1, 1), LocalDate.now());
        System.out.println("相差年份"+period.getYears()+
        					"相差月份"+period.getMonths()+
        					"相差天数"+period.getDays());

        //时间间隔
        Duration duration = Duration.between(LocalDateTime.of(2020, 5, 14, 1, 1, 1), 
        															LocalDateTime.now());
        System.out.println("一共相差多少天"+duration.toDays()+
        					"相差小时"+duration.toHours()+
        					"相差秒"+duration.getSeconds());
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值