获取LocalDateTime类型数据间隔的月数,周数,天数小时数,分钟数等

场景:获取两个时间段间隔的时间,用于判断是否有效

使用ChronoUnit工具类,里面可以获取间隔的月数,周数,天数小时数,分钟数等

下面这个是获取小时数

 

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

LocalDateTime dateTime = LocalDateTime.of(2023, 8, 1, 12, 30); // 示例时间
long hours = ChronoUnit.HOURS.between(dateTime, LocalDateTime.now());
System.out.println("时间差为:" + hours + "小时");

 其它时间用法如下

    /**
     * Unit that represents the concept of a nanosecond, the smallest supported unit of time.
     * For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
     */
    NANOS("Nanos", Duration.ofNanos(1)),
    /**
     * Unit that represents the concept of a microsecond.
     * For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.
     */
    MICROS("Micros", Duration.ofNanos(1000)),
    /**
     * Unit that represents the concept of a millisecond.
     * For the ISO calendar system, it is equal to the 1000th part of the second unit.
     */
    MILLIS("Millis", Duration.ofNanos(1000_000)),
    /**
     * Unit that represents the concept of a second.
     * For the ISO calendar system, it is equal to the second in the SI system
     * of units, except around a leap-second.
     */
    SECONDS("Seconds", Duration.ofSeconds(1)),
    /**
     * Unit that represents the concept of a minute.
     * For the ISO calendar system, it is equal to 60 seconds.
     */
    MINUTES("Minutes", Duration.ofSeconds(60)),
    /**
     * Unit that represents the concept of an hour.
     * For the ISO calendar system, it is equal to 60 minutes.
     */
    HOURS("Hours", Duration.ofSeconds(3600)),
    /**
     * Unit that represents the concept of half a day, as used in AM/PM.
     * For the ISO calendar system, it is equal to 12 hours.
     */
    HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),
    /**
     * Unit that represents the concept of a day.
     * For the ISO calendar system, it is the standard day from midnight to midnight.
     * The estimated duration of a day is {@code 24 Hours}.
     * <p>
     * When used with other calendar systems it must correspond to the day defined by
     * the rising and setting of the Sun on Earth. It is not required that days begin
     * at midnight - when converting between calendar systems, the date should be
     * equivalent at midday.
     */
    DAYS("Days", Duration.ofSeconds(86400)),
    /**
     * Unit that represents the concept of a week.
     * For the ISO calendar system, it is equal to 7 days.
     * <p>
     * When used with other calendar systems it must correspond to an integral number of days.
     */
    WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),
    /**
     * Unit that represents the concept of a month.
     * For the ISO calendar system, the length of the month varies by month-of-year.
     * The estimated duration of a month is one twelfth of {@code 365.2425 Days}.
     * <p>
     * When used with other calendar systems it must correspond to an integral number of days.
     */
    MONTHS("Months", Duration.ofSeconds(31556952L / 12)),
    /**
     * Unit that represents the concept of a year.
     * For the ISO calendar system, it is equal to 12 months.
     * The estimated duration of a year is {@code 365.2425 Days}.
     * <p>
     * When used with other calendar systems it must correspond to an integral number of days
     * or months roughly equal to a year defined by the passage of the Earth around the Sun.
     */
    YEARS("Years", Duration.ofSeconds(31556952L)),
    /**
     * Unit that represents the concept of a decade.
     * For the ISO calendar system, it is equal to 10 years.
     * <p>
     * When used with other calendar systems it must correspond to an integral number of days
     * and is normally an integral number of years.
     */
    DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),
    /**
     * Unit that represents the concept of a century.
     * For the ISO calendar system, it is equal to 100 years.
     * <p>
     * When used with other calendar systems it must correspond to an integral number of days
     * and is normally an integral number of years.
     */
    CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),
    /**
     * Unit that represents the concept of a millennium.
     * For the ISO calendar system, it is equal to 1000 years.
     * <p>
     * When used with other calendar systems it must correspond to an integral number of days
     * and is normally an integral number of years.
     */
    MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),
    /**
     * Unit that represents the concept of an era.
     * The ISO calendar system doesn't have eras thus it is impossible to add
     * an era to a date or date-time.
     * The estimated duration of the era is artificially defined as {@code 1,000,000,000 Years}.
     * <p>
     * When used with other calendar systems there are no restrictions on the unit.
     */
    ERAS("Eras", Duration.ofSeconds(31556952L * 1000_000_000L)),
    /**
     * Artificial unit that represents the concept of forever.
     * This is primarily used with {@link TemporalField} to represent unbounded fields
     * such as the year or era.
     * The estimated duration of the era is artificially defined as the largest duration
     * supported by {@code Duration}.
     */
    FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 您好,我可以回答您的问题。要判断两个日期的先后,可以将日期转换为时间戳,然后比较大小。计算间隔天数可以将两个日期转换为时间戳后相减,再将结果转换为天数。计算间隔周数可以将间隔天数除以7,取整即可。希望能够帮到您。 ### 回答2: 要判断两个日期的先后,我们可以比较这两个日期的大小。如果第一个日期在第二个日期之前,则我们可以说第一个日期较早;如果第一个日期在第二个日期之后,则我们可以说第一个日期较晚。如果两个日期相同,则可以说这两个日期相等。 计算两个日期的间隔天数可以通过数学运算来实现。我们可以将两个日期都转换为自公元1年1月1日以来的天数,并计算这两个天数的差值。这样就可以得到这两个日期之间的天数间隔。 计算两个日期的间隔周数可以通过先计算两个日期的间隔天数,然后再将天数除以7来得到。因为每周有7天,所以将天数除以7就可以得到相应的周数。 例如,如果第一个日期是2022年1月1日,第二个日期是2022年1月8日,那么这两个日期的间隔天数是7天,间隔周数是1周。 总结起来,判断两个日期的先后可以比较它们的大小,计算两个日期的间隔天数可以通过计算天数差值,计算两个日期的间隔周数可以通过计算天数除以7得到。 ### 回答3: 判断两个日期的先后可以根据日期的大小进行比较。比较的时候可以按照年、月、日依次进行比较。如果第一个日期的年份大于第二个日期的年份,则第一个日期较晚;如果年份相同,再比较月份,月份大的日期较晚;如果年份和月份都相同,则比较日,日大的日期较晚。根据这种比较方式,我们就可以判断两个日期的先后关系。 计算间隔天数可以通过减法运算获得。假设第一个日期为A,第二个日期为B,我们可以将A的年、月、日分别与B的年、月、日相减,并将这个差值相加,即可得到间隔天数。如果差值为负数,则表示A日期在B日期之后,需要取绝对值。 计算间隔周数可以通过间隔天数与7进行整除得到。首先计算两个日期的间隔天数D,然后将D除以7,取商即可得到间隔周数。如果D除以7的余数不为0,则表示有剩余的天数,不能整除,所以需要再加上1周。 总结起来,判断两个日期的先后关系,可以通过比较年、月、日的大小。计算间隔天数可以通过减法运算得到,而计算间隔周数可以通过间隔天数除以7得到。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值