Java Code Examples for java.time.LocalDate#withDayOfYear()
The following examples show how to use
java.time.LocalDate#withDayOfYear() .
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: TCKWeekFields.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 2
Source File: LocalDatePeriodCountCalculator.java From objectlabkit with Apache License 2.0 | 5 votes |
@Override public double yearDiff(final LocalDate start, final LocalDate end, final PeriodCountBasis basis) { double diff = 0.0; switch (basis) { case ACT_ACT: final int startYear = start.getYear(); final int endYear = end.getYear(); if (startYear != endYear) { final LocalDate endOfStartYear = start.with(TemporalAdjusters.lastDayOfYear()); final LocalDate startOfEndYear = end.withDayOfYear(1); final long diff1 = ChronoUnit.DAYS.between(start, endOfStartYear); final long diff2 = ChronoUnit.DAYS.between(startOfEndYear, end); diff = (diff1 + 1.0) / start.lengthOfYear() + (endYear - startYear - 1.0) + (double) diff2 / (double) end.lengthOfYear(); } break; case CONV_30_360: case CONV_360E_ISDA: case CONV_360E_ISMA: case ACT_360: diff = dayDiff(start, end, basis) / CalculatorConstants.YEAR_360_0; break; case ACT_365: diff = dayDiff(start, end, basis) / CalculatorConstants.YEAR_365_0; break; default: throw new UnsupportedOperationException("Sorry ACT_UST is not supported"); } return diff; }
Example 3
Source File: TCKWeekFields.java From j2objc with Apache License 2.0 | 5 votes |
@Test @UseDataProvider("data_weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals("Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result), result, day); day = day.plusDays(1); } }
Example 4
Source File: TCKWeekFields.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 5
Source File: TCKWeekFields.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 6
Source File: TCKWeekFields.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 7
Source File: TCKWeekFields.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 8
Source File: TCKWeekFields.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 9
Source File: TCKWeekFields.java From hottub with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 10
Source File: TCKWeekFields.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 11
Source File: TCKWeekFields.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 12
Source File: TCKWeekFields.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 13
Source File: TCKWeekFields.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 14
Source File: TCKWeekFields.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 15
Source File: TCKWeekFields.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="weekFields") public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) { LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday WeekFields week = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = week.dayOfWeek(); TemporalField woyField = week.weekOfYear(); for (int i = 1; i <= 15; i++) { int actualDOW = day.get(dowField); int actualWOY = day.get(woyField); // Verify that the combination of day of week and week of month can be used // to reconstruct the same date. LocalDate day1 = day.withDayOfYear(1); int offset = - (day1.get(dowField) - 1); int week1 = day1.get(woyField); if (week1 == 0) { // week of the 1st is partial; start with first full week offset += 7; } offset += actualDOW - 1; offset += (actualWOY - 1) * 7; LocalDate result = day1.plusDays(offset); assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result)); day = day.plusDays(1); } }
Example 16
Source File: DayCountConvention_ACT_ACT_YEARFRAC.java From finmath-lib with Apache License 2.0 | 4 votes |
@Override public double getDaycountFraction(final LocalDate startDate, final LocalDate endDate) { if(startDate.isAfter(endDate)) { return -getDaycountFraction(endDate,startDate); } /* * Determine the denominator of act/act. */ double denominator; final LocalDate startDatePlusOneYear = startDate.plusYears(1); if(endDate.isAfter(startDatePlusOneYear)) { /* * The following method applies, if the interval spans more than one year: * fraction = actual number of days / average number of days per year. */ final LocalDate startDateYearStart = startDate.withDayOfYear(1); final LocalDate endDateYearEnd = endDate.withDayOfYear(endDate.lengthOfYear()).plusDays(1); final double spannedYears = endDate.getYear() - startDate.getYear() + 1; denominator = getDaycount(startDateYearStart, endDateYearEnd) / spannedYears; } else { final boolean isStartLeapYear = startDate.isLeapYear(); final boolean isEndLeapYear = endDate.isLeapYear(); /* * If the start and end span less or equal one year: * If start and end fall in a leap year, use ACT/366. */ if(isStartLeapYear && isEndLeapYear) { denominator = 366.0; } else /* * If the start and end span less or equal one year: * If either falls in a leap year and Feb 29th of that leap year lies in between start and end (or is equal to start or end), use ACT/366. */ if(isStartLeapYear || isEndLeapYear) { // Get February 29th of the respective leap year final LocalDate leapYearsFeb29th = isStartLeapYear ? LocalDate.of(startDate.getYear(), Month.FEBRUARY, 29) : LocalDate.of(endDate.getYear(), Month.FEBRUARY, 29); // Check position of February 29th if(startDate.compareTo(leapYearsFeb29th) <= 0 && endDate.compareTo(leapYearsFeb29th) >= 0) { denominator = 366.0; } else { denominator = 365.0; } } else { /* * If the start and end span less or equal one year: * If none of the above applies, use denominator = ACT/365. */ denominator = 365.0; } } final double daycountFraction = getDaycount(startDate, endDate) / denominator; return daycountFraction; }
Example 17
Source File: DayCountConvention_ACT_ACT_YEARFRAC.java From finmath-lib with Apache License 2.0 | 4 votes |
@Override public double getDaycountFraction(final LocalDate startDate, final LocalDate endDate) { if(startDate.isAfter(endDate)) { return -getDaycountFraction(endDate,startDate); } /* * Determine the denominator of act/act. */ double denominator; final LocalDate startDatePlusOneYear = startDate.plusYears(1); if(endDate.isAfter(startDatePlusOneYear)) { /* * The following method applies, if the interval spans more than one year: * fraction = actual number of days / average number of days per year. */ final LocalDate startDateYearStart = startDate.withDayOfYear(1); final LocalDate endDateYearEnd = endDate.withDayOfYear(endDate.lengthOfYear()).plusDays(1); final double spannedYears = endDate.getYear() - startDate.getYear() + 1; denominator = getDaycount(startDateYearStart, endDateYearEnd) / spannedYears; } else { final boolean isStartLeapYear = startDate.isLeapYear(); final boolean isEndLeapYear = endDate.isLeapYear(); /* * If the start and end span less or equal one year: * If start and end fall in a leap year, use ACT/366. */ if(isStartLeapYear && isEndLeapYear) { denominator = 366.0; } else /* * If the start and end span less or equal one year: * If either falls in a leap year and Feb 29th of that leap year lies in between start and end (or is equal to start or end), use ACT/366. */ if(isStartLeapYear || isEndLeapYear) { // Get February 29th of the respective leap year final LocalDate leapYearsFeb29th = isStartLeapYear ? LocalDate.of(startDate.getYear(), Month.FEBRUARY, 29) : LocalDate.of(endDate.getYear(), Month.FEBRUARY, 29); // Check position of February 29th if(startDate.compareTo(leapYearsFeb29th) <= 0 && endDate.compareTo(leapYearsFeb29th) >= 0) { denominator = 366.0; } else { denominator = 365.0; } } else { /* * If the start and end span less or equal one year: * If none of the above applies, use denominator = ACT/365. */ denominator = 365.0; } } final double daycountFraction = getDaycount(startDate, endDate) / denominator; return daycountFraction; }