Search in sources :

Example 76 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method test_parse_resolve_localizedWom_strict.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWom_strict(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField womField = week.weekOfMonth();
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(MONTH_OF_YEAR).appendLiteral(':').appendValue(womField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(STRICT);
    String str = "2012:1:0:1";
    try {
        LocalDate date = LocalDate.parse(str, f);
        assertEquals(date.getYear(), 2012);
        assertEquals(date.getMonthValue(), 1);
        assertEquals(date.get(womField), 0);
        assertEquals(date.get(DAY_OF_WEEK), 1);
    } catch (DateTimeException ex) {
    // expected
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) WeekFields(java.time.temporal.WeekFields) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 77 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method test_withDayOfWeek.

//-----------------------------------------------------------------------
// withDayOfWeek()
//-----------------------------------------------------------------------
@Test(dataProvider = "weekFields")
public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {
    // Safely in the middle of a month
    LocalDate day = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    TemporalField woyField = week.weekOfYear();
    int wom = day.get(womField);
    int woy = day.get(woyField);
    for (int dow = 1; dow <= 7; dow++) {
        LocalDate result = day.with(dowField, dow);
        assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
        assertEquals(result.get(womField), wom, "Week of Month should not change");
        assertEquals(result.get(woyField), woy, "Week of Year should not change");
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 78 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method test_dayOfWeekField.

@Test(dataProvider = "weekFields")
public void test_dayOfWeekField(DayOfWeek firstDayOfWeek, int minDays) {
    // Known to be ISO Monday
    LocalDate day = LocalDate.of(2000, 1, 10);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField f = week.dayOfWeek();
    for (int i = 1; i <= 7; i++) {
        assertEquals(day.get(f), (7 + day.getDayOfWeek().getValue() - firstDayOfWeek.getValue()) % 7 + 1);
        day = day.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 79 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method test_parse_resolve_localizedWoWBYDow_lenient.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoWBYDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();
    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(yowbyField).appendLiteral(':').appendValue(wowbyField).appendLiteral(':').appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
        int wowby = date.get(wowbyField);
        int dow = date.get(dowField);
        for (int j = wowby - 60; j < wowby + 60; j++) {
            String str = date.get(yowbyField) + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wowby), " ::" + str + ": :" + i + "::" + j);
        }
        date = date.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 80 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class DateTimeFormatterBuilder method parsePattern.

private void parsePattern(String pattern) {
    for (int pos = 0; pos < pattern.length(); pos++) {
        char cur = pattern.charAt(pos);
        if ((cur >= 'A' && cur <= 'Z') || (cur >= 'a' && cur <= 'z')) {
            int start = pos++;
            // short loop
            for (; pos < pattern.length() && pattern.charAt(pos) == cur; pos++) ;
            int count = pos - start;
            // padding
            if (cur == 'p') {
                int pad = 0;
                if (pos < pattern.length()) {
                    cur = pattern.charAt(pos);
                    if ((cur >= 'A' && cur <= 'Z') || (cur >= 'a' && cur <= 'z')) {
                        pad = count;
                        start = pos++;
                        // short loop
                        for (; pos < pattern.length() && pattern.charAt(pos) == cur; pos++) ;
                        count = pos - start;
                    }
                }
                if (pad == 0) {
                    throw new IllegalArgumentException("Pad letter 'p' must be followed by valid pad pattern: " + pattern);
                }
                // pad and continue parsing
                padNext(pad);
            }
            // main rules
            TemporalField field = FIELD_MAP.get(cur);
            if (field != null) {
                parseField(cur, count, field);
            } else if (cur == 'z') {
                if (count > 4) {
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
                } else if (count == 4) {
                    appendZoneText(TextStyle.FULL);
                } else {
                    appendZoneText(TextStyle.SHORT);
                }
            } else if (cur == 'V') {
                if (count != 2) {
                    throw new IllegalArgumentException("Pattern letter count must be 2: " + cur);
                }
                appendZoneId();
            } else if (cur == 'Z') {
                if (count < 4) {
                    appendOffset("+HHMM", "+0000");
                } else if (count == 4) {
                    appendLocalizedOffset(TextStyle.FULL);
                } else if (count == 5) {
                    appendOffset("+HH:MM:ss", "Z");
                } else {
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
                }
            } else if (cur == 'O') {
                if (count == 1) {
                    appendLocalizedOffset(TextStyle.SHORT);
                } else if (count == 4) {
                    appendLocalizedOffset(TextStyle.FULL);
                } else {
                    throw new IllegalArgumentException("Pattern letter count must be 1 or 4: " + cur);
                }
            } else if (cur == 'X') {
                if (count > 5) {
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
                }
                appendOffset(OffsetIdPrinterParser.PATTERNS[count + (count == 1 ? 0 : 1)], "Z");
            } else if (cur == 'x') {
                if (count > 5) {
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
                }
                String zero = (count == 1 ? "+00" : (count % 2 == 0 ? "+0000" : "+00:00"));
                appendOffset(OffsetIdPrinterParser.PATTERNS[count + (count == 1 ? 0 : 1)], zero);
            } else if (cur == 'W') {
                // Fields defined by Locale
                if (count > 1) {
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
                }
                appendInternal(new WeekBasedFieldPrinterParser(cur, count));
            } else if (cur == 'w') {
                // Fields defined by Locale
                if (count > 2) {
                    throw new IllegalArgumentException("Too many pattern letters: " + cur);
                }
                appendInternal(new WeekBasedFieldPrinterParser(cur, count));
            } else if (cur == 'Y') {
                // Fields defined by Locale
                appendInternal(new WeekBasedFieldPrinterParser(cur, count));
            } else {
                throw new IllegalArgumentException("Unknown pattern letter: " + cur);
            }
            pos--;
        } else if (cur == '\'') {
            // parse literals
            int start = pos++;
            for (; pos < pattern.length(); pos++) {
                if (pattern.charAt(pos) == '\'') {
                    if (pos + 1 < pattern.length() && pattern.charAt(pos + 1) == '\'') {
                        pos++;
                    } else {
                        // end of literal
                        break;
                    }
                }
            }
            if (pos >= pattern.length()) {
                throw new IllegalArgumentException("Pattern ends with an incomplete string literal: " + pattern);
            }
            String str = pattern.substring(start + 1, pos);
            if (str.length() == 0) {
                appendLiteral('\'');
            } else {
                appendLiteral(str.replace("''", "'"));
            }
        } else if (cur == '[') {
            optionalStart();
        } else if (cur == ']') {
            if (active.parent == null) {
                throw new IllegalArgumentException("Pattern invalid as it contains ] without previous [");
            }
            optionalEnd();
        } else if (cur == '{' || cur == '}' || cur == '#') {
            throw new IllegalArgumentException("Pattern includes reserved character: '" + cur + "'");
        } else {
            appendLiteral(cur);
        }
    }
}
Also used : TemporalField(java.time.temporal.TemporalField)

Aggregations

TemporalField (java.time.temporal.TemporalField)104 Test (org.testng.annotations.Test)95 HashMap (java.util.HashMap)56 DateTimeException (java.time.DateTimeException)48 LocalDate (java.time.LocalDate)38 WeekFields (java.time.temporal.WeekFields)25 AbstractTCKTest (tck.java.time.AbstractTCKTest)25 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)20 DateTimeFormatter (java.time.format.DateTimeFormatter)19 TemporalAccessor (java.time.temporal.TemporalAccessor)13 JapaneseDate (java.time.chrono.JapaneseDate)12 HijrahDate (java.time.chrono.HijrahDate)11 MinguoDate (java.time.chrono.MinguoDate)10 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)10 Chronology (java.time.chrono.Chronology)4 IsoChronology (java.time.chrono.IsoChronology)4 ChronoLocalDate (java.time.chrono.ChronoLocalDate)3 HijrahChronology (java.time.chrono.HijrahChronology)3 JapaneseChronology (java.time.chrono.JapaneseChronology)3 MinguoChronology (java.time.chrono.MinguoChronology)3