Java Code Examples for java.time.LocalDate#isLeapYear()

The following examples show how to use java.time.LocalDate#isLeapYear() . 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: IsoFields.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private static int getWeek(LocalDate date) {
    int dow0 = date.getDayOfWeek().ordinal();
    int doy0 = date.getDayOfYear() - 1;
    int doyThu0 = doy0 + (3 - dow0);  // adjust to mid-week Thursday (which is 3 indexed from zero)
    int alignedWeek = doyThu0 / 7;
    int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
    int firstMonDoy0 = firstThuDoy0 - 3;
    if (firstMonDoy0 < -3) {
        firstMonDoy0 += 7;
    }
    if (doy0 < firstMonDoy0) {
        return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
    }
    int week = ((doy0 - firstMonDoy0) / 7) + 1;
    if (week == 53) {
        if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
            week = 1;
        }
    }
    return week;
}
 
Example 2
Source File: IsoFields.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static int getWeek(LocalDate date) {
    int dow0 = date.getDayOfWeek().ordinal();
    int doy0 = date.getDayOfYear() - 1;
    int doyThu0 = doy0 + (3 - dow0);  // adjust to mid-week Thursday (which is 3 indexed from zero)
    int alignedWeek = doyThu0 / 7;
    int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
    int firstMonDoy0 = firstThuDoy0 - 3;
    if (firstMonDoy0 < -3) {
        firstMonDoy0 += 7;
    }
    if (doy0 < firstMonDoy0) {
        return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
    }
    int week = ((doy0 - firstMonDoy0) / 7) + 1;
    if (week == 53) {
        if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
            week = 1;
        }
    }
    return week;
}
 
Example 3
Source File: DayCountConvention_ACT_365L.java    From finmath-lib with Apache License 2.0 6 votes vote down vote up
@Override
public double getDaycountFraction(final LocalDate startDate, final LocalDate endDate) {
	if(startDate.isAfter(endDate)) {
		return -getDaycountFraction(endDate,startDate);
	}

	double daysPerYear = 365.0;

	// Check endDate for leap year
	if (endDate.isLeapYear()) {
		daysPerYear = 366.0;
	}

	final double daycountFraction = getDaycount(startDate, endDate) / daysPerYear;

	return daycountFraction;
}
 
Example 4
Source File: IsoFields.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
private static int getWeek(LocalDate date) {
    int dow0 = date.getDayOfWeek().ordinal();
    int doy0 = date.getDayOfYear() - 1;
    int doyThu0 = doy0 + (3 - dow0);  // adjust to mid-week Thursday (which is 3 indexed from zero)
    int alignedWeek = doyThu0 / 7;
    int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
    int firstMonDoy0 = firstThuDoy0 - 3;
    if (firstMonDoy0 < -3) {
        firstMonDoy0 += 7;
    }
    if (doy0 < firstMonDoy0) {
        return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
    }
    int week = ((doy0 - firstMonDoy0) / 7) + 1;
    if (week == 53) {
        if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
            week = 1;
        }
    }
    return week;
}
 
Example 5
Source File: IsoFields.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static int getWeek(LocalDate date) {
    int dow0 = date.getDayOfWeek().ordinal();
    int doy0 = date.getDayOfYear() - 1;
    int doyThu0 = doy0 + (3 - dow0);  // adjust to mid-week Thursday (which is 3 indexed from zero)
    int alignedWeek = doyThu0 / 7;
    int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
    int firstMonDoy0 = firstThuDoy0 - 3;
    if (firstMonDoy0 < -3) {
        firstMonDoy0 += 7;
    }
    if (doy0 < firstMonDoy0) {
        return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
    }
    int week = ((doy0 - firstMonDoy0) / 7) + 1;
    if (week == 53) {
        if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
            week = 1;
        }
    }
    return week;
}
 
Example 6
Source File: IsoFields.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static int getWeek(LocalDate date) {
    int dow0 = date.getDayOfWeek().ordinal();
    int doy0 = date.getDayOfYear() - 1;
    int doyThu0 = doy0 + (3 - dow0);  // adjust to mid-week Thursday (which is 3 indexed from zero)
    int alignedWeek = doyThu0 / 7;
    int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
    int firstMonDoy0 = firstThuDoy0 - 3;
    if (firstMonDoy0 < -3) {
        firstMonDoy0 += 7;
    }
    if (doy0 < firstMonDoy0) {
        return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
    }
    int week = ((doy0 - firstMonDoy0) / 7) + 1;
    if (week == 53) {
        if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
            week = 1;
        }
    }
    return week;
}
 
Example 7
Source File: IsoFields.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static int getWeek(LocalDate date) {
    int dow0 = date.getDayOfWeek().ordinal();
    int doy0 = date.getDayOfYear() - 1;
    int doyThu0 = doy0 + (3 - dow0);  // adjust to mid-week Thursday (which is 3 indexed from zero)
    int alignedWeek = doyThu0 / 7;
    int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
    int firstMonDoy0 = firstThuDoy0 - 3;
    if (firstMonDoy0 < -3) {
        firstMonDoy0 += 7;
    }
    if (doy0 < firstMonDoy0) {
        return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
    }
    int week = ((doy0 - firstMonDoy0) / 7) + 1;
    if (week == 53) {
        if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
            week = 1;
        }
    }
    return week;
}
 
Example 8
Source File: IsoFields.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static int getWeek(LocalDate date) {
    int dow0 = date.getDayOfWeek().ordinal();
    int doy0 = date.getDayOfYear() - 1;
    int doyThu0 = doy0 + (3 - dow0);  // adjust to mid-week Thursday (which is 3 indexed from zero)
    int alignedWeek = doyThu0 / 7;
    int firstThuDoy0 = doyThu0 - (alignedWeek * 7);
    int firstMonDoy0 = firstThuDoy0 - 3;
    if (firstMonDoy0 < -3) {
        firstMonDoy0 += 7;
    }
    if (doy0 < firstMonDoy0) {
        return (int) getWeekRange(date.withDayOfYear(180).minusYears(1)).getMaximum();
    }
    int week = ((doy0 - firstMonDoy0) / 7) + 1;
    if (week == 53) {
        if ((firstMonDoy0 == -3 || (firstMonDoy0 == -2 && date.isLeapYear())) == false) {
            week = 1;
        }
    }
    return week;
}
 
Example 9
Source File: TCKIsoFields.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_loop() {
    // loop round at least one 400 year cycle, including before 1970
    LocalDate date = LocalDate.of(1960, 1, 5);  // Tuseday of week 1 1960
    int year = 1960;
    int wby = 1960;
    int weekLen = 52;
    int week = 1;
    while (date.getYear() < 2400) {
        DayOfWeek loopDow = date.getDayOfWeek();
        if (date.getYear() != year) {
            year = date.getYear();
        }
        if (loopDow == MONDAY) {
            week++;
            if ((week == 53 && weekLen == 52) || week == 54) {
                week = 1;
                LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1);
                DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek();
                weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52);
                wby++;
            }
        }
        assertEquals("Failed on " + date + " " + date.getDayOfWeek(), IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen));
        assertEquals("Failed on " + date + " " + date.getDayOfWeek(), IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week);
        assertEquals("Failed on " + date + " " + date.getDayOfWeek(), date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week);
        assertEquals("Failed on " + date + " " + date.getDayOfWeek(), IsoFields.WEEK_BASED_YEAR.getFrom(date), wby);
        assertEquals("Failed on " + date + " " + date.getDayOfWeek(), date.get(IsoFields.WEEK_BASED_YEAR), wby);
        date = date.plusDays(1);
    }
}
 
Example 10
Source File: IsoFields.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private static int getWeekRange(int wby) {
    LocalDate date = LocalDate.of(wby, 1, 1);
    // 53 weeks if standard year starts on Thursday, or Wed in a leap year
    if (date.getDayOfWeek() == THURSDAY || (date.getDayOfWeek() == WEDNESDAY && date.isLeapYear())) {
        return 53;
    }
    return 52;
}
 
Example 11
Source File: IsoFields.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static int getWeekRange(int wby) {
    LocalDate date = LocalDate.of(wby, 1, 1);
    // 53 weeks if standard year starts on Thursday, or Wed in a leap year
    if (date.getDayOfWeek() == THURSDAY || (date.getDayOfWeek() == WEDNESDAY && date.isLeapYear())) {
        return 53;
    }
    return 52;
}
 
Example 12
Source File: TCKIsoFields.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void test_loop() {
    // loop round at least one 400 year cycle, including before 1970
    LocalDate date = LocalDate.of(1960, 1, 5);  // Tuseday of week 1 1960
    int year = 1960;
    int wby = 1960;
    int weekLen = 52;
    int week = 1;
    while (date.getYear() < 2400) {
        DayOfWeek loopDow = date.getDayOfWeek();
        if (date.getYear() != year) {
            year = date.getYear();
        }
        if (loopDow == MONDAY) {
            week++;
            if ((week == 53 && weekLen == 52) || week == 54) {
                week = 1;
                LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1);
                DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek();
                weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52);
                wby++;
            }
        }
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek());
        date = date.plusDays(1);
    }
}
 
Example 13
Source File: TCKIsoFields.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_loop() {
    // loop round at least one 400 year cycle, including before 1970
    LocalDate date = LocalDate.of(1960, 1, 5);  // Tuseday of week 1 1960
    int year = 1960;
    int wby = 1960;
    int weekLen = 52;
    int week = 1;
    while (date.getYear() < 2400) {
        DayOfWeek loopDow = date.getDayOfWeek();
        if (date.getYear() != year) {
            year = date.getYear();
        }
        if (loopDow == MONDAY) {
            week++;
            if ((week == 53 && weekLen == 52) || week == 54) {
                week = 1;
                LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1);
                DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek();
                weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52);
                wby++;
            }
        }
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek());
        date = date.plusDays(1);
    }
}
 
Example 14
Source File: IsoFields.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
private static int getWeekRange(int wby) {
    LocalDate date = LocalDate.of(wby, 1, 1);
    // 53 weeks if standard year starts on Thursday, or Wed in a leap year
    if (date.getDayOfWeek() == THURSDAY || (date.getDayOfWeek() == WEDNESDAY && date.isLeapYear())) {
        return 53;
    }
    return 52;
}
 
Example 15
Source File: IsoFields.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static int getWeekRange(int wby) {
    LocalDate date = LocalDate.of(wby, 1, 1);
    // 53 weeks if standard year starts on Thursday, or Wed in a leap year
    if (date.getDayOfWeek() == THURSDAY || (date.getDayOfWeek() == WEDNESDAY && date.isLeapYear())) {
        return 53;
    }
    return 52;
}
 
Example 16
Source File: IsoFields.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static int getWeekRange(int wby) {
    LocalDate date = LocalDate.of(wby, 1, 1);
    // 53 weeks if standard year starts on Thursday, or Wed in a leap year
    if (date.getDayOfWeek() == THURSDAY || (date.getDayOfWeek() == WEDNESDAY && date.isLeapYear())) {
        return 53;
    }
    return 52;
}
 
Example 17
Source File: DateAdjusters.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the next leap day on or after the input date.
 * <p>
 * If the input date is February 29, the input date is returned unaltered.
 * Otherwise, the adjuster returns the next occurrence of February 29 after the input date.
 * 
 * @param input  the input date
 * @return the next leap day date
 */
static LocalDate nextOrSameLeapDay(LocalDate input) {
  // already a leap day, return it
  if (input.getMonthValue() == 2 && input.getDayOfMonth() == 29) {
    return input;
  }
  // handle if before February 29 in a leap year
  if (input.isLeapYear() && input.getMonthValue() <= 2) {
    return LocalDate.of(input.getYear(), 2, 29);
  }
  // handle any other date
  return ensureLeapDay(((input.getYear() / 4) * 4) + 4);
}
 
Example 18
Source File: TCKIsoFields.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void test_loop() {
    // loop round at least one 400 year cycle, including before 1970
    LocalDate date = LocalDate.of(1960, 1, 5);  // Tuseday of week 1 1960
    int year = 1960;
    int wby = 1960;
    int weekLen = 52;
    int week = 1;
    while (date.getYear() < 2400) {
        DayOfWeek loopDow = date.getDayOfWeek();
        if (date.getYear() != year) {
            year = date.getYear();
        }
        if (loopDow == MONDAY) {
            week++;
            if ((week == 53 && weekLen == 52) || week == 54) {
                week = 1;
                LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1);
                DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek();
                weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52);
                wby++;
            }
        }
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek());
        date = date.plusDays(1);
    }
}
 
Example 19
Source File: IsoFields.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static int getWeekRange(int wby) {
    LocalDate date = LocalDate.of(wby, 1, 1);
    // 53 weeks if standard year starts on Thursday, or Wed in a leap year
    if (date.getDayOfWeek() == THURSDAY || (date.getDayOfWeek() == WEDNESDAY && date.isLeapYear())) {
        return 53;
    }
    return 52;
}
 
Example 20
Source File: UseLocalDate.java    From tutorials with MIT License 4 votes vote down vote up
boolean isLeapYear(LocalDate localDate) {
    return localDate.isLeapYear();
}