Period 计算日期之间的天数差------ 坑点*****

计算两个时间之间的天数:

LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1528197133157L), ZoneId.systemDefault());
        LocalDate now = LocalDate.now();
        Period between = Period.between(dateTime.toLocalDate(), now);
        int days = between.getDays();
        System.out.println(days);

使用Period 你会发现,天数差不是想要的,是当月的天数差
具体的如下图:
Period 计算日期之间的天数差------ 坑点*****_第1张图片

求两个时间之间的天数差,正确的应该是:

LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1528197133157L), ZoneId.systemDefault());
        LocalDate now = LocalDate.now();
        Period between = Period.between(dateTime.toLocalDate(), now);
        long between1 = ChronoUnit.DAYS.between(dateTime.toLocalDate(), now);
        long between2 = ChronoUnit.MONTHS.between(dateTime.toLocalDate(), now);

ChronoUnit 中有求两个时间之间的天数差,月份差
`public enum ChronoUnit implements TemporalUnit {

/**
 * 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}.
 * 

* 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. *

* 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}. *

* 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}. *

* 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. *

* 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. *

* 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. *

* 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}. *

* 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));

根据自己的需要计算时间

你可能感兴趣的:(API,java8)