Search in sources :

Example 66 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFields method test_parse_resolve_localizedWoWBY.

// -----------------------------------------------------------------------
@Test
@UseDataProvider("data_weekFields")
public void test_parse_resolve_localizedWoWBY(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    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(DAY_OF_WEEK).toFormatter();
        String str = date.get(yowbyField) + ":" + date.get(wowbyField) + ":" + date.get(DAY_OF_WEEK);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(" :: " + str + " " + i, parsed, date);
        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.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 67 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFields method test_equals.

// -----------------------------------------------------------------------
// equals() and hashCode().
// -----------------------------------------------------------------------
@Test
public void test_equals() {
    WeekFields weekDef_iso = WeekFields.ISO;
    WeekFields weekDef_sundayStart = WeekFields.SUNDAY_START;
    assertTrue(weekDef_iso.equals(WeekFields.of(DayOfWeek.MONDAY, 4)));
    assertTrue(weekDef_sundayStart.equals(WeekFields.of(DayOfWeek.SUNDAY, 1)));
    assertEquals(weekDef_iso.hashCode(), WeekFields.of(DayOfWeek.MONDAY, 4).hashCode());
    assertEquals(weekDef_sundayStart.hashCode(), WeekFields.of(DayOfWeek.SUNDAY, 1).hashCode());
    assertFalse(weekDef_iso.equals(weekDef_sundayStart));
    assertNotEquals(weekDef_iso.hashCode(), weekDef_sundayStart.hashCode());
}
Also used : WeekFields(java.time.temporal.WeekFields) Test(org.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 68 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKLocalizedFieldParser method test_parse_WeekBasedYear.

@Test
@UseDataProvider("provider_patternLocalWeekBasedYearDate")
public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        WeekFields weekDef = WeekFields.of(locale);
        assertEquals("Incorrect ending parse position", ppos.getIndex(), expectedPos);
        assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0);
        // ensure combination resolves into a date
        LocalDate result = LocalDate.parse(text, dtf);
        assertEquals("LocalDate incorrect for " + pattern + ", weekDef: " + weekDef, result, expectedValue);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) WeekFields(java.time.temporal.WeekFields) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 69 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFieldsSerialization method test_serializable_singleton.

// -----------------------------------------------------------------------
@Test
@UseDataProvider("data_weekFields")
public void test_serializable_singleton(DayOfWeek firstDayOfWeek, int minDays) throws IOException, ClassNotFoundException {
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    // spec state singleton
    assertSerializableSame(weekDef);
}
Also used : WeekFields(java.time.temporal.WeekFields) Test(org.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 70 with WeekFields

use of java.time.temporal.WeekFields in project dhis2-core by dhis2.

the class WeeklyAbstractPeriodType method getIsoDate.

@Override
public String getIsoDate(DateTimeUnit dateTimeUnit, Calendar calendar) {
    int year;
    int week;
    if (calendar.isIso8601()) {
        LocalDate date = LocalDate.of(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay());
        WeekFields weekFields = WeekFields.of(PeriodType.MAP_WEEK_TYPE.get(getName()), 4);
        year = date.get(weekFields.weekBasedYear());
        week = date.get(weekFields.weekOfWeekBasedYear());
    } else {
        dateTimeUnit = adjustToStartOfWeek(dateTimeUnit, calendar);
        week = calendar.week(dateTimeUnit);
        if (week == 1 && dateTimeUnit.getMonth() == calendar.monthsInYear()) {
            dateTimeUnit.setYear(dateTimeUnit.getYear() + 1);
        }
        year = dateTimeUnit.getYear();
    }
    return String.format("%d%s%d", year, weekPrefix, week);
}
Also used : WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate)

Aggregations

WeekFields (java.time.temporal.WeekFields)73 AbstractTCKTest (tck.java.time.AbstractTCKTest)58 LocalDate (java.time.LocalDate)57 TemporalField (java.time.temporal.TemporalField)51 DateTimeFormatter (java.time.format.DateTimeFormatter)36 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)34 Test (org.junit.Test)33 Test (org.testng.annotations.Test)33 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)31 DayOfWeek (java.time.DayOfWeek)7 DateTimeException (java.time.DateTimeException)6 ValueRange (java.time.temporal.ValueRange)6 ParsePosition (java.text.ParsePosition)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 GregorianCalendar (java.util.GregorianCalendar)2