use of java.time.DayOfWeek 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.DayOfWeek in project jdk8u_jdk by JetBrains.
the class TCKWeekFields method data_weekFields.
//-----------------------------------------------------------------------
@DataProvider(name = "weekFields")
Object[][] data_weekFields() {
Object[][] objects = new Object[49][];
int i = 0;
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
objects[i++] = new Object[] { firstDayOfWeek, minDays };
}
}
return objects;
}
use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.
the class TCKTemporalAdjusters method test_previousOrCurrent.
@Test
public void test_previousOrCurrent() {
for (Month month : Month.values()) {
for (int i = 1; i <= month.length(false); i++) {
LocalDate date = date(2007, month, i);
for (DayOfWeek dow : DayOfWeek.values()) {
LocalDate test = (LocalDate) TemporalAdjusters.previousOrSame(dow).adjustInto(date);
assertSame(test.getDayOfWeek(), dow);
if (test.getYear() == 2007) {
int dayDiff = test.getDayOfYear() - date.getDayOfYear();
assertTrue(dayDiff <= 0 && dayDiff > -7);
assertEquals(date.equals(test), date.getDayOfWeek() == dow);
} else {
assertFalse(date.getDayOfWeek() == dow);
assertSame(month, Month.JANUARY);
assertTrue(date.getDayOfMonth() < 7);
assertEquals(test.getYear(), 2006);
assertSame(test.getMonth(), Month.DECEMBER);
assertTrue(test.getDayOfMonth() > 25);
}
}
}
}
}
use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.
the class TCKLocalDateTime method test_getDayOfWeek.
//-----------------------------------------------------------------------
// getDayOfWeek()
//-----------------------------------------------------------------------
@Test
public void test_getDayOfWeek() {
DayOfWeek dow = DayOfWeek.MONDAY;
for (Month month : Month.values()) {
int length = month.length(false);
for (int i = 1; i <= length; i++) {
LocalDateTime d = LocalDateTime.of(LocalDate.of(2007, month, i), TEST_2007_07_15_12_30_40_987654321.toLocalTime());
assertSame(d.getDayOfWeek(), dow);
dow = dow.plus(1);
}
}
}
use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.
the class TCKIsoFields method test_loop.
//-----------------------------------------------------------------------
public void test_loop() {
// loop round at least one 400 year cycle, including before 1970
// Tuseday of week 1 1960
LocalDate date = LocalDate.of(1960, 1, 5);
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);
}
}
Aggregations