新日期API

LocalDate类

方法:

new()//获取当前时间                of(y,M,d)   //获取时间对象

ofYearDay(int Year,int dayofYear) //获取哪年第多少天的日期

parse(String) //将字符串转为日期

    LocalDate now = LocalDate.now();//获取当前时间。
        System.out.println(now);
        LocalDate of = LocalDate.of(2019, 12, 25);//获取2019.12.25对象
        System.out.println(of);
        // TODO 获取2017年第223天的日期
        LocalDate localDate = LocalDate.ofYearDay(2019, 223);
        System.out.println(localDate);
        LocalDate parse = LocalDate.parse("2019-12-25");//字符串转为日期格式
        System.out.println(parse);
        ValueRange range = parse.range(ChronoField.DAY_OF_MONTH);//12月天数范围
        System.out.println(range);
        LocalDate localDate1 = now.plusDays(25);
        System.out.println(localDate1);
        //localDate 和 localDate1相差的天数
        long until = localDate.until(localDate1, ChronoUnit.DAYS);
        System.out.println(until);
        String format = parse.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        // 将date5的日期转化为 day/month/year
        System.out.println(format)

 

LocalTime类

方法

now();     plusHours() ;      minusHours();   isBefore() ;    isAfter();

format(DateTimeFormatter.ofPattern("hh:mm:ss"));// 将当前时间转化为 hh:mm:ss 格式的字符串
  LocalTime currentTime = LocalTime.now();
        System.out.println(currentTime);
        // 获取伦敦的时间
        LocalTime i = LocalTime.now(ZoneId.of("Europe/London"));
        System.out.println(i);

        // 10个小时20分钟之后是几点
        LocalTime localTime = LocalTime.now().plusHours(10).plusHours(20);
        System.out.println(localTime);

        // 判断localTime时间是否为当前时间之后的时间
        boolean after = localTime.isAfter(LocalTime.now());
        System.out.println(after);

        // 当前时间的前20个小时间是几点
        LocalTime beforeTime = currentTime.minusHours(20);
        System.out.println(beforeTime);

        // 判断beforeTime是否在当前时间之前
        boolean before = beforeTime.isBefore(currentTime);
        System.out.println(before);

        // 将当前时间转化为 hh:mm:ss 格式的字符串
        String format = currentTime.format(DateTimeFormatter.ofPattern("hh:mm:ss"));
        System.out.println(format);

LocalDateTime

方法

new()//获取当前时间     format();           range() //查看范围

     LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        String nowStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
        System.out.println(nowStr);

        // 查看now日期时间的所在月份的天范围
        ValueRange range = now.range(ChronoField.DAY_OF_MONTH);
        System.out.println(range.getMinimum()+","+range.getMaximum());

        // 获取第一天和最后一天的日期
        LocalDate first = LocalDate.of(now.getYear(), now.getMonth(),(int)range.getMinimum());
        LocalDate last = LocalDate.of(now.getYear(), now.getMonth(),(int)range.getMaximum());

        System.out.println(first+"/"+last);

 Instan

now()          plusSeconds()

   ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime.toLocalDateTime());
        Instant now = Instant.now();
        now = now.plusSeconds(8*3600); // 增加8个小时的秒数
        // 查看时间戳的毫秒值
        System.out.println(now.toEpochMilli()+","+now);
        System.out.println(System.currentTimeMillis()+","+ new Date(System.currentTimeMillis()));
        System.out.println(new Date().getTime()+","+new Date());

小白写的仅供参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值