use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.
the class TCKWeekFields method test_weekOfWeekBasedYearField.
/**
* Verify that the date can be reconstructed from the DOW, WeekOfWeekBasedYear,
* and WeekBasedYear for every combination of start of week
* and minimal days in week.
* @param firstDayOfWeek the first day of the week
* @param minDays the minimum number of days in the week
*/
@Test(dataProvider = "weekFields")
public void test_weekOfWeekBasedYearField(DayOfWeek firstDayOfWeek, int minDays) {
// Known to be ISO Monday
LocalDate day = LocalDate.of(2012, 12, 31);
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = weekDef.dayOfWeek();
TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
TemporalField yowbyField = weekDef.weekBasedYear();
for (int i = 1; i <= 15; i++) {
int actualDOW = day.get(dowField);
int actualWOWBY = day.get(wowbyField);
int actualYOWBY = day.get(yowbyField);
// Verify that the combination of day of week and week of month can be used
// to reconstruct the same date.
LocalDate day1 = LocalDate.of(actualYOWBY, 1, 1);
DayOfWeek isoDOW = day1.getDayOfWeek();
int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;
int weekStart = Math.floorMod(1 - dow, 7);
if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
// The previous week has the minimum days in the current month to be a 'week'
weekStart -= 7;
}
weekStart += actualDOW - 1;
weekStart += (actualWOWBY - 1) * 7;
LocalDate result = day1.plusDays(weekStart);
assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, weekStart: %s, actualDOW: %s, actualWOWBY: %s, YearOfWBY: %d, expected day: %s, result: %s%n", weekDef, day.getDayOfWeek(), weekStart, actualDOW, actualWOWBY, actualYOWBY, day, result));
day = day.plusDays(1);
}
}
use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.
the class TCKWeekFields method test_parse_resolve_localizedWoy_strict.
@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoy_strict(DayOfWeek firstDayOfWeek, int minDays) {
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField woyField = week.weekOfYear();
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(woyField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(STRICT);
String str = "2012:0:1";
try {
LocalDate date = LocalDate.parse(str, f);
assertEquals(date.getYear(), 2012);
assertEquals(date.get(woyField), 0);
assertEquals(date.get(DAY_OF_WEEK), 1);
} catch (DateTimeException ex) {
// expected
}
}
use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.
the class TCKWeekFields method test_parse_resolve_localizedWom.
//-----------------------------------------------------------------------
@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWom(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField womField = week.weekOfMonth();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(MONTH_OF_YEAR).appendLiteral(':').appendValue(womField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(SMART);
String str = date.getYear() + ":" + date.getMonthValue() + ":" + date.get(womField) + ":" + date.get(DAY_OF_WEEK);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date, " ::" + str + "::" + i);
date = date.plusDays(1);
}
}
use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.
the class TCKWeekFields method test_withWeekOfWeekBasedYear.
@Test(dataProvider = "weekFields")
public void test_withWeekOfWeekBasedYear(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate day = LocalDate.of(2012, 12, 31);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField wowbyField = week.weekOfWeekBasedYear();
TemporalField yowbyField = week.weekBasedYear();
int dowExpected = (day.get(dowField) - 1) % 7 + 1;
LocalDate dowDate = day.with(dowField, dowExpected);
int dowResult = dowDate.get(dowField);
assertEquals(dowResult, dowExpected, "Localized DayOfWeek not correct; " + day + " -->" + dowDate);
int weekExpected = day.get(wowbyField) + 1;
ValueRange range = day.range(wowbyField);
weekExpected = ((weekExpected - 1) % (int) range.getMaximum()) + 1;
LocalDate weekDate = day.with(wowbyField, weekExpected);
int weekResult = weekDate.get(wowbyField);
assertEquals(weekResult, weekExpected, "Localized WeekOfWeekBasedYear not correct; " + day + " -->" + weekDate);
int yearExpected = day.get(yowbyField) + 1;
LocalDate yearDate = day.with(yowbyField, yearExpected);
int yearResult = yearDate.get(yowbyField);
assertEquals(yearResult, yearExpected, "Localized WeekBasedYear not correct; " + day + " --> " + yearDate);
range = yearDate.range(wowbyField);
weekExpected = Math.min(day.get(wowbyField), (int) range.getMaximum());
int weekActual = yearDate.get(wowbyField);
assertEquals(weekActual, weekExpected, "Localized WeekOfWeekBasedYear week should not change; " + day + " --> " + yearDate + ", actual: " + weekActual + ", weekExpected: " + weekExpected);
}
use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.
the class TCKWeekFields method test_parse_resolve_localizedWoyDow.
//-----------------------------------------------------------------------
@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoyDow(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate date = LocalDate.of(2012, 12, 15);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField woyField = week.weekOfYear();
for (int i = 1; i <= 60; i++) {
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(MONTH_OF_YEAR).appendLiteral(':').appendValue(woyField).appendLiteral(':').appendValue(dowField).toFormatter();
String str = date.getYear() + ":" + date.getMonthValue() + ":" + date.get(woyField) + ":" + date.get(dowField);
LocalDate parsed = LocalDate.parse(str, f);
assertEquals(parsed, date, " :: " + str + " " + i);
date = date.plusDays(1);
}
}
Aggregations