介绍
在JDK8中,引入了三个非常有用的时间相关的API:Duration,Period和ChronoUnit。
他们都是用来对时间进行统计的。
Duration主要用来衡量秒级和纳秒级的时间,使用于时间精度要求比较高的情况。
ChronoUnit是用来表示时间单位的,但是也提供了一些非常有用的between方法来计算两个时间的差值。
当然以上是百度知乎里的,但不是很详细,指示了一个方向

没有对比就没有伤害,看下面代码

//---------------------------------------------------------------------------
这是使用了 chronoUnit 的写法  拿到的是一个时间区间,天数 ,只有正数,long between = ChronoUnit.DAYS.between(LocalDateTime.now(), servicePriceTime);if (between==0){companyService.setServicePriceTimeType(company.getId());}//---------------------------------------------------------------------------//,年月日  这是之前的写法String time1 = df.format(yearPriceTime).substring(0, 10);//取当前时间String time2 = df.format(LocalDateTime.now()).substring(0, 10);//时间到期了 改年费状态if (time1.equals(time2)) {companyService.setCompanyYearPriceType(company.getId());}

顺便提一下localDateTime的用法 这里直接看底层代码
非常简单

月份相加public LocalDateTime plusMonths(long months) {LocalDate newDate = date.plusMonths(months);return with(newDate, time);}
时间相加public LocalDateTime plusWeeks(long weeks) {LocalDate newDate = date.plusWeeks(weeks);return with(newDate, time);}天数相加public LocalDateTime plusDays(long days) {LocalDate newDate = date.plusDays(days);return with(newDate, time);}//-----------------------------------------------------------------------
小时相加public LocalDateTime plusHours(long hours) {return plusWithOverflow(date, hours, 0, 0, 0, 1);}分钟相加public LocalDateTime plusMinutes(long minutes) {return plusWithOverflow(date, 0, minutes, 0, 0, 1);}
秒相加LocalDateTime plusSeconds(long seconds) {return plusWithOverflow(date, 0, 0, seconds, 0, 1);}毫秒加public LocalDateTime plusNanos(long nanos) {return plusWithOverflow(date, 0, 0, 0, nanos, 1);}

还有减少的 这就不解释了

     */public LocalDateTime minusMonths(long months) {return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));}/*** Returns a copy of this {@code LocalDateTime} with the specified number of weeks subtracted.* <p>* This method subtracts the specified amount in weeks from the days field decrementing* the month and year fields as necessary to ensure the result remains valid.* The result is only invalid if the maximum/minimum year is exceeded.* <p>* For example, 2009-01-07 minus one week would result in 2008-12-31.* <p>* This instance is immutable and unaffected by this method call.** @param weeks  the weeks to subtract, may be negative* @return a {@code LocalDateTime} based on this date-time with the weeks subtracted, not null* @throws DateTimeException if the result exceeds the supported date range*/public LocalDateTime minusWeeks(long weeks) {return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));}/*** Returns a copy of this {@code LocalDateTime} with the specified number of days subtracted.* <p>* This method subtracts the specified amount from the days field decrementing the* month and year fields as necessary to ensure the result remains valid.* The result is only invalid if the maximum/minimum year is exceeded.* <p>* For example, 2009-01-01 minus one day would result in 2008-12-31.* <p>* This instance is immutable and unaffected by this method call.** @param days  the days to subtract, may be negative* @return a {@code LocalDateTime} based on this date-time with the days subtracted, not null* @throws DateTimeException if the result exceeds the supported date range*/public LocalDateTime minusDays(long days) {return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));}

先到这了, 永远的苏神,还有红婵大佬

ChronoUnit 的使用,localDateTime的用法相关推荐

  1. 【LocalDateTime常见用法】

    简单记录下LocalDateTime常见用法 1.获取系统当前时间(转换格式) (1)返回String类型时间格式(yyyy-MM-dd HH:mm:ss,yyyy-MM-dd,yyyy-MM-dd ...

  2. java的until_Java LocalDateTime until()用法及代码示例

    LocalDateTime类的until()方法用于使用TemporalUnit计算两个LocalDateTime对象之间的时间量.起点和终点是此点,指定的LocalDateTime作为参数传递.如果 ...

  3. java获取年份getyear_Java LocalDateTime getYear()用法及代码示例

    LocalDateTime类的getYear()方法用于返回year字段.此方法将MIN_YEAR到MAX_YEAR的Year的原始int值返回. 用法: public int getYear() 参 ...

  4. java day of month_Java LocalDateTime getDayOfMonth()用法及代码示例

    LocalDateTime类的getDayOfMonth()方法用于返回day-of-month字段.此方法返回一个介于1到31之间的整数值,即一个月的日期. 用法: public int getDa ...

  5. java compareto 时间_Java LocalDateTime compareTo()用法及代码示例

    Java中的LocalDateTime类的compareTo()方法用于将此日期时间与作为参数传递的日期时间进行比较. 用法: public int compareTo(ChronoLocalDate ...

  6. datetime parse java_Java LocalDateTime parse()用法及代码示例

    在LocalDateTime类中,根据传递给它的参数,有两种类型的parse()方法. parse(CharSequence text) LocalDateTime类的parse()方法用于从作为参数 ...

  7. java 时间before_Java LocalDateTime isBefore()用法及代码示例

    Java中的LocalDateTime类的isBefore()方法检查此日期是否在指定的日期时间之前. 用法: public boolean isAfter(ChronoLocalDateTime o ...

  8. LocalDateTime 的用法

    与 Date 相比 LocalDateTime 线程安全,因为所有字段都用了 final 修饰 可直接转换成 日期 和 时间,不需要 format Date 效率稍高,不过可以忽略 获取当前日期+时间 ...

  9. JDK1.8时间接口 LocalDateTime用法

    一.新时间日期API常用.重要对象介绍 ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则 Instant: 用来表示时间线上的一个点(瞬时) LocalDat ...

最新文章

  1. 分层级联Transformer!苏黎世联邦提出TransCNN: 显著降低了计算/空间复杂度!
  2. 2021年春季学期-信号与系统-第六次作业参考答案-第一小题
  3. keepalived(4)——演练故障出现时keepalived的状态
  4. 计算机应用基础专科作业二,电子科大18秋《计算机应用基础(专科)》在线作业2...
  5. String和STL的一些基础知识
  6. CVPR 2019 | 旷视提出新型目标检测损失函数:定位更精准
  7. 装Oracle10时报错,win10系统安装oracle10g时出现未知错误的详细教程
  8. win10深度清理c盘垃圾的方法【系统天地】
  9. java错误代码1061_求助java大神,看下这是哪里出错了
  10. 知识变现海哥:如何利用自己的时间和知识获得财富自由
  11. 轻松处理 针式打印机故障解决方法
  12. 如何用EasyRecovery恢复U盘内损坏的数据
  13. 深度Deepin20 安装软件的依赖问题(sudo apt --fix-broken install)
  14. 一台微型计算机字长为4个字节,若一台计算机的字长为4个字节,则表明该计算机()。...
  15. Python | 小白的 Asyncio 教程
  16. Android游戏之仿 微信飞机大战
  17. 【Java进阶营】2022全栈Java开发工程师要掌握哪些技能?
  18. 【业界冥灯】今年校招太难了我面试过的大厂都逃不过被制裁的命运
  19. CCleaner 4.07中文版下载 绿色精简
  20. docker: Error response from daemon: driver failed programming external connectivity on endpoint

热门文章

  1. php中的PDO对象---对PDO对象的认识、什么是PDO对象
  2. 2019.5.31 晴
  3. android开发app在huawei真机调试
  4. pandas --移动窗口rolling的概念
  5. 实现秒杀、抢购的实现方式
  6. C# task 取消
  7. 小目标检测相关技巧总结
  8. Magenta:Google带来机器与艺术的火花
  9. ---已搬运--:[0CTF 2016]piapiapia -----代码审计+字符串逃逸+数组绕过长度限制+以及一下小知识点 preg_match()用数组,而且注意if是正确判断,还是错误判断
  10. Java实现 LeetCode 212 单词搜索 II