Search in sources :

Example 16 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class TCKWeekFieldsSerialization 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;
}
Also used : DayOfWeek(java.time.DayOfWeek) DataProvider(org.testng.annotations.DataProvider)

Example 17 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class TestIsoChronoImpl method test_DayOfWeek_IsoChronology_vsCalendar.

//-----------------------------------------------------------------------
// Verify  ISO Calendar matches java.util.Calendar
// DayOfWeek, WeekOfMonth, WeekOfYear for range
//-----------------------------------------------------------------------
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
    GregorianCalendar cal = new GregorianCalendar();
    assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
    LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
    for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
        for (int minDays = 1; minDays <= 7; minDays++) {
            WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
            cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
            cal.setMinimalDaysInFirstWeek(minDays);
            cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
            cal.set(Calendar.YEAR, isoDate.get(YEAR));
            cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
            cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
            // For every date in the range
            while (isoDate.isBefore(isoEndDate)) {
                assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + ";  cal: " + cal);
                assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
                assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
                int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
                int dow = isoDate.get(weekDef.dayOfWeek());
                assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
                int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
                int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
                assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
                int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
                int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
                assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
                int jWeekYear = cal.getWeekYear();
                int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
                assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
                int jweeksInWeekyear = cal.getWeeksInWeekYear();
                int weeksInWeekBasedYear = (int) isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
                assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
                isoDate = isoDate.plus(1, ChronoUnit.DAYS);
                cal.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
    }
}
Also used : DayOfWeek(java.time.DayOfWeek) GregorianCalendar(java.util.GregorianCalendar) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 18 with DayOfWeek

use of java.time.DayOfWeek in project jgnash by ccavanaugh.

the class UpdateFactory method shouldAutomaticUpdateOccur.

/**
     * Determines if an automatic update is recommended.
     * <p>
     * The current approach is to avoid multiple updates on Saturday or Sunday if one has already occurred.
     * This could be expanded to understand locale rules.
     *
     * @param lastUpdate the last known timestamp for an update to have occurred
     * @return true if an update is recommended
     */
public static boolean shouldAutomaticUpdateOccur(final LocalDateTime lastUpdate) {
    boolean result = true;
    final LocalDate lastDate = LocalDate.from(lastUpdate);
    final DayOfWeek lastDayOfWeek = lastDate.getDayOfWeek();
    if (lastDayOfWeek == DayOfWeek.SATURDAY || lastDayOfWeek == DayOfWeek.SUNDAY) {
        if (LocalDate.now().equals(lastDate) || (LocalDate.now().minusDays(1).equals(lastDate)) && lastDayOfWeek == DayOfWeek.SATURDAY) {
            result = false;
        }
    }
    if (result && LocalDate.now().equals(lastDate)) {
        // check for an after hours update
        switch(Locale.getDefault().getCountry()) {
            case "AU":
            case "CA":
            case "HK":
            case "US":
                final ZonedDateTime zdtUS = lastUpdate.atZone(ZoneId.of("UTC").normalized());
                if (zdtUS.getHour() >= 21 && zdtUS.getMinute() > 25) {
                    // 4:25 EST for delayed online sources
                    result = false;
                }
                break;
            case // UK
            "GB":
                final ZonedDateTime zdtUK = lastUpdate.atZone(ZoneId.of("UTC").normalized());
                if (zdtUK.getHour() >= 21 && zdtUK.getMinute() > 55) {
                    // 4:55 EST for delayed online sources
                    result = false;
                }
                break;
            case // India
            "IN":
                final ZonedDateTime zdtIN = lastUpdate.atZone(ZoneId.of("UTC").normalized());
                if (zdtIN.getHour() >= 20 && zdtIN.getMinute() > 55) {
                    // 3:55 EST for delayed online sources
                    result = false;
                }
                break;
            default:
                break;
        }
    }
    return result;
}
Also used : DayOfWeek(java.time.DayOfWeek) ZonedDateTime(java.time.ZonedDateTime) LocalDate(java.time.LocalDate)

Aggregations

DayOfWeek (java.time.DayOfWeek)18 LocalDate (java.time.LocalDate)10 Test (org.testng.annotations.Test)9 Month (java.time.Month)8 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 WeekFields (java.time.temporal.WeekFields)2 DataProvider (org.testng.annotations.DataProvider)2 Instant (java.time.Instant)1 LocalTime (java.time.LocalTime)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 TemporalField (java.time.temporal.TemporalField)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 Locale (java.util.Locale)1 Callable (java.util.concurrent.Callable)1 AbstractTCKTest (tck.java.time.AbstractTCKTest)1