java.time.Year.isLeap()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(99)

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

Year.isLeap介绍

[英]Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

This method applies the current rules for leap years across the whole time-line. In general, a year is a leap year if it is divisible by four without remainder. However, years divisible by 100, are not leap years, with the exception of years divisible by 400 which are.

For example, 1904 is a leap year it is divisible by 4. 1900 was not a leap year as it is divisible by 100, however 2000 was a leap year as it is divisible by 400.

The calculation is proleptic - applying the same rules into the far future and far past. This is historically inaccurate, but is correct for the ISO-8601 standard.
[中]根据ISO proleptic日历系统规则检查年份是否为闰年。
此方法在整个时间线上应用闰年的当前规则。一般来说,如果一年可以被四除而没有余数,那么它就是闰年。然而,可被100整除的年份不是闰年,但可被400整除的年份除外。
例如,1904年是闰年,它可以被4整除。1900年不是闰年,因为它可以被100整除,而2000年是闰年,因为它可以被400整除。
计算是前瞻性的——将同样的规则应用到遥远的未来和遥远的过去。这在历史上是不准确的,但对于ISO-8601标准来说是正确的。

代码示例

代码示例来源:origin: org.threeten/threeten-extra

/**
 * Checks if the year is valid for this day-of-year.
 * <p>
 * This method checks whether this day-of-yearand the input year form
 * a valid date. This can only return false for day-of-year 366.
 *
 * @param year  the year to validate
 * @return true if the year is valid for this day-of-year
 */
public boolean isValidYear(int year) {
  return (day < 366 || Year.isLeap(year));
}

代码示例来源:origin: AnalyticalGraphicsInc/czml-writer

/**
 * Returns whether the specified year is a leap year.
 *
 * @param year
 *            The 4-digit year.
 * @return true if year is a leap year, otherwise false.
 */
public static boolean isLeapYear(int year) {
  return Year.isLeap(year);
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Gets the length of this year in days.
 *
 * @return the length of this year in days, 365 or 366
 */
public int length() {
  return isLeap() ? 366 : 365;
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Checks if the year is valid for this month-day.
 * <p>
 * This method checks whether this month and day and the input year form
 * a valid date. This can only return false for February 29th.
 *
 * @param year  the year to validate, an out of range value returns false
 * @return true if the year is valid for this month-day
 * @see Year#isValidMonthDay(MonthDay)
 */
public boolean isValidYear(int year) {
  return (day == 29 && month == 2 && Year.isLeap(year) == false) == false;
}

代码示例来源:origin: real-logic/artio

private static int monthsToDays(final int month, final int year)
{
  int days = (367 * month - 362) / MONTHS_IN_YEAR;
  if (month > 2)
  {
    days--;
    if (!isLeap(year))
    {
      days--;
    }
  }
  return days;
}

代码示例来源:origin: EvoSuite/evosuite

public static boolean isLeap(long year) {
  return Year.isLeap(year);
}

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

ZonedDateTime now = ZonedDateTime.now ( ZoneId.of ( "America/Montreal" ) );
Year year = Year.from ( now );
Boolean isLeap = year.isLeap ();

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Checks if the year is a leap year, according to the ISO proleptic
 * calendar system rules.
 * <p>
 * This method applies the current rules for leap years across the whole time-line.
 * In general, a year is a leap year if it is divisible by four without
 * remainder. However, years divisible by 100, are not leap years, with
 * the exception of years divisible by 400 which are.
 * <p>
 * For example, 1904 is a leap year it is divisible by 4.
 * 1900 was not a leap year as it is divisible by 100, however 2000 was a
 * leap year as it is divisible by 400.
 * <p>
 * The calculation is proleptic - applying the same rules into the far future and far past.
 * This is historically inaccurate, but is correct for the ISO-8601 standard.
 *
 * @return true if the year is leap, false otherwise
 */
public boolean isLeap() {
  return Year.isLeap(year);
}

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

private static LocalDate ensureLeapDay(int possibleLeapYear) {
 if (Year.isLeap(possibleLeapYear)) {
  return LocalDate.of(possibleLeapYear, 2, 29);
 } else {
  return LocalDate.of(possibleLeapYear + 4, 2, 29);
 }
}

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

Year year = Year.from( zdt );
Boolean isLeapYear = year.isLeap();
int yearNumber = year.getValue();

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

/** 指定年の最終日を取得します。 */
public static LocalDate dayTo(int year) {
  return LocalDate.ofYearDay(year, Year.of(year).isLeap() ? 366 : 365);
}

代码示例来源:origin: org.threeten/threeten-extra

/**
 * Combines this year-week with a day-of-week to create a {@code LocalDate}.
 * <p>
 * This returns a {@code LocalDate} formed from this year-week and the specified day-of-Week.
 * <p>
 * This method can be used as part of a chain to produce a date:
 * <pre>
 *  LocalDate date = yearWeek.atDay(MONDAY);
 * </pre>
 *
 * @param dayOfWeek  the day-of-week to use, not null
 * @return the date formed from this year-week and the specified day, not null
 */
public LocalDate atDay(DayOfWeek dayOfWeek) {
  Objects.requireNonNull(dayOfWeek, "dayOfWeek");
  int correction = LocalDate.of(year, 1, 4).getDayOfWeek().getValue() + 3;
  int dayOfYear = week * 7 + dayOfWeek.getValue() - correction;
  int maxDaysOfYear = Year.isLeap(year) ? 366 : 365;
  if (dayOfYear > maxDaysOfYear) {
    return LocalDate.ofYearDay(year + 1, dayOfYear - maxDaysOfYear);
  }
  if (dayOfYear > 0) {
    return LocalDate.ofYearDay(year, dayOfYear);
  } else {
    int daysOfPreviousYear = Year.isLeap(year - 1) ? 366 : 365;
    return LocalDate.ofYearDay(year - 1, daysOfPreviousYear + dayOfYear);
  }
}

代码示例来源:origin: com.github.seratch/java-time-backport

private LocalDateTime toDateTime(int year) {
    adjustToFowards(year);
    LocalDate date;
    if (dayOfMonth == -1) {
      dayOfMonth = month.length(Year.isLeap(year));
      date = LocalDate.of(year, month, dayOfMonth);
      if (dayOfWeek != null) {
        date = date.with(TemporalAdjusters.previousOrSame(dayOfWeek));
      }
    } else {
      date = LocalDate.of(year, month, dayOfMonth);
      if (dayOfWeek != null) {
        date = date.with(TemporalAdjusters.nextOrSame(dayOfWeek));
      }
    }
    date = deduplicate(date);
    LocalDateTime ldt = LocalDateTime.of(date, time);
    if (endOfDay) {
      ldt = ldt.plusDays(1);
    }
    return ldt;
  }
}

代码示例来源:origin: org.threeten/threeten-extra

/**
 * Combines this year-quarter with a day-of-quarter to create a {@code LocalDate}.
 * <p>
 * This returns a {@code LocalDate} formed from this year-quarter and the specified day-of-quarter.
 * <p>
 * The day-of-quarter value must be valid for the year-quarter.
 * <p>
 * This method can be used as part of a chain to produce a date:
 * <pre>
 *  LocalDate date = yearQuarter.atDay(day);
 * </pre>
 *
 * @param dayOfQuarter  the day-of-quarter to use, from 1 to 92
 * @return the date formed from this year-quarter and the specified day, not null
 * @throws DateTimeException if the day is invalid for the year-quarter
 * @see #isValidDay(int)
 */
public LocalDate atDay(int dayOfQuarter) {
  ValueRange.of(1, lengthOfQuarter()).checkValidValue(dayOfQuarter, DAY_OF_QUARTER);
  boolean leap = Year.isLeap(year);
  Month month = quarter.firstMonth();
  while (dayOfQuarter > month.length(leap)) {
    dayOfQuarter -= month.length(leap);
    month = month.plus(1);
  }
  return LocalDate.of(year, month, dayOfQuarter);
}

代码示例来源:origin: com.github.seratch/java-time-backport

dom = Math.min(dom, 30);
} else if (moy == 2) {
  dom = Math.min(dom, Month.FEBRUARY.length(Year.isLeap(y)));

代码示例来源:origin: com.github.seratch/java-time-backport

private int localizedWBY(TemporalAccessor temporal) {
  int sow = weekDef.getFirstDayOfWeek().getValue();
  int isoDow = temporal.get(DAY_OF_WEEK);
  int dow = Jdk8Methods.floorMod(isoDow - sow, 7) + 1;
  int year = temporal.get(YEAR);
  long woy = localizedWeekOfYear(temporal, dow);
  if (woy == 0) {
    return year - 1;
  } else if (woy < 53) {
    return year;
  }
  int offset = startOfWeekOffset(temporal.get(DAY_OF_YEAR), dow);
  int yearLen = Year.isLeap(year) ? 366 : 365;
  int weekIndexOfFirstWeekNextYear = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
  if (woy >= weekIndexOfFirstWeekNextYear) {
    return year + 1;
  }
  return year;
}

代码示例来源:origin: com.github.seratch/java-time-backport

private int localizedWOWBY(TemporalAccessor temporal) {
  int sow = weekDef.getFirstDayOfWeek().getValue();
  int isoDow = temporal.get(DAY_OF_WEEK);
  int dow = Jdk8Methods.floorMod(isoDow - sow, 7) + 1;
  long woy = localizedWeekOfYear(temporal, dow);
  if (woy == 0) {
    ChronoLocalDate previous = Chronology.from(temporal).date(temporal).minus(1, ChronoUnit.WEEKS);
    return (int) localizedWeekOfYear(previous, dow) + 1;
  } else if (woy >= 53) {
    int offset = startOfWeekOffset(temporal.get(DAY_OF_YEAR), dow);
    int year = temporal.get(YEAR);
    int yearLen = Year.isLeap(year) ? 366 : 365;
    int weekIndexOfFirstWeekNextYear = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
    if (woy >= weekIndexOfFirstWeekNextYear) {
      return (int) (woy - (weekIndexOfFirstWeekNextYear - 1));
    }
  }
  return (int) woy;
}

代码示例来源:origin: com.github.seratch/java-time-backport

private ValueRange rangeWOWBY(TemporalAccessor temporal) {
  int sow = weekDef.getFirstDayOfWeek().getValue();
  int isoDow = temporal.get(DAY_OF_WEEK);
  int dow = Jdk8Methods.floorMod(isoDow - sow, 7) + 1;
  long woy = localizedWeekOfYear(temporal, dow);
  if (woy == 0) {
    return rangeWOWBY(Chronology.from(temporal).date(temporal).minus(2, ChronoUnit.WEEKS));
  }
  int offset = startOfWeekOffset(temporal.get(DAY_OF_YEAR), dow);
  int year = temporal.get(YEAR);
  int yearLen = Year.isLeap(year) ? 366 : 365;
  int weekIndexOfFirstWeekNextYear = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
  if (woy >= weekIndexOfFirstWeekNextYear) {
    return rangeWOWBY(Chronology.from(temporal).date(temporal).plus(2, ChronoUnit.WEEKS));
  }
  return ValueRange.of(1, weekIndexOfFirstWeekNextYear - 1);
}

相关文章