JDK1.8中LocalDate和LocalDateTime常用方法总结

一、 LocalDate

LocalDate now = LocalDate.now(); //当前时间
int day = now.getDayOfYear(); //当前时间在所在年份的第几天
DayOfWeek week = now.getDayOfWeek(); //当前时间是星期几(英文显示)
int getDayOfMonth = now.getDayOfMonth(); //当前日期是当月的第几天

Month getMonth = now.getMonth(); //当前月份(月份显示英文)
int getMonthValue = now.getMonthValue(); //当前月份int类型
int lengthOfMonth = now.lengthOfMonth(); //当月有多少天

int lengthOfYear = now.lengthOfYear(); //当前时间所在年份有多少天
boolean isLeapYear = now.isLeapYear(); //当前时间所在年份是否闰月

LocalDate minusDays = now.minusDays(1); //当前时间减一天
LocalDate minusWeeks = now.minusWeeks(1); //当前时间减一周
LocalDate minusMonths = now.minusMonths(1); //当前时间减一个月
LocalDate minusYears = now.minusYears(1); //当前时间减一年

LocalDate plusDays = now.plusDays(1); //当前时间加一天
LocalDate plusWeeks = now.plusWeeks(1); //当前时间加一周
LocalDate plusMonths = now.plusMonths(1); //当前时间加一个月
LocalDate plusYears = now.plusYears(1); //当前时间加一年

LocalDate withDayOfMonth = now.withDayOfMonth(5); //设置当前月份的号数 如2020-12-05 这个5号就是自己设置的
LocalDate withDayOfYear = now.withDayOfYear(10); //设置当前年份中的号数 如2020-01-10 这个10号就是自己设置的
LocalDate withMonth = now.withMonth(8); //设置月份
LocalDate withYear = now.withYear(2019); //设置年份

LocalDate tomorrow = LocalDate.now().plusDays(1);
boolean isAfter = now.isAfter(tomorrow); //比较当前时间是否在tomorrow之后 是为true、否为false
boolean isBefore = now.isBefore(tomorrow); //比较当前时间是否在tomorrow之前 是为true、否为false

二、 LocalDateTime

LocalDateTime nowTime = LocalDateTime.now(); //获取当前时间带时分秒
int hour = nowTime.getHour(); //获取当前时间的小时
int minute = nowTime.getMinute(); //获取当前时间的分钟
int second = nowTime.getSecond(); //获取当前时间的秒数
int nano = nowTime.getNano(); //获取当前时间的纳秒值

LocalDateTime withHour = nowTime.withHour(20); //设置当前时间的小时 返回值:2020-12-09T20:12:17.173
LocalDateTime withMinute = nowTime.withMinute(55); //设置当前时间的分钟数 返回值:2020-12-09T17:55:47.918
LocalDateTime withSecond = nowTime.withSecond(25); //设置当前时间的秒数 返回值:2020-12-09T17:13:25.278

LocalDate toLocalDate = nowTime.toLocalDate(); //获取当前时间只有年月日
LocalTime toLocalTime = nowTime.toLocalTime(); //获取当前时间的时分秒毫秒

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值