java.time.Period.between()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(327)

本文整理了Java中java.time.Period.between()方法的一些代码示例,展示了Period.between()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Period.between()方法的具体详情如下:
包路径:java.time.Period
类名称:Period
方法名:between

Period.between介绍

[英]Obtains a Period consisting of the number of years, months, and days between two dates.

The start date is included, but the end date is not. The period is calculated by removing complete months, then calculating the remaining number of days, adjusting to ensure that both have the same sign. The number of months is then split into years and months based on a 12 month year. A month is considered if the end day-of-month is greater than or equal to the start day-of-month. For example, from 2010-01-15 to 2011-03-18 is one year, two months and three days.

The result of this method can be a negative period if the end is before the start. The negative sign will be the same in each of year, month and day.
[中]获取由两个日期之间的年、月和日数组成的期间。
包括开始日期,但不包括结束日期。计算周期的方法是删除完整的月份,然后计算剩余的天数,并进行调整以确保两者具有相同的符号。然后根据12个月的年份将月数分为年和月。如果一个月的结束日大于或等于该月的开始日,则视为一个月。例如,从2010-01-15到2011-03-18是一年零两个月零三天。
如果结束时间早于开始时间,则此方法的结果可能是负周期。负号在每年、每月和每天都是相同的。

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

/**
 * Returns a {@link java.time.Period} equivalent to the time between this date (inclusive)
 * and the provided {@link java.time.LocalDate} (exclusive).
 *
 * @param self  a LocalDate
 * @param other another LocalDate
 * @return a Period representing the time between the two LocalDates
 * @since 2.5.0
 */
public static Period rightShift(final LocalDate self, LocalDate other) {
  return Period.between(self, other);
}

代码示例来源:origin: OpenGamma/Strata

/**
 * Returns the length of the period.
 * <p>
 * This returns the length of the period, considering the adjusted start and end dates.
 * The calculation does not involve a day count or holiday calendar.
 * The period is calculated using {@link Period#between(LocalDate, LocalDate)} and as
 * such includes the start date and excludes the end date.
 * 
 * @return the length of the period
 */
public Period length() {
 return Period.between(startDate, endDate);
}

代码示例来源:origin: jkazama/sample-boot-micro

/** 日付の間隔を取得します。 */
public static Optional<Period> between(LocalDate start, LocalDate end) {
  if (start == null || end == null)
    return Optional.empty();
  return Optional.of(Period.between(start, end));
}

代码示例来源:origin: stackoverflow.com

import java.time.Period
import java.time.LocalDate
import java.time.format.DateTimeFormatter

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");    
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.parse("1/1/1960", formatter);

Period p = Period.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
          " months and " + p.getDays() +
          " days old.");

代码示例来源:origin: thjanssen/HibernateTips

public int getAge() {
  if (this.age == null) {
    log.info("Calculate age");
    this.age = Period.between(dateOfBirth, LocalDate.now()).getYears();
  } else {
    log.info("Return cached value.");
  }
  return age;
}

代码示例来源:origin: stackoverflow.com

LocalDate last=LocalDate.of ( 2015, Month.DECEMBER, 31);
 LocalDate now= LocalDate.of ( 2016, Month.JANUARY, 1);
 java.time.Period period=java.time.Period.between ( last, now);
 System.out.println ( period.getDays () );

代码示例来源:origin: com.goldmansachs.jdmn/jdmn-core

@Override
public TemporalAmount dateSubtract(LocalDate first, LocalDate second) {
  if (first == null || second == null) {
    return null;
  }
  try {
    return Period.between(first, second);
  } catch (Throwable e) {
    String message = String.format("dateSubtract(%s, %s)", first, second);
    logError(message, e);
    return null;
  }
}

代码示例来源:origin: goldmansachs/jdmn

@Override
public TemporalAmount dateSubtract(LocalDate first, LocalDate second) {
  if (first == null || second == null) {
    return null;
  }
  try {
    return Period.between(first, second);
  } catch (Throwable e) {
    String message = String.format("dateSubtract(%s, %s)", first, second);
    logError(message, e);
    return null;
  }
}

代码示例来源:origin: thjanssen/HibernateTips

public int getCalculatedAge() {
  log.info("Calculate age");
  return Period.between(dateOfBirth, LocalDate.now()).getYears();
}

代码示例来源:origin: kittylyst/javanut6-examples

public int getDaysUntilBirthday(String name) {
  Period period = Period.between(LocalDate.now(), birthdays.get(name));
  return period.getDays();
}

代码示例来源:origin: kittylyst/javanut6-examples

public int getAgeInYear(String name, int year) {
  Period period = Period.between(birthdays.get(name), birthdays.get(name).withYear(year));
  return period.getYears();
}

代码示例来源:origin: IanDarwin/javasrc

public static void main(String[] args) {
    /** The date at the end of the last century */
    LocalDate endofCentury = LocalDate.of(2000, 12, 31);
    LocalDate now = LocalDate.now();
    
    Period diff = Period.between(endofCentury, now);
    
    System.out.printf("The 21st century (up to %s) is %s old%n", now, diff);
    System.out.printf(
        "The 21st century is %d years, %d months and %d days old",
        diff.getYears(), diff.getMonths(), diff.getDays());
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

/**
 * 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));
}

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

/**
 * 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));
}

代码示例来源:origin: com.github.robozonky/robozonky-strategy-natural

public long getMonthsBeforeExit() {
  if (exitProperties == null) {
    return -1;
  } else {
    return Math.max(0, Period.between(DateUtil.localNow().toLocalDate(),
                     exitProperties.getAccountTermination()).toTotalMonths());
  }
}

代码示例来源:origin: RoboZonky/robozonky

public long getMonthsBeforeExit() {
  if (exitProperties == null) {
    return -1;
  } else {
    return Math.max(0, Period.between(DateUtil.localNow().toLocalDate(),
                     exitProperties.getAccountTermination()).toTotalMonths());
  }
}

代码示例来源:origin: com.github.robozonky/robozonky-notifications

private static long getMonthsElapsed(final Investment i) {
  return Period.between(i.getInvestmentDate().toLocalDate(), DateUtil.localNow().toLocalDate()).toTotalMonths();
}

代码示例来源:origin: OpenGamma/Strata

public void test_length() {
 assertEquals(SchedulePeriod.of(JUN_16, JUN_18, JUN_16, JUN_18).length(), Period.between(JUN_16, JUN_18));
 assertEquals(SchedulePeriod.of(JUN_16, JUL_18, JUN_16, JUL_17).length(), Period.between(JUN_16, JUL_18));
}

代码示例来源:origin: com.goldmansachs.jdmn/jdmn-core

public static 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);
}

代码示例来源:origin: goldmansachs/jdmn

public static 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);
}

相关文章