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

The following examples show how to use java.time.LocalDate#getDayOfWeek() . 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: ImmutableHolidayCalendarTest.java    From Strata with Apache License 2.0 6 votes vote down vote up
@Test
public void test_broadCheck() {
  LocalDate start = LocalDate.of(2010, 1, 1);
  LocalDate end = LocalDate.of(2020, 1, 1);
  Random random = new Random(547698);
  for (int i = 0; i < 10; i++) {
    // create sample holiday dates
    LocalDate date = start;
    SortedSet<LocalDate> set = new TreeSet<>();
    while (date.isBefore(end)) {
      set.add(date);
      date = date.plusDays(random.nextInt(10) + 1);
    }
    // check holiday calendar works using simple algorithm
    ImmutableHolidayCalendar test = ImmutableHolidayCalendar.of(
        HolidayCalendarId.of("TestBroad" + i), set, SATURDAY, SUNDAY);
    LocalDate checkDate = start;
    while (checkDate.isBefore(end)) {
      DayOfWeek dow = checkDate.getDayOfWeek();
      assertThat(test.isHoliday(checkDate)).isEqualTo(dow == SATURDAY || dow == SUNDAY || set.contains(checkDate));
      checkDate = checkDate.plusDays(1);
    }
  }
}
 
Example 2
Source File: MarketDayUtil.java    From java-trader with Apache License 2.0 6 votes vote down vote up
public static LocalDate nextMarketDay(Exchange exchange, LocalDate tradingDay){
    if ( exchange==null ) {
        exchange = Exchange.SSE;
    }
    List<LocalDate> closeDays = closeDayMap.get(exchange.name());
    while(true){
        tradingDay = tradingDay.plusDays(1);
        DayOfWeek dayOfWeek = tradingDay.getDayOfWeek();
        if ( dayOfWeek == DayOfWeek.SUNDAY || dayOfWeek==DayOfWeek.SATURDAY) {
            continue;
        }
        if ( closeDays!=null && closeDays.contains(tradingDay) ) {
            continue;
        }
        return tradingDay;
    }
}
 
Example 3
Source File: FullDemo.java    From LGoodDatePicker with MIT License 6 votes vote down vote up
/**
 * getHighlightInformationOrNull, Implement this function to indicate if a date should be
 * highlighted, and what highlighting details should be used for the highlighted date.
 *
 * If a date should be highlighted, then return an instance of HighlightInformation. If the
 * date should not be highlighted, then return null.
 *
 * You may (optionally) fill out the fields in the HighlightInformation class to give any
 * particular highlighted day a unique foreground color, background color, or tooltip text.
 * If the color fields are null, then the default highlighting colors will be used. If the
 * tooltip field is null (or empty), then no tooltip will be displayed.
 *
 * Dates that are passed to this function will never be null.
 */
@Override
public HighlightInformation getHighlightInformationOrNull(LocalDate date) {
    // Highlight a chosen date, with a tooltip and a red background color.
    if (date.getDayOfMonth() == 25) {
        return new HighlightInformation(Color.red, null, "It's the 25th!");
    }
    // Highlight all Saturdays with a unique background and foreground color.
    if (date.getDayOfWeek() == DayOfWeek.SATURDAY) {
        return new HighlightInformation(Color.orange, Color.yellow, "It's Saturday!");
    }
    // Highlight all Sundays with default colors and a tooltip.
    if (date.getDayOfWeek() == DayOfWeek.SUNDAY) {
        return new HighlightInformation(null, null, "It's Sunday!");
    }
    // All other days should not be highlighted.
    return null;
}
 
Example 4
Source File: RelativeDate.java    From baleen with Apache License 2.0 6 votes vote down vote up
private void createRelativeWeek(
    TextBlock block, Integer charBegin, Integer charEnd, Integer weekOffset) {
  Temporal t = new Temporal(block.getJCas());
  block.setBeginAndEnd(t, charBegin, charEnd);

  t.setConfidence(1.0);
  t.setPrecision(PRECISION_RELATIVE);
  t.setScope(SCOPE_SINGLE);
  t.setTemporalType(TYPE_DATE);

  if (relativeTo != null && weekOffset != null) {
    LocalDate startOfWeek = relativeTo.plusWeeks(weekOffset);

    while (startOfWeek.getDayOfWeek() != DayOfWeek.MONDAY) {
      startOfWeek = startOfWeek.minusDays(1);
    }

    t.setTimestampStart(startOfWeek.atStartOfDay(ZoneOffset.UTC).toEpochSecond());

    LocalDate endOfWeek = startOfWeek.plusWeeks(1);
    t.setTimestampStop(endOfWeek.atStartOfDay(ZoneOffset.UTC).toEpochSecond());
  }

  addToJCasIndex(t);
}
 
Example 5
Source File: GlobalHolidayCalendars.java    From Strata with Apache License 2.0 5 votes vote down vote up
private static LocalDate christmasBumpedSatSun(int year) {
  LocalDate base = LocalDate.of(year, 12, 25);
  if (base.getDayOfWeek() == SATURDAY || base.getDayOfWeek() == SUNDAY) {
    return LocalDate.of(year, 12, 27);
  }
  return base;
}
 
Example 6
Source File: IsoFields.java    From j2objc 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 7
Source File: IsoFields.java    From hottub 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 8
Source File: CalendarPanelAssortmentTest.java    From LGoodDatePicker with MIT License 5 votes vote down vote up
/**
 * isDateAllowed, Return true if a date should be allowed, or false if a date should be
 * vetoed.
 */
@Override
public boolean isDateAllowed(LocalDate date) {
    // Disallow days 7 to 11.
    if ((date.getDayOfMonth() >= 7) && (date.getDayOfMonth() <= 11)) {
        return false;
    }
    // Disallow odd numbered saturdays.
    if ((date.getDayOfWeek() == DayOfWeek.SATURDAY) && ((date.getDayOfMonth() % 2) == 1)) {
        return false;
    }
    // Allow all other days.
    return true;
}
 
Example 9
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 10
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 11
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 12
Source File: TimerChecker.java    From FX-AlgorithmTrading with MIT License 5 votes vote down vote up
private LocalDateTime culcurateNextDailyUpdate(LocalDate previousDate) {
    // 更新は火-土
    LocalDate nextDate = previousDate.plusDays(1);
    while (nextDate.getDayOfWeek() == DayOfWeek.SUNDAY || nextDate.getDayOfWeek() == DayOfWeek.MONDAY) {
        nextDate = nextDate.plusDays(1);
    }
    // 夏冬考慮
    return LocalDateTime.of(nextDate, MarketTimeUtility.getEODTime(nextDate));
}
 
Example 13
Source File: TCKWeekFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    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);
    }
}
 
Example 14
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_brbd")
public void test_brbd(int year, List<LocalDate> holidays) {
  LocalDate date = LocalDate.of(year, 1, 1);
  int len = date.lengthOfYear();
  for (int i = 0; i < len; i++) {
    boolean isHoliday = holidays.contains(date) || date.getDayOfWeek() == SATURDAY || date.getDayOfWeek() == SUNDAY;
    assertThat(BRBD.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 15
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 16
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_dkco")
public void test_dkco(int year, List<LocalDate> holidays) {
  LocalDate date = LocalDate.of(year, 1, 1);
  int len = date.lengthOfYear();
  for (int i = 0; i < len; i++) {
    boolean isHoliday = holidays.contains(date) || date.getDayOfWeek() == SATURDAY || date.getDayOfWeek() == SUNDAY;
    assertThat(DKCO.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 17
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_jpto")
public void test_jpto(int year, List<LocalDate> holidays) {
  LocalDate date = LocalDate.of(year, 1, 1);
  int len = date.lengthOfYear();
  for (int i = 0; i < len; i++) {
    boolean isHoliday = holidays.contains(date) || date.getDayOfWeek() == SATURDAY || date.getDayOfWeek() == SUNDAY;
    assertThat(JPTO.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}
 
Example 18
Source File: OnDayOfMonthValueGenerator.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
private int generateValue(final On on, final int year, final int month) throws NoSuchValueException {
    final int dayOfMonth = on.getTime().getValue();
    switch (on.getSpecialChar().getValue()) {
        case L:
            final int daysBefore = on.getNth().getValue();
            return LocalDate.of(year, month, 1).lengthOfMonth() - (daysBefore > 0 ? daysBefore : 0);
        case W: // First work day of the week
            final LocalDate doM = LocalDate.of(year, month, dayOfMonth);
            if (doM.getDayOfWeek() == DayOfWeek.SATURDAY) { //dayOfWeek is Saturday!
                if (dayOfMonth == 1) { //first day in month is Saturday! We execute on Monday
                    return 3;
                }
                return dayOfMonth - 1;
            }
            if (doM.getDayOfWeek() == DayOfWeek.SUNDAY && (dayOfMonth + 1) <= doM.lengthOfMonth()) {
                return dayOfMonth + 1;
            }
            return dayOfMonth;  // first day of week is a weekday
        case LW:
            final LocalDate lastDayOfMonth = LocalDate.of(year, month, LocalDate.of(year, month, 1).lengthOfMonth());
            final int dow = lastDayOfMonth.getDayOfWeek().getValue();
            final int diff = dow - 5;
            if (diff > 0) {
                return lastDayOfMonth.minusDays(diff).getDayOfMonth();
            }
            return lastDayOfMonth.getDayOfMonth();
        default:
            throw new NoSuchValueException();
    }
}
 
Example 19
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 20
Source File: GlobalHolidayCalendarsTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("data_defr")
public void test_defr(int year, List<LocalDate> holidays) {
  LocalDate date = LocalDate.of(year, 1, 1);
  int len = date.lengthOfYear();
  for (int i = 0; i < len; i++) {
    boolean isHoliday = holidays.contains(date) || date.getDayOfWeek() == SATURDAY || date.getDayOfWeek() == SUNDAY;
    assertThat(DEFR.isHoliday(date)).as(date.toString()).isEqualTo(isHoliday);
    date = date.plusDays(1);
  }
}