Java Code Examples for java.time.Period#between()

The following examples show how to use java.time.Period#between() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalculateDateTimeDifference.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void difference_between_two_dates_java8_period() {

	LocalDate sinceGraduation = LocalDate.of(1984, Month.JUNE, 4);
	LocalDate currentDate = LocalDate.now();

	Period betweenDates = Period.between(sinceGraduation, currentDate);

	int diffInDays = betweenDates.getDays();
	int diffInMonths = betweenDates.getMonths();
	int diffInYears = betweenDates.getYears();

	logger.info(diffInDays);
	logger.info(diffInMonths);
	logger.info(diffInYears);

	assertTrue(diffInDays >= anyInt());
	assertTrue(diffInMonths >= anyInt());
	assertTrue(diffInYears >= anyInt());
}
 
Example 2
Source File: DateDiffUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
    LocalDate now = LocalDate.now();
    LocalDate sixDaysBehind = now.minusDays(6);

    Period period = Period.between(now, sixDaysBehind);
    int diff = Math.abs(period.getDays());

    assertEquals(diff, 6);
}
 
Example 3
Source File: DateTest.java    From java-master with Apache License 2.0 5 votes vote down vote up
@Test
public void test2() {
    Period tenDays = Period.between(LocalDate.of(2014, 3, 8), LocalDate.of(2014, 3, 18));
    // Duration和Period类都提供了很多非常方便的工厂类,直接创建对应的实例:
    Duration threeMinutes = Duration.ofMinutes(3);
    threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
    tenDays = Period.ofDays(10);
    Period threeWeeks = Period.ofWeeks(3);
    Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
}
 
Example 4
Source File: TCKPeriod.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="between")
public void factory_between_LocalDate(int y1, int m1, int d1, int y2, int m2, int d2, int ye, int me, int de) {
    LocalDate start = LocalDate.of(y1, m1, d1);
    LocalDate end = LocalDate.of(y2, m2, d2);
    Period test = Period.between(start, end);
    assertPeriod(test, ye, me, de);
    //assertEquals(start.plus(test), end);
}
 
Example 5
Source File: PeriodDemo.java    From SA47 with The Unlicense 5 votes vote down vote up
public static void main(String[] args) {
	
	LocalDate dateA = LocalDate.of(1978, 8, 26);
	LocalDate dateB = LocalDate.of(1988, 9, 28);
	Period period = Period.between(dateA, dateB);
	System.out.printf("Between %s and %s" + " there are %d years, %d months" + " and %d days%n", dateA, dateB,
			period.getYears(), period.getMonths(), period.getDays());

}
 
Example 6
Source File: TCKPeriod.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="between")
public void factory_between_LocalDate(int y1, int m1, int d1, int y2, int m2, int d2, int ye, int me, int de) {
    LocalDate start = LocalDate.of(y1, m1, d1);
    LocalDate end = LocalDate.of(y2, m2, d2);
    Period test = Period.between(start, end);
    assertPeriod(test, ye, me, de);
    //assertEquals(start.plus(test), end);
}
 
Example 7
Source File: TCKPeriod.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="between")
public void factory_between_LocalDate(int y1, int m1, int d1, int y2, int m2, int d2, int ye, int me, int de) {
    LocalDate start = LocalDate.of(y1, m1, d1);
    LocalDate end = LocalDate.of(y2, m2, d2);
    Period test = Period.between(start, end);
    assertPeriod(test, ye, me, de);
    //assertEquals(start.plus(test), end);
}
 
Example 8
Source File: DefaultDurationLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
private Duration toYearsMonthDuration(DatatypeFactory datatypeFactory, LocalDate date1, LocalDate date2) {
    Period between = Period.between(date2, date1);
    int years = between.getYears();
    int months = between.getMonths();
    if (between.isNegative()) {
        years = - years;
        months = - months;
    }
    return datatypeFactory.newDurationYearMonth(!between.isNegative(), years, months);
}
 
Example 9
Source File: LocalDateType.java    From jdmn with Apache License 2.0 5 votes vote down vote up
@Override
public TemporalAmount dateSubtract(LocalDate first, LocalDate second) {
    if (first == null || second == null) {
        return null;
    }

    try {
        return Period.between(first, second);
    } catch (Exception e) {
        String message = String.format("dateSubtract(%s, %s)", first, second);
        logError(message, e);
        return null;
    }
}
 
Example 10
Source File: TCKPeriod.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="between")
public void factory_between_LocalDate(int y1, int m1, int d1, int y2, int m2, int d2, int ye, int me, int de) {
    LocalDate start = LocalDate.of(y1, m1, d1);
    LocalDate end = LocalDate.of(y2, m2, d2);
    Period test = Period.between(start, end);
    assertPeriod(test, ye, me, de);
    //assertEquals(start.plus(test), end);
}
 
Example 11
Source File: UserUIContext.java    From mycollab with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String formatDuration(LocalDate date) {
    Period period = Period.between(date, LocalDate.now());
    return UserUIContext.getMessage(DayI18nEnum.DURATION, period.getDays());
}
 
Example 12
Source File: TCKPeriod.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_between_LocalDate_nullSecond() {
    Period.between(LocalDate.of(2010, 1, 1), (LocalDate) null);
}
 
Example 13
Source File: PersonService.java    From tutorials with MIT License 4 votes vote down vote up
public int getAge(Person person){
    Period p = Period.between(person.getDateOfBirth(), LocalDate.now());
    return p.getYears();
}
 
Example 14
Source File: TCKPeriod.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_between_LocalDate_nullSecond() {
    Period.between(LocalDate.of(2010, 1, 1), (LocalDate) null);
}
 
Example 15
Source File: CostSheetServiceImpl.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
protected void computeRealHumanResourceCost(
    OperationOrder operationOrder,
    int priority,
    int bomLevel,
    CostSheetLine parentCostSheetLine,
    LocalDate previousCostSheetDate)
    throws AxelorException {
  if (operationOrder.getProdHumanResourceList() != null) {
    Long duration = 0L;
    if (parentCostSheetLine.getCostSheet().getCalculationTypeSelect()
            == CostSheetRepository.CALCULATION_END_OF_PRODUCTION
        || parentCostSheetLine.getCostSheet().getCalculationTypeSelect()
            == CostSheetRepository.CALCULATION_PARTIAL_END_OF_PRODUCTION) {
      Period period =
          previousCostSheetDate != null
              ? Period.between(
                  parentCostSheetLine.getCostSheet().getCalculationDate(), previousCostSheetDate)
              : null;
      duration =
          period != null ? Long.valueOf(period.getDays() * 24) : operationOrder.getRealDuration();
    } else if (parentCostSheetLine.getCostSheet().getCalculationTypeSelect()
        == CostSheetRepository.CALCULATION_WORK_IN_PROGRESS) {

      BigDecimal ratio = costSheet.getManufOrderProducedRatio();

      Long plannedDuration =
          DurationTool.getSecondsDuration(
                  Duration.between(
                      operationOrder.getPlannedStartDateT(), operationOrder.getPlannedEndDateT()))
              * ratio.longValue();
      Long totalPlannedDuration = 0L;
      for (OperationOrder manufOperationOrder :
          operationOrder.getManufOrder().getOperationOrderList()) {
        if (manufOperationOrder.getId() == operationOrder.getId()) {
          totalPlannedDuration += manufOperationOrder.getPlannedDuration();
        }
      }
      duration = Math.abs(totalPlannedDuration - plannedDuration);
    }
    for (ProdHumanResource prodHumanResource : operationOrder.getProdHumanResourceList()) {
      this.computeRealHumanResourceCost(
          prodHumanResource,
          operationOrder.getWorkCenter(),
          priority,
          bomLevel,
          parentCostSheetLine,
          duration);
    }
  }
}
 
Example 16
Source File: CostSheetServiceBusinessImpl.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void computeRealHumanResourceCost(
    OperationOrder operationOrder,
    int priority,
    int bomLevel,
    CostSheetLine parentCostSheetLine,
    LocalDate previousCostSheetDate)
    throws AxelorException {
  AppProductionService appProductionService = Beans.get(AppProductionService.class);

  if (!appProductionService.isApp("production")
      || !appProductionService.getAppProduction().getManageBusinessProduction()) {
    super.computeRealHumanResourceCost(
        operationOrder, priority, bomLevel, parentCostSheetLine, previousCostSheetDate);
    return;
  }

  if (operationOrder.getTimesheetLineList() != null) {
    Long duration = 0L;
    if (parentCostSheetLine.getCostSheet().getCalculationTypeSelect()
            == CostSheetRepository.CALCULATION_END_OF_PRODUCTION
        || parentCostSheetLine.getCostSheet().getCalculationTypeSelect()
            == CostSheetRepository.CALCULATION_PARTIAL_END_OF_PRODUCTION) {
      Period period =
          previousCostSheetDate != null
              ? Period.between(
                  parentCostSheetLine.getCostSheet().getCalculationDate(), previousCostSheetDate)
              : null;
      duration =
          period != null ? Long.valueOf(period.getDays() * 24) : operationOrder.getRealDuration();
    } else if (parentCostSheetLine.getCostSheet().getCalculationTypeSelect()
        == CostSheetRepository.CALCULATION_WORK_IN_PROGRESS) {

      duration =
          operationOrder.getRealDuration()
              - (operationOrder.getPlannedDuration()
                  * costSheet.getManufOrderProducedRatio().longValue());
    }

    // TODO get the timesheet Line done when we run the calculation.

    this.computeRealHumanResourceCost(
        null, operationOrder.getWorkCenter(), priority, bomLevel, parentCostSheetLine, duration);
  }
}
 
Example 17
Source File: TCKPeriod.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_between_LocalDate_nullFirst() {
    Period.between((LocalDate) null, LocalDate.of(2010, 1, 1));
}
 
Example 18
Source File: UsePeriod.java    From tutorials with MIT License 4 votes vote down vote up
Period getDifferenceBetweenDates(LocalDate localDate1, LocalDate localDate2) {
    return Period.between(localDate1, localDate2);
}
 
Example 19
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link java.time.Period} between the first day of this year (inclusive) and the first day of the
 * provided {@link java.time.Year} (exclusive).
 *
 * @param self a Year
 * @param year another Year
 * @return a Period between the Years
 * @since 2.5.0
 */
public static Period rightShift(final Year self, Year year) {
    return Period.between(self.atDay(1), year.atDay(1));
}
 
Example 20
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link java.time.Period} of time between the first day of this year/month (inclusive) and the
 * given {@link java.time.YearMonth} (exclusive).
 *
 * @param self  a YearMonth
 * @param other another YearMonth
 * @return a Period
 * @since 2.5.0
 */
public static Period rightShift(YearMonth self, YearMonth other) {
    return Period.between(self.atDay(1), other.atDay(1));
}