Java 类java.time.calendrical.ChronoUnit 实例源码

项目:optashift-employee-rostering    文件:Instant.java   
@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);
}
项目:optashift-employee-rostering    文件:LocalTime.java   
/**
 * 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);
}
项目:optashift-employee-rostering    文件:ChronoDateTimeImpl.java   
@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));
}
项目:optashift-employee-rostering    文件:ChronoDateTimeImpl.java   
private ChronoDateTimeImpl<C> plusWithOverflow(ChronoLocalDate<C> newDate, long hours, long minutes, long seconds,
    long nanos) {

  // 9223372036854775808 long, 2147483648 int
  if ((hours | minutes | seconds | nanos) == 0) {
    return with(newDate, this.time);
  }
  long totDays = nanos / NANOS_PER_DAY + // max/24*60*60*1B
      seconds / SECONDS_PER_DAY + // max/24*60*60
      minutes / MINUTES_PER_DAY + // max/24*60
      hours / HOURS_PER_DAY; // max/24
  long totNanos = nanos % NANOS_PER_DAY + // max 86400000000000
      (seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND + // max 86400000000000
      (minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE + // max 86400000000000
      (hours % HOURS_PER_DAY) * NANOS_PER_HOUR; // max 86400000000000
  long curNoD = this.time.toNanoOfDay(); // max 86400000000000
  totNanos = totNanos + curNoD; // total 432000000000000
  totDays += Jdk8Methods.floorDiv(totNanos, NANOS_PER_DAY);
  long newNoD = Jdk8Methods.floorMod(totNanos, NANOS_PER_DAY);
  LocalTime newTime = (newNoD == curNoD ? this.time : LocalTime.ofNanoOfDay(newNoD));
  return with(newDate.plus(totDays, ChronoUnit.DAYS), newTime);
}
项目:optashift-employee-rostering    文件:ChronoZonedDateTimeImpl.java   
@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();
}
项目:optashift-employee-rostering    文件:Year.java   
@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);
}
项目:optashift-employee-rostering    文件:Year.java   
@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();
}
项目:gwt-time    文件:Instant.java   
@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);
}
项目:gwt-time    文件:LocalTime.java   
/**
 * 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);
}
项目:gwt-time    文件:ChronoDateTimeImpl.java   
@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));
}
项目:gwt-time    文件:ChronoDateTimeImpl.java   
private ChronoDateTimeImpl<C> plusWithOverflow(ChronoLocalDate<C> newDate, long hours, long minutes, long seconds,
    long nanos) {

  // 9223372036854775808 long, 2147483648 int
  if ((hours | minutes | seconds | nanos) == 0) {
    return with(newDate, this.time);
  }
  long totDays = nanos / NANOS_PER_DAY + // max/24*60*60*1B
      seconds / SECONDS_PER_DAY + // max/24*60*60
      minutes / MINUTES_PER_DAY + // max/24*60
      hours / HOURS_PER_DAY; // max/24
  long totNanos = nanos % NANOS_PER_DAY + // max 86400000000000
      (seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND + // max 86400000000000
      (minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE + // max 86400000000000
      (hours % HOURS_PER_DAY) * NANOS_PER_HOUR; // max 86400000000000
  long curNoD = this.time.toNanoOfDay(); // max 86400000000000
  totNanos = totNanos + curNoD; // total 432000000000000
  totDays += Jdk8Methods.floorDiv(totNanos, NANOS_PER_DAY);
  long newNoD = Jdk8Methods.floorMod(totNanos, NANOS_PER_DAY);
  LocalTime newTime = (newNoD == curNoD ? this.time : LocalTime.ofNanoOfDay(newNoD));
  return with(newDate.plus(totDays, ChronoUnit.DAYS), newTime);
}
项目:java8-backports    文件:UsabilityChrono.java   
/**
 * Print a month calendar with complete week rows.
 * 
 * @param date A date in some calendar
 * @param out a PrintStream
 */
private static <C extends Chrono<C>> void printMonthCal(ChronoLocalDate<C> date, PrintStream out) {

  int lengthOfMonth = date.lengthOfMonth();
  ChronoLocalDate<C> end = date.with(ChronoField.DAY_OF_MONTH, lengthOfMonth);
  end = end.plus(7 - end.get(ChronoField.DAY_OF_WEEK), ChronoUnit.DAYS);
  // Back up to the beginning of the week including the 1st of the month
  ChronoLocalDate<C> start = date.with(ChronoField.DAY_OF_MONTH, 1);
  start = start.minus(start.get(ChronoField.DAY_OF_WEEK), ChronoUnit.DAYS);

  out.printf("%9s Month %2d, %4d%n", date.getChrono().getId(), date.get(ChronoField.MONTH_OF_YEAR),
      date.get(ChronoField.YEAR));
  String[] colText = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
  printMonthRow(colText, " ", out);

  String[] cell = new String[7];
  for (; start.compareTo(end) <= 0; start = start.plus(1, ChronoUnit.DAYS)) {
    int ndx = start.get(ChronoField.DAY_OF_WEEK) - 1;
    cell[ndx] = Integer.toString(start.get(ChronoField.DAY_OF_MONTH));
    if (ndx == 6) {
      printMonthRow(cell, "|", out);
    }
  }
}
项目:gwt-time    文件:Year.java   
@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);
}
项目:gwt-time    文件:Year.java   
@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();
}
项目:java8-backports    文件:TestMinguoChrono.java   
@SuppressWarnings("unused")
@Test(dataProvider = "samples", groups = { "implementation" })
public void test_MinguoDate(ChronoLocalDate<MinguoChrono> minguoDate, LocalDate iso) {

  ChronoLocalDate<MinguoChrono> hd = minguoDate;
  ChronoLocalDateTime<MinguoChrono> hdt = hd.atTime(LocalTime.NOON);
  ZoneOffset zo = ZoneOffset.ofHours(1);
  ChronoZonedDateTime<MinguoChrono> hzdt = hdt.atZone(zo);
  hdt = hdt.plus(1, ChronoUnit.YEARS);
  hdt = hdt.plus(1, ChronoUnit.MONTHS);
  hdt = hdt.plus(1, ChronoUnit.DAYS);
  hdt = hdt.plus(1, ChronoUnit.HOURS);
  hdt = hdt.plus(1, ChronoUnit.MINUTES);
  hdt = hdt.plus(1, ChronoUnit.SECONDS);
  hdt = hdt.plus(1, ChronoUnit.NANOS);
  ChronoLocalDateTime<MinguoChrono> a2 = hzdt.getDateTime();
  ChronoLocalDate<MinguoChrono> a3 = a2.getDate();
  ChronoLocalDate<MinguoChrono> a5 = hzdt.getDate();
  // System.out.printf(" d: %s, dt: %s; odt: %s; zodt: %s; a4: %s%n", date, hdt, hodt, hzdt, a5);
}
项目:java8-backports    文件:Instant.java   
@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);
}
项目:java8-backports    文件:LocalTime.java   
/**
 * 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);
}
项目:java8-backports    文件:ChronoDateTimeImpl.java   
@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));
}
项目:java8-backports    文件:ChronoDateTimeImpl.java   
private ChronoDateTimeImpl<C> plusWithOverflow(ChronoLocalDate<C> newDate, long hours, long minutes, long seconds,
    long nanos) {

  // 9223372036854775808 long, 2147483648 int
  if ((hours | minutes | seconds | nanos) == 0) {
    return with(newDate, this.time);
  }
  long totDays = nanos / NANOS_PER_DAY + // max/24*60*60*1B
      seconds / SECONDS_PER_DAY + // max/24*60*60
      minutes / MINUTES_PER_DAY + // max/24*60
      hours / HOURS_PER_DAY; // max/24
  long totNanos = nanos % NANOS_PER_DAY + // max 86400000000000
      (seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND + // max 86400000000000
      (minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE + // max 86400000000000
      (hours % HOURS_PER_DAY) * NANOS_PER_HOUR; // max 86400000000000
  long curNoD = this.time.toNanoOfDay(); // max 86400000000000
  totNanos = totNanos + curNoD; // total 432000000000000
  totDays += Jdk8Methods.floorDiv(totNanos, NANOS_PER_DAY);
  long newNoD = Jdk8Methods.floorMod(totNanos, NANOS_PER_DAY);
  LocalTime newTime = (newNoD == curNoD ? this.time : LocalTime.ofNanoOfDay(newNoD));
  return with(newDate.plus(totDays, ChronoUnit.DAYS), newTime);
}
项目:java8-backports    文件:ChronoZonedDateTimeImpl.java   
@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();
}
项目:java8-backports    文件:Year.java   
@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);
}
项目:java8-backports    文件:UsabilityBasic.java   
private static void period() {

    LocalDate date1 = LocalDate.now();
    LocalDate date2 = LocalDate.now().plusDays(25367);
    System.out.println(ChronoUnit.DAYS.between(date1, date2));
    System.out.println(ChronoUnit.YEARS.between(date1, date2));

    date1 = LocalDate.of(2012, 2, 20);
    date2 = LocalDate.of(2014, 2, 19);
    System.out.println(ChronoUnit.YEARS.between(date1, date2));
    date2 = LocalDate.of(2014, 2, 20);
    System.out.println(ChronoUnit.YEARS.between(date1, date2));
    date2 = LocalDate.of(2014, 2, 21);
    System.out.println(ChronoUnit.YEARS.between(date1, date2));
    date2 = LocalDate.of(2010, 2, 19);
    System.out.println(ChronoUnit.YEARS.between(date1, date2));
    date2 = LocalDate.of(2010, 2, 20);
    System.out.println(ChronoUnit.YEARS.between(date1, date2));
    date2 = LocalDate.of(2010, 2, 21);
    System.out.println(ChronoUnit.YEARS.between(date1, date2));

    LocalDate date3 = LocalDate.now().plus(3, ChronoUnit.DAYS);
    System.out.println("3 days later " + date3);
  }
项目:java8-backports    文件:UsabilityChrono.java   
private static void newPackagePluggable() {

    Chrono<?> chrono = MinguoChrono.INSTANCE;

    ChronoLocalDate<?> date = chrono.dateNow();
    System.out.printf("now: %s%n", date);

    date = date.with(DAY_OF_MONTH, 1);
    System.out.printf("first of month: %s%n", date);

    int month = date.get(ChronoField.MONTH_OF_YEAR);
    date = date.with(DAY_OF_WEEK, 1);
    System.out.printf("start of first week: %s%n", date);

    while (date.get(ChronoField.MONTH_OF_YEAR) <= month) {
      String row = "";
      for (int i = 0; i < 7; i++) {
        row += date.get(ChronoField.DAY_OF_MONTH) + " ";
        date = date.plus(1, ChronoUnit.DAYS);
      }
      System.out.println(row);
    }
  }
项目:optashift-employee-rostering    文件:OffsetTime.java   
@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();
}
项目:optashift-employee-rostering    文件:YearMonth.java   
@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);
}
项目:optashift-employee-rostering    文件:YearMonth.java   
@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();
}
项目:optashift-employee-rostering    文件:Instant.java   
@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();
}
项目:optashift-employee-rostering    文件:OffsetDateTime.java   
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {

  if (endDateTime instanceof OffsetDateTime == false) {
    throw new DateTimeException("Unable to calculate period between objects of two different types");
  }
  if (unit instanceof ChronoUnit) {
    OffsetDateTime end = (OffsetDateTime) endDateTime;
    end = end.withOffsetSameInstant(this.offset);
    return this.dateTime.periodUntil(end.dateTime, unit);
  }
  return unit.between(this, endDateTime).getAmount();
}
项目:optashift-employee-rostering    文件:Period.java   
/**
 * 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());
}
项目:optashift-employee-rostering    文件:Duration.java   
/**
 * Returns a copy of this duration with the specified duration added.
 * <p>
 * The duration amount is measured in terms of the specified unit. Only a subset of units are accepted by
 * this method. The unit must either have an {@link PeriodUnit#isDurationEstimated() exact duration} or be
 * {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 * 
 * @param amountToAdd the amount of the period, measured in terms of the unit, positive or negative
 * @param unit the unit that the period is measured in, must have an exact duration, not null
 * @return a {@code Duration} based on this duration with the specified duration added, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
public Duration plus(long amountToAdd, PeriodUnit unit) {

  Jdk7Methods.Objects_requireNonNull(unit, "unit");
  if (unit == DAYS) {
    return plus(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY), 0);
  }
  if (unit.isDurationEstimated()) {
    throw new DateTimeException("Unit must not have an estimated duration");
  }
  if (amountToAdd == 0) {
    return this;
  }
  if (unit instanceof ChronoUnit) {
    switch ((ChronoUnit) unit) {
      case NANOS:
        return plusNanos(amountToAdd);
      case MICROS:
        return plusSeconds((amountToAdd / (1000000L * 1000)) * 1000).plusNanos(
            (amountToAdd % (1000000L * 1000)) * 1000);
      case MILLIS:
        return plusMillis(amountToAdd);
      case SECONDS:
        return plusSeconds(amountToAdd);
    }
    return plusSeconds(Jdk8Methods.safeMultiply(unit.getDuration().seconds, amountToAdd));
  }
  Duration duration = unit.getDuration().multipliedBy(amountToAdd);
  return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
}
项目:optashift-employee-rostering    文件:LocalTime.java   
/**
 * 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);
}
项目:optashift-employee-rostering    文件:LocalTime.java   
@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();
}
项目:optashift-employee-rostering    文件:LocalDate.java   
/**
 * 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);
}
项目:optashift-employee-rostering    文件:LocalDate.java   
@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();
}
项目:optashift-employee-rostering    文件:LocalDateTime.java   
/**
 * Returns a copy of this date-time with the specified period added.
 * <p>
 * This method returns a new date-time based on this date-time 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 LocalDateTime} based on this date-time with the specified period added, not null
 * @throws DateTimeException if the unit cannot be added to this type
 */
@Override
public LocalDateTime 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 unit.doPlus(this, amountToAdd);
}
项目:optashift-employee-rostering    文件:ZonedDateTime.java   
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {

  if (endDateTime instanceof ZonedDateTime == false) {
    throw new DateTimeException("Unable to calculate period between objects of two different types");
  }
  if (unit instanceof ChronoUnit) {
    ZonedDateTime end = (ZonedDateTime) endDateTime;
    end = end.withZoneSameInstant(this.offset);
    return this.dateTime.periodUntil(end.dateTime, unit);
  }
  return unit.between(this, endDateTime).getAmount();
}
项目:optashift-employee-rostering    文件:ChronoZonedDateTimeImpl.java   
@Override
public ChronoZonedDateTime<C> plus(long amountToAdd, PeriodUnit unit) {

  if (unit instanceof ChronoUnit) {
    return with(this.dateTime.plus(amountToAdd, unit));
  }
  return getDate().getChrono().ensureChronoZonedDateTime(unit.doPlus(this, amountToAdd)); // / TODO:
                                                                                          // Generics
                                                                                          // replacement
                                                                                          // Risk!
}
项目:optashift-employee-rostering    文件:OffsetDate.java   
@Override
public long periodUntil(DateTime endDateTime, PeriodUnit unit) {

  if (endDateTime instanceof OffsetDate == false) {
    throw new DateTimeException("Unable to calculate period between objects of two different types");
  }
  if (unit instanceof ChronoUnit) {
    OffsetDate end = (OffsetDate) endDateTime;
    long offsetDiff = end.offset.getTotalSeconds() - this.offset.getTotalSeconds();
    LocalDate endLocal = end.date.plusDays(Jdk8Methods.floorDiv(-offsetDiff, SECONDS_PER_DAY));
    return this.date.periodUntil(endLocal, unit);
  }
  return unit.between(this, endDateTime).getAmount();
}
项目:gwt-time    文件:OffsetTime.java   
@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();
}
项目:gwt-time    文件:YearMonth.java   
@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);
}