本文整理汇总了Java中java.time.calendrical.PeriodUnit类的典型用法代码示例。如果您正苦于以下问题:Java PeriodUnit类的具体用法?Java PeriodUnit怎么用?Java PeriodUnit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PeriodUnit类属于java.time.calendrical包,在下文中一共展示了PeriodUnit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: plus
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Override
public Instant plus(long amountToAdd, PeriodUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case NANOS:
return plusNanos(amountToAdd);
case MICROS:
return plus(amountToAdd / 1000000, (amountToAdd % 1000000) * 1000);
case MILLIS:
return plusMillis(amountToAdd);
case SECONDS:
return plusSeconds(amountToAdd);
case MINUTES:
return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_MINUTE));
case HOURS:
return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_HOUR));
case HALF_DAYS:
return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY / 2));
case DAYS:
return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY));
}
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.doPlus(this, amountToAdd);
}
示例2: truncatedTo
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
/**
* Returns a copy of this {@code LocalTime} with the time truncated.
* <p>
* Truncating the time returns a copy of the original time with fields smaller than the specified unit set
* to zero. For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit will set the
* second-of-minute and nano-of-second field to zero.
* <p>
* Not all units are accepted. The {@link ChronoUnit#DAYS days} unit and time units with an exact duration
* can be used, other units throw an exception.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param unit the unit to truncate to, not null
* @return a {@code LocalTime} based on this time with the time truncated, not null
* @throws DateTimeException if unable to truncate
*/
public LocalTime truncatedTo(PeriodUnit unit) {
if (unit == ChronoUnit.NANOS) {
return this;
} else if (unit == ChronoUnit.DAYS) {
return MIDNIGHT;
} else if (unit.isDurationEstimated()) {
throw new DateTimeException("Unit must not have an estimated duration");
}
long nod = toNanoOfDay();
long dur = unit.getDuration().toNanos();
if (dur >= NANOS_PER_DAY) {
throw new DateTimeException("Unit must not be a date unit");
}
nod = (nod / dur) * dur;
return ofNanoOfDay(nod);
}
示例3: plus
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Override
public ChronoDateTimeImpl<C> plus(long amountToAdd, PeriodUnit unit) {
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
switch (f) {
case NANOS:
return plusNanos(amountToAdd);
case MICROS:
return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS:
return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000);
case SECONDS:
return plusSeconds(amountToAdd);
case MINUTES:
return plusMinutes(amountToAdd);
case HOURS:
return plusHours(amountToAdd);
case HALF_DAYS:
return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is
// multiple of 2)
}
return with(this.date.plus(amountToAdd, unit), this.time);
}
return this.date.getChrono().ensureChronoLocalDateTime(unit.doPlus(this, amountToAdd));
}
示例4: periodUntil
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
if (endDateTime instanceof ChronoZonedDateTime == false) {
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
@SuppressWarnings("unchecked")
ChronoZonedDateTime<C> end = (ChronoZonedDateTime<C>) endDateTime;
if (getDate().getChrono().equals(end.getDate().getChrono()) == false) {
throw new DateTimeException("Unable to calculate period between two different chronologies");
}
if (unit instanceof ChronoUnit) {
end = end.withZoneSameInstant(this.offset);
return this.dateTime.periodUntil(end.getDateTime(), unit);
}
return unit.between(this, endDateTime).getAmount();
}
示例5: plus
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Override
public Year plus(long amountToAdd, PeriodUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case YEARS:
return plusYears(amountToAdd);
case DECADES:
return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10));
case CENTURIES:
return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100));
case MILLENNIA:
return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000));
case ERAS:
return with(ERA, Jdk8Methods.safeAdd(getLong(ERA), amountToAdd));
}
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.doPlus(this, amountToAdd);
}
示例6: periodUntil
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
if (endDateTime instanceof Year == false) {
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
Year end = (Year) endDateTime;
if (unit instanceof ChronoUnit) {
long yearsUntil = ((long) end.year) - this.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 DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.between(this, endDateTime).getAmount();
}
示例7: test_badPlusPeriodUnitChrono
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Test(groups = { "tck" }, dataProvider = "calendars")
public void test_badPlusPeriodUnitChrono(Chrono chrono) {
LocalDate refDate = LocalDate.of(1900, 1, 1);
ChronoLocalDate date = chrono.date(refDate);
for (Chrono[] clist : data_of_calendars()) {
Chrono chrono2 = clist[0];
ChronoLocalDate<?> date2 = chrono2.date(refDate);
PeriodUnit adjuster = new FixedPeriodUnit(date2);
if (chrono != chrono2) {
try {
ChronoLocalDate<?> notreached = date.plus(1, adjuster);
Assert.fail("PeriodUnit.doPlus plus should have thrown a ClassCastException" + date.getClass()
+ ", can not be cast to " + date2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDate<?> result = date.plus(1, adjuster);
assertEquals(result, date2, "WithAdjuster failed to replace date");
}
}
}
示例8: test_badMinusPeriodUnitChrono
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Test(groups = { "tck" }, dataProvider = "calendars")
public void test_badMinusPeriodUnitChrono(Chrono chrono) {
LocalDate refDate = LocalDate.of(1900, 1, 1);
ChronoLocalDate date = chrono.date(refDate);
for (Chrono[] clist : data_of_calendars()) {
Chrono chrono2 = clist[0];
ChronoLocalDate<?> date2 = chrono2.date(refDate);
PeriodUnit adjuster = new FixedPeriodUnit(date2);
if (chrono != chrono2) {
try {
ChronoLocalDate<?> notreached = date.minus(1, adjuster);
Assert.fail("PeriodUnit.doAdd minus should have thrown a ClassCastException" + date.getClass()
+ ", can not be cast to " + date2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDate<?> result = date.minus(1, adjuster);
assertEquals(result, date2, "WithAdjuster failed to replace date");
}
}
}
示例9: test_badPlusPeriodUnitChrono
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Test(groups = { "tck" }, dataProvider = "calendars")
public void test_badPlusPeriodUnitChrono(Chrono chrono) {
LocalDate refDate = LocalDate.of(1900, 1, 1);
ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
for (Chrono[] clist : data_of_calendars()) {
Chrono chrono2 = clist[0];
ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
PeriodUnit adjuster = new FixedPeriodUnit(cdt2);
if (chrono != chrono2) {
try {
ChronoLocalDateTime<?> notreached = cdt.plus(1, adjuster);
Assert.fail("PeriodUnit.doPlus plus should have thrown a ClassCastException" + cdt + ", can not be cast to "
+ cdt2);
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDateTime<?> result = cdt.plus(1, adjuster);
assertEquals(result, cdt2, "WithAdjuster failed to replace date");
}
}
}
示例10: test_badPlusPeriodUnitChrono
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Test(groups = { "tck" }, dataProvider = "calendars")
public void test_badPlusPeriodUnitChrono(Chrono chrono) {
LocalDate refDate = LocalDate.of(1900, 1, 1);
ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
for (Chrono[] clist : data_of_calendars()) {
Chrono chrono2 = clist[0];
ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
PeriodUnit adjuster = new FixedPeriodUnit(cdt2);
if (chrono != chrono2) {
try {
ChronoLocalDateTime<?> notreached = cdt.plus(1, adjuster);
Assert.fail("PeriodUnit.doAdd plus should have thrown a ClassCastException" + cdt + ", can not be cast to "
+ cdt2);
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDateTime<?> result = cdt.plus(1, adjuster);
assertEquals(result, cdt2, "WithAdjuster failed to replace date");
}
}
}
示例11: test_badPlusPeriodUnitChrono
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Test(groups = { "tck" }, dataProvider = "calendars")
public void test_badPlusPeriodUnitChrono(Chrono chrono) {
LocalDate refDate = LocalDate.of(1900, 1, 1);
ChronoLocalDate date = chrono.date(refDate);
for (Chrono[] clist : data_of_calendars()) {
Chrono chrono2 = clist[0];
ChronoLocalDate<?> date2 = chrono2.date(refDate);
PeriodUnit adjuster = new FixedPeriodUnit(date2);
if (chrono != chrono2) {
try {
ChronoLocalDate<?> notreached = date.plus(1, adjuster);
Assert.fail("PeriodUnit.doAdd plus should have thrown a ClassCastException" + date.getClass()
+ ", can not be cast to " + date2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDate<?> result = date.plus(1, adjuster);
assertEquals(result, date2, "WithAdjuster failed to replace date");
}
}
}
示例12: test_badMinusPeriodUnitChrono
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Test(groups = { "tck" }, dataProvider = "calendars")
public void test_badMinusPeriodUnitChrono(Chrono chrono) {
LocalDate refDate = LocalDate.of(1900, 1, 1);
ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
for (Chrono[] clist : data_of_calendars()) {
Chrono chrono2 = clist[0];
ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
PeriodUnit adjuster = new FixedPeriodUnit(cdt2);
if (chrono != chrono2) {
try {
ChronoLocalDateTime<?> notreached = cdt.minus(1, adjuster);
Assert.fail("PeriodUnit.doPlus minus should have thrown a ClassCastException" + cdt.getClass()
+ ", can not be cast to " + cdt2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDateTime<?> result = cdt.minus(1, adjuster);
assertEquals(result, cdt2, "WithAdjuster failed to replace date");
}
}
}
示例13: test_badMinusPeriodUnitChrono
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Test(groups = { "tck" }, dataProvider = "calendars")
public void test_badMinusPeriodUnitChrono(Chrono chrono) {
LocalDate refDate = LocalDate.of(1900, 1, 1);
ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON);
for (Chrono[] clist : data_of_calendars()) {
Chrono chrono2 = clist[0];
ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
PeriodUnit adjuster = new FixedPeriodUnit(cdt2);
if (chrono != chrono2) {
try {
ChronoLocalDateTime<?> notreached = cdt.minus(1, adjuster);
Assert.fail("PeriodUnit.doAdd minus should have thrown a ClassCastException" + cdt.getClass()
+ ", can not be cast to " + cdt2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDateTime<?> result = cdt.minus(1, adjuster);
assertEquals(result, cdt2, "WithAdjuster failed to replace date");
}
}
}
示例14: periodUntil
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
if (endDateTime instanceof OffsetTime == false) {
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
if (unit instanceof ChronoUnit) {
OffsetTime end = (OffsetTime) endDateTime;
long nanosUntil = end.toEpochNano() - toEpochNano(); // no overflow
switch ((ChronoUnit) unit) {
case NANOS:
return nanosUntil;
case MICROS:
return nanosUntil / 1000;
case MILLIS:
return nanosUntil / 1000000;
case SECONDS:
return nanosUntil / NANOS_PER_SECOND;
case MINUTES:
return nanosUntil / NANOS_PER_MINUTE;
case HOURS:
return nanosUntil / NANOS_PER_HOUR;
case HALF_DAYS:
return nanosUntil / (12 * NANOS_PER_HOUR);
}
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.between(this, endDateTime).getAmount();
}
示例15: plus
import java.time.calendrical.PeriodUnit; //导入依赖的package包/类
@Override
public YearMonth plus(long amountToAdd, PeriodUnit unit) {
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case MONTHS:
return plusMonths(amountToAdd);
case QUARTER_YEARS:
return plusYears(amountToAdd / 256).plusMonths((amountToAdd % 256) * 3); // no overflow (256 is
// multiple of 4)
case HALF_YEARS:
return plusYears(amountToAdd / 256).plusMonths((amountToAdd % 256) * 6); // no overflow (256 is
// multiple of 2)
case YEARS:
return plusYears(amountToAdd);
case DECADES:
return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10));
case CENTURIES:
return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100));
case MILLENNIA:
return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000));
case ERAS:
return with(ERA, Jdk8Methods.safeAdd(getLong(ERA), amountToAdd));
}
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.doPlus(this, amountToAdd);
}