当前位置: 首页>>代码示例>>Java>>正文


Java TemporalUnit.between方法代码示例

本文整理汇总了Java中java.time.temporal.TemporalUnit.between方法的典型用法代码示例。如果您正苦于以下问题:Java TemporalUnit.between方法的具体用法?Java TemporalUnit.between怎么用?Java TemporalUnit.between使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.time.temporal.TemporalUnit的用法示例。


在下文中一共展示了TemporalUnit.between方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    Objects.requireNonNull(endExclusive, "endExclusive");
    ChronoLocalDate end = getChronology().date(endExclusive);
    if (unit instanceof ChronoUnit) {
        switch ((ChronoUnit) unit) {
            case DAYS: return daysUntil(end);
            case WEEKS: return daysUntil(end) / 7;
            case MONTHS: return monthsUntil(end);
            case YEARS: return monthsUntil(end) / 12;
            case DECADES: return monthsUntil(end) / 120;
            case CENTURIES: return monthsUntil(end) / 1200;
            case MILLENNIA: return monthsUntil(end) / 12000;
            case ERAS: return end.getLong(ERA) - getLong(ERA);
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    Objects.requireNonNull(unit, "unit");
    return unit.between(this, end);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:21,代码来源:ChronoLocalDateImpl.java

示例2: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    CopticDate end = getChronology().date(endExclusive);
    if (unit instanceof ChronoUnit) {
        return LocalDate.from(this).until(end, unit);  // TODO: this is wrong
    }
    return unit.between(this, end);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:CopticDate.java

示例3: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    Objects.requireNonNull(endExclusive, "endExclusive");
    @SuppressWarnings("unchecked")
    ChronoZonedDateTime<D> end = (ChronoZonedDateTime<D>) getChronology().zonedDateTime(endExclusive);
    if (unit instanceof ChronoUnit) {
        end = end.withZoneSameInstant(offset);
        return dateTime.until(end.toLocalDateTime(), unit);
    }
    Objects.requireNonNull(unit, "unit");
    return unit.between(this, end);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:ChronoZonedDateTimeImpl.java

示例4: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Calculates the amount of time until another year-month in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code YearMonth}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified year-month.
 * The result will be negative if the end is before the start.
 * The {@code Temporal} passed to this method is converted to a
 * {@code YearMonth} using {@link #from(TemporalAccessor)}.
 * For example, the period in years between two year-months can be calculated
 * using {@code startYearMonth.until(endYearMonth, YEARS)}.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two year-months.
 * For example, the period in decades between 2012-06 and 2032-05
 * will only be one decade as it is one month short of two decades.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, MONTHS);
 *   amount = MONTHS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code MONTHS}, {@code YEARS}, {@code DECADES},
 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to a {@code YearMonth}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this year-month and the end year-month
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to a {@code YearMonth}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    YearMonth end = YearMonth.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        long monthsUntil = end.getProlepticMonth() - getProlepticMonth();  // no overflow
        switch ((ChronoUnit) unit) {
            case MONTHS: return monthsUntil;
            case YEARS: return monthsUntil / 12;
            case DECADES: return monthsUntil / 120;
            case CENTURIES: return monthsUntil / 1200;
            case MILLENNIA: return monthsUntil / 12000;
            case ERAS: return end.getLong(ERA) - getLong(ERA);
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return unit.between(this, end);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:65,代码来源:YearMonth.java

示例5: test_until_TemporalUnit_between

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(OffsetDateTime odt1, OffsetDateTime odt2, TemporalUnit unit, long expected) {
    long amount = unit.between(odt1, odt2);
    assertEquals(amount, expected);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKOffsetDateTime.java

示例6: test_until_TemporalUnit_between

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(LocalTime time1, LocalTime time2, TemporalUnit unit, long expected) {
    long amount = unit.between(time1, time2);
    assertEquals(amount, expected);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKLocalTime.java

示例7: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Calculates the amount of time until another year in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code Year}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified year.
 * The result will be negative if the end is before the start.
 * The {@code Temporal} passed to this method is converted to a
 * {@code Year} using {@link #from(TemporalAccessor)}.
 * For example, the period in decades between two year can be calculated
 * using {@code startYear.until(endYear, DECADES)}.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two years.
 * For example, the period in decades between 2012 and 2031
 * will only be one decade as it is one year short of two decades.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, YEARS);
 *   amount = YEARS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code YEARS}, {@code DECADES}, {@code CENTURIES},
 * {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to a {@code Year}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this year and the end year
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to a {@code Year}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    Year end = Year.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        long yearsUntil = ((long) end.year) - year;  // no overflow
        switch ((ChronoUnit) unit) {
            case YEARS: return yearsUntil;
            case DECADES: return yearsUntil / 10;
            case CENTURIES: return yearsUntil / 100;
            case MILLENNIA: return yearsUntil / 1000;
            case ERAS: return end.getLong(ERA) - getLong(ERA);
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return unit.between(this, end);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:64,代码来源:Year.java

示例8: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Calculates the amount of time until another year-month in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code YearMonth}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified year-month.
 * The result will be negative if the end is before the start.
 * The {@code Temporal} passed to this method is converted to a
 * {@code YearMonth} using {@link #from(TemporalAccessor)}.
 * For example, the amount in years between two year-months can be calculated
 * using {@code startYearMonth.until(endYearMonth, YEARS)}.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two year-months.
 * For example, the amount in decades between 2012-06 and 2032-05
 * will only be one decade as it is one month short of two decades.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, MONTHS);
 *   amount = MONTHS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code MONTHS}, {@code YEARS}, {@code DECADES},
 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to a {@code YearMonth}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this year-month and the end year-month
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to a {@code YearMonth}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    YearMonth end = YearMonth.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        long monthsUntil = end.getProlepticMonth() - getProlepticMonth();  // no overflow
        switch ((ChronoUnit) unit) {
            case MONTHS: return monthsUntil;
            case YEARS: return monthsUntil / 12;
            case DECADES: return monthsUntil / 120;
            case CENTURIES: return monthsUntil / 1200;
            case MILLENNIA: return monthsUntil / 12000;
            case ERAS: return end.getLong(ERA) - getLong(ERA);
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return unit.between(this, end);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:65,代码来源:YearMonth.java

示例9: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Calculates the amount of time until another instant in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code Instant}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified instant.
 * The result will be negative if the end is before the start.
 * The calculation returns a whole number, representing the number of
 * complete units between the two instants.
 * The {@code Temporal} passed to this method is converted to a
 * {@code Instant} using {@link #from(TemporalAccessor)}.
 * For example, the amount in days between two dates can be calculated
 * using {@code startInstant.until(endInstant, SECONDS)}.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, SECONDS);
 *   amount = SECONDS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS}
 * are supported. Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to an {@code Instant}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this instant and the end instant
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to an {@code Instant}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    Instant end = Instant.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case NANOS: return nanosUntil(end);
            case MICROS: return nanosUntil(end) / 1000;
            case MILLIS: return Math.subtractExact(end.toEpochMilli(), toEpochMilli());
            case SECONDS: return secondsUntil(end);
            case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE;
            case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR;
            case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR);
            case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY);
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return unit.between(this, end);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:64,代码来源:Instant.java

示例10: test_until_TemporalUnit_between

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(YearMonth ym1, YearMonth ym2, TemporalUnit unit, long expected) {
    long amount = unit.between(ym1, ym2);
    assertEquals(amount, expected);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TCKYearMonth.java

示例11: test_until_TemporalUnit_between

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = unit.between(dt1, dt2);
    assertEquals(amount, expected);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TCKLocalDateTime.java

示例12: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Calculates the amount of time until another instant in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code Instant}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified instant.
 * The result will be negative if the end is before the start.
 * The calculation returns a whole number, representing the number of
 * complete units between the two instants.
 * The {@code Temporal} passed to this method is converted to a
 * {@code Instant} using {@link #from(TemporalAccessor)}.
 * For example, the amount in seconds between two dates can be calculated
 * using {@code startInstant.until(endInstant, SECONDS)}.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, SECONDS);
 *   amount = SECONDS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS}
 * are supported. Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to an {@code Instant}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this instant and the end instant
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to an {@code Instant}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    Instant end = Instant.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case NANOS: return nanosUntil(end);
            case MICROS: return nanosUntil(end) / 1000;
            case MILLIS: return Math.subtractExact(end.toEpochMilli(), toEpochMilli());
            case SECONDS: return secondsUntil(end);
            case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE;
            case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR;
            case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR);
            case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY);
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return unit.between(this, end);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:64,代码来源:Instant.java

示例13: test_until_TemporalUnit_between

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(Year year1, Year year2, TemporalUnit unit, long expected) {
    long amount = unit.between(year1, year2);
    assertEquals(amount, expected);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKYear.java

示例14: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Calculates the amount of time until another date-time in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code OffsetDateTime}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified date-time.
 * The result will be negative if the end is before the start.
 * For example, the period in days between two date-times can be calculated
 * using {@code startDateTime.until(endDateTime, DAYS)}.
 * <p>
 * The {@code Temporal} passed to this method is converted to a
 * {@code OffsetDateTime} using {@link #from(TemporalAccessor)}.
 * If the offset differs between the two date-times, the specified
 * end date-time is normalized to have the same offset as this date-time.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two date-times.
 * For example, the period in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z
 * will only be one month as it is one minute short of two months.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, MONTHS);
 *   amount = MONTHS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to an {@code OffsetDateTime}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this date-time and the end date-time
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to an {@code OffsetDateTime}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    OffsetDateTime end = OffsetDateTime.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        end = end.withOffsetSameInstant(offset);
        return dateTime.until(end.dateTime, unit);
    }
    return unit.between(this, end);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:62,代码来源:OffsetDateTime.java

示例15: until

import java.time.temporal.TemporalUnit; //导入方法依赖的package包/类
/**
 * Calculates the amount of time until another date-time in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code ZonedDateTime}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified date-time.
 * The result will be negative if the end is before the start.
 * For example, the period in days between two date-times can be calculated
 * using {@code startDateTime.until(endDateTime, DAYS)}.
 * <p>
 * The {@code Temporal} passed to this method is converted to a
 * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}.
 * If the time-zone differs between the two zoned date-times, the specified
 * end date-time is normalized to have the same zone as this date-time.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two date-times.
 * For example, the period in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z
 * will only be one month as it is one minute short of two months.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, MONTHS);
 *   amount = MONTHS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * The calculation for date and time units differ.
 * <p>
 * Date units operate on the local time-line, using the local date-time.
 * For example, the period from noon on day 1 to noon the following day
 * in days will always be counted as exactly one day, irrespective of whether
 * there was a daylight savings change or not.
 * <p>
 * Time units operate on the instant time-line.
 * The calculation effectively converts both zoned date-times to instants
 * and then calculates the period between the instants.
 * For example, the period from noon on day 1 to noon the following day
 * in hours may be 23, 24 or 25 hours (or some other amount) depending on
 * whether there was a daylight savings change or not.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this date-time and the end date-time
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to a {@code ZonedDateTime}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    ZonedDateTime end = ZonedDateTime.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        end = end.withZoneSameInstant(zone);
        if (unit.isDateBased()) {
            return dateTime.until(end.dateTime, unit);
        } else {
            return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
        }
    }
    return unit.between(this, end);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:80,代码来源:ZonedDateTime.java


注:本文中的java.time.temporal.TemporalUnit.between方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。