本文整理汇总了Java中java.time.calendrical.PeriodUnit.getName方法的典型用法代码示例。如果您正苦于以下问题:Java PeriodUnit.getName方法的具体用法?Java PeriodUnit.getName怎么用?Java PeriodUnit.getName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.calendrical.PeriodUnit
的用法示例。
在下文中一共展示了PeriodUnit.getName方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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);
}
示例3: 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();
}
示例4: 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();
}
示例5: 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);
}
示例6: periodUntil
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
if (endDateTime instanceof YearMonth == false) {
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
YearMonth end = (YearMonth) endDateTime;
if (unit instanceof ChronoUnit) {
long monthsUntil = end.getEpochMonth() - getEpochMonth(); // no overflow
switch ((ChronoUnit) unit) {
case MONTHS:
return monthsUntil;
case QUARTER_YEARS:
return monthsUntil / 3;
case HALF_YEARS:
return monthsUntil / 6;
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 DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.between(this, endDateTime).getAmount();
}
示例7: periodUntil
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
if (endDateTime instanceof Instant == false) {
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
Instant end = (Instant) endDateTime;
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
switch (f) {
case NANOS:
return nanosUntil(end);
case MICROS:
return nanosUntil(end) / 1000;
case MILLIS:
return Jdk8Methods.safeSubtract(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 DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.between(this, endDateTime).getAmount();
}
示例8: plus
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
/**
* Returns a copy of this period with the specified period added.
* <p>
* The specified unit must be one of the supported units from {@link ChronoUnit}, {@code YEARS},
* {@code MONTHS} or {@code DAYS} or be a time unit with an {@link PeriodUnit#isDurationEstimated() exact
* duration}. Other units throw an exception.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amount the amount to add, positive or negative
* @param unit the unit that the amount is expressed in, not null
* @return a {@code Period} based on this period with the requested amount added, not null
* @throws ArithmeticException if numeric overflow occurs
*/
public Period plus(long amount, PeriodUnit unit) {
Jdk7Methods.Objects_requireNonNull(unit, "unit");
if (unit instanceof ChronoUnit) {
if (unit == YEARS || unit == MONTHS || unit == DAYS || unit.isDurationEstimated() == false) {
if (amount == 0) {
return this;
}
switch ((ChronoUnit) unit) {
case NANOS:
return plusNanos(amount);
case MICROS:
return plusNanos(Jdk8Methods.safeMultiply(amount, 1000L));
case MILLIS:
return plusNanos(Jdk8Methods.safeMultiply(amount, 1000000L));
case SECONDS:
return plusSeconds(amount);
case MINUTES:
return plusMinutes(amount);
case HOURS:
return plusHours(amount);
case HALF_DAYS:
return plusNanos(Jdk8Methods.safeMultiply(amount, 12 * NANOS_PER_HOUR));
case DAYS:
return plusDays(amount);
case MONTHS:
return plusMonths(amount);
case YEARS:
return plusYears(amount);
default :
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
}
}
if (unit.isDurationEstimated()) {
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return plusNanos(Duration.of(amount, unit).toNanos());
}
示例9: plus
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
/**
* Returns a copy of this time with the specified period added.
* <p>
* This method returns a new time based on this time with the specified period added. This can be used to
* add any period that is defined by a unit, for example to add hours, minutes or seconds. The unit is
* responsible for the details of the calculation, including the resolution of any edge cases in the
* calculation.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToAdd the amount of the unit to add to the result, may be negative
* @param unit the unit of the period to add, not null
* @return a {@code LocalTime} based on this time with the specified period added, not null
* @throws DateTimeException if the unit cannot be added to this type
*/
@Override
public LocalTime plus(long amountToAdd, PeriodUnit unit) {
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
switch (f) {
case NANOS:
return plusNanos(amountToAdd);
case MICROS:
return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
case MILLIS:
return 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 plusHours((amountToAdd % 2) * 12);
case DAYS:
return this;
}
throw new DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.doPlus(this, amountToAdd);
}
示例10: periodUntil
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
if (endDateTime instanceof LocalTime == false) {
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
LocalTime end = (LocalTime) endDateTime;
if (unit instanceof ChronoUnit) {
long nanosUntil = end.toNanoOfDay() - toNanoOfDay(); // 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();
}
示例11: plus
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
/**
* Returns a copy of this date with the specified period added.
* <p>
* This method returns a new date based on this date with the specified period added. This can be used to
* add any period that is defined by a unit, for example to add years, months or days. The unit is
* responsible for the details of the calculation, including the resolution of any edge cases in the
* calculation.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToAdd the amount of the unit to add to the result, may be negative
* @param unit the unit of the period to add, not null
* @return a {@code LocalDate} based on this date with the specified period added, not null
* @throws DateTimeException if the unit cannot be added to this type
*/
@Override
public LocalDate plus(long amountToAdd, PeriodUnit unit) {
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
switch (f) {
case DAYS:
return plusDays(amountToAdd);
case WEEKS:
return plusWeeks(amountToAdd);
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);
}
示例12: periodUntil
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
if (endDateTime instanceof LocalDate == false) {
throw new DateTimeException("Unable to calculate period between objects of two different types");
}
LocalDate end = (LocalDate) endDateTime;
if (unit instanceof ChronoUnit) {
switch ((ChronoUnit) unit) {
case DAYS:
return daysUntil(end);
case WEEKS:
return daysUntil(end) / 7;
case MONTHS:
return monthsUntil(end);
case QUARTER_YEARS:
return monthsUntil(end) / 3;
case HALF_YEARS:
return monthsUntil(end) / 6;
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 DateTimeException("Unsupported unit: " + unit.getName());
}
return unit.between(this, endDateTime).getAmount();
}
示例13: plus
import java.time.calendrical.PeriodUnit; //导入方法依赖的package包/类
@Override
public ChronoDateImpl<C> plus(long amountToAdd, PeriodUnit unit) {
if (unit instanceof ChronoUnit) {
ChronoUnit f = (ChronoUnit) unit;
switch (f) {
case DAYS:
return plusDays(amountToAdd);
case WEEKS:
return plusDays(Jdk8Methods.safeMultiply(amountToAdd, 7));
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: throw new
// DateTimeException("Unable to add era, standard calendar system only has one era");
// case FOREVER: return (period == 0 ? this : (period > 0 ? LocalDate.MAX_DATE :
// LocalDate.MIN_DATE));
}
throw new DateTimeException(unit.getName() + " not valid for chronology " + getChrono().getId());
}
return (ChronoDateImpl<C>) getChrono().ensureChronoLocalDate(unit.doPlus(this, amountToAdd));
}