java.time.chrono.ChronoLocalDate Java Examples

The following examples show how to use java.time.chrono.ChronoLocalDate. 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: WeekFields.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the week of week-based-year for the temporal.
 * The week can be part of the previous year, the current year,
 * or the next year depending on the week start and minimum number
 * of days.
 * @param temporal  a date of any chronology
 * @return the week of the year
 * @see #localizedWeekBasedYear(java.time.temporal.TemporalAccessor)
 */
private int localizedWeekOfWeekBasedYear(TemporalAccessor temporal) {
    int dow = localizedDayOfWeek(temporal);
    int doy = temporal.get(DAY_OF_YEAR);
    int offset = startOfWeekOffset(doy, dow);
    int week = computeWeek(offset, doy);
    if (week == 0) {
        // Day is in end of week of previous year
        // Recompute from the last day of the previous year
        ChronoLocalDate date = Chronology.from(temporal).date(temporal);
        date = date.minus(doy, DAYS);   // Back down into previous year
        return localizedWeekOfWeekBasedYear(date);
    } else if (week > 50) {
        // If getting close to end of year, use higher precision logic
        // Check if date of year is in partial week associated with next year
        ValueRange dayRange = temporal.range(DAY_OF_YEAR);
        int yearLen = (int)dayRange.getMaximum();
        int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
        if (week >= newYearWeek) {
            // Overlaps with week of following year; reduce to week in following year
            week = week - newYearWeek + 1;
        }
    }
    return week;
}
 
Example #2
Source File: TCKChronoLocalDate.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badMinusTemporalUnitChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDate date = chrono.date(refDate);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDate date2 = chrono2.date(refDate);
        TemporalUnit adjuster = new FixedTemporalUnit(date2);
        if (chrono != chrono2) {
            try {
                date.minus(1, adjuster);
                Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass()
                        + ", can not be cast to " + date2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDate result = date.minus(1, adjuster);
            assertEquals(result, date2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #3
Source File: TCKChronoLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badPlusTemporalUnitChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDate date = chrono.date(refDate);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDate date2 = chrono2.date(refDate);
        TemporalUnit adjuster = new FixedTemporalUnit(date2);
        if (chrono != chrono2) {
            try {
                date.plus(1, adjuster);
                Assert.fail("TemporalUnit.doAdd plus should have thrown a ClassCastException" + date.getClass()
                        + ", can not be cast to " + date2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDate result = date.plus(1, adjuster);
            assertEquals(result, date2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #4
Source File: TestReducedParser.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYearOfEra(ChronoLocalDate date) {
    Chronology chrono = date.getChronology();
    DateTimeFormatter df
            = new DateTimeFormatterBuilder().appendValueReduced(YEAR_OF_ERA, 2, 2, LocalDate.of(2000, 1, 1))
            .toFormatter()
            .withChronology(chrono);
    int expected = date.get(YEAR_OF_ERA);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    int actual = parsed.get(YEAR_OF_ERA);
    assertEquals(actual, expected,
            String.format("Wrong date parsed, chrono: %s, input: %s",
            chrono, input));

}
 
Example #5
Source File: TestReducedParser.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYear(ChronoLocalDate date) {
    Chronology chrono = date.getChronology();
    DateTimeFormatter df
            = new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
            .toFormatter()
            .withChronology(chrono);
    int expected = date.get(YEAR);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    int actual = parsed.get(YEAR);
    assertEquals(actual, expected,
            String.format("Wrong date parsed, chrono: %s, input: %s",
            chrono, input));

}
 
Example #6
Source File: DateTimeFormatterBuilder.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param field  the field to format, validated not null
 * @param minWidth  the minimum field width, from 1 to 10
 * @param maxWidth  the maximum field width, from 1 to 10
 * @param baseValue  the base value
 * @param baseDate  the base date
 */
ReducedPrinterParser(TemporalField field, int minWidth, int maxWidth,
        int baseValue, ChronoLocalDate baseDate) {
    this(field, minWidth, maxWidth, baseValue, baseDate, 0);
    if (minWidth < 1 || minWidth > 10) {
        throw new IllegalArgumentException("The minWidth must be from 1 to 10 inclusive but was " + minWidth);
    }
    if (maxWidth < 1 || maxWidth > 10) {
        throw new IllegalArgumentException("The maxWidth must be from 1 to 10 inclusive but was " + minWidth);
    }
    if (maxWidth < minWidth) {
        throw new IllegalArgumentException("Maximum width must exceed or equal the minimum width but " +
                maxWidth + " < " + minWidth);
    }
    if (baseDate == null) {
        if (field.range().isValidValue(baseValue) == false) {
            throw new IllegalArgumentException("The base value must be within the range of the field");
        }
        if ((((long) baseValue) + EXCEED_POINTS[maxWidth]) > Integer.MAX_VALUE) {
            throw new DateTimeException("Unable to add printer-parser as the range exceeds the capacity of an int");
        }
    }
}
 
Example #7
Source File: TestDateTimeFormatterBuilderWithLocale.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="mapTextLookup")
public void test_appendText_mapTextLookup(ChronoLocalDate date, Locale locale) {
    final String firstYear = "firstYear";
    final String firstMonth = "firstMonth";
    final String firstYearMonth = firstYear + firstMonth;
    final long first = 1L;

    Map<Long, String> yearMap = new HashMap<Long, String>();
    yearMap.put(first, firstYear);

    Map<Long, String> monthMap = new HashMap<Long, String>();
    monthMap.put(first, firstMonth);

    DateTimeFormatter formatter = builder
        .appendText(ChronoField.YEAR_OF_ERA, yearMap)
        .appendText(ChronoField.MONTH_OF_YEAR, monthMap)
        .toFormatter(locale)
        .withResolverStyle(ResolverStyle.STRICT);

    assertEquals(date.format(formatter), firstYearMonth);

    TemporalAccessor ta = formatter.parse(firstYearMonth);
    assertEquals(ta.getLong(ChronoField.YEAR_OF_ERA), first);
    assertEquals(ta.getLong(ChronoField.MONTH_OF_YEAR), first);
}
 
Example #8
Source File: TCKChronoLocalDate.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badMinusAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDate date = chrono.date(refDate);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDate date2 = chrono2.date(refDate);
        TemporalAmount adjuster = new FixedAdjuster(date2);
        if (chrono != chrono2) {
            try {
                date.minus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException");
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDate result = date.minus(adjuster);
            assertEquals(result, date2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example #9
Source File: TestReducedParser.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYearOfEra(ChronoLocalDate date) {
    Chronology chrono = date.getChronology();
    DateTimeFormatter df
            = new DateTimeFormatterBuilder().appendValueReduced(YEAR_OF_ERA, 2, 2, LocalDate.of(2000, 1, 1))
            .toFormatter()
            .withChronology(chrono);
    int expected = date.get(YEAR_OF_ERA);
    String input = df.format(date);

    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(input, pos);
    int actual = parsed.get(YEAR_OF_ERA);
    assertEquals(actual, expected,
            String.format("Wrong date parsed, chrono: %s, input: %s",
            chrono, input));

}
 
Example #10
Source File: IsoFields.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
@Override
public ChronoLocalDate resolve(
        Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
    Long yearLong = fieldValues.get(YEAR);
    Long qoyLong = fieldValues.get(QUARTER_OF_YEAR);
    if (yearLong == null || qoyLong == null) {
        return null;
    }
    int y = YEAR.checkValidIntValue(yearLong);  // always validate
    long doq = fieldValues.get(DAY_OF_QUARTER);
    ensureIso(partialTemporal);
    LocalDate date;
    if (resolverStyle == ResolverStyle.LENIENT) {
        date = LocalDate.of(y, 1, 1).plusMonths(Math.multiplyExact(Math.subtractExact(qoyLong, 1), 3));
        doq = Math.subtractExact(doq, 1);
    } else {
        int qoy = QUARTER_OF_YEAR.range().checkValidIntValue(qoyLong, QUARTER_OF_YEAR);  // validated
        date = LocalDate.of(y, ((qoy - 1) * 3) + 1, 1);
        if (doq < 1 || doq > 90) {
            if (resolverStyle == ResolverStyle.STRICT) {
                rangeRefinedBy(date).checkValidValue(doq, this);  // only allow exact range
            } else {  // SMART
                range().checkValidValue(doq, this);  // allow 1-92 rolling into next quarter
            }
        }
        doq--;
    }
    fieldValues.remove(this);
    fieldValues.remove(YEAR);
    fieldValues.remove(QUARTER_OF_YEAR);
    return date.plusDays(doq);
}
 
Example #11
Source File: TestExampleCode.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_chronoPackageExample() {
    // Print the Thai Buddhist date
    ChronoLocalDate now1 = Chronology.of("ThaiBuddhist").dateNow();
    int day = now1.get(ChronoField.DAY_OF_MONTH);
    int dow = now1.get(ChronoField.DAY_OF_WEEK);
    int month = now1.get(ChronoField.MONTH_OF_YEAR);
    int year = now1.get(ChronoField.YEAR);
    System.out.printf("  Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
            dow, day, month, year);

    // Enumerate the list of available calendars and print today for each
    Set<Chronology> chronos = Chronology.getAvailableChronologies();
    for (Chronology chrono : chronos) {
        ChronoLocalDate date = chrono.dateNow();
        System.out.printf("   %20s: %s%n", chrono.getId(), date.toString());
    }

    // Print today's date and the last day of the year for the Thai Buddhist Calendar.
    ChronoLocalDate first = now1
            .with(ChronoField.DAY_OF_MONTH, 1)
            .with(ChronoField.MONTH_OF_YEAR, 1);
    ChronoLocalDate last = first
            .plus(1, ChronoUnit.YEARS)
            .minus(1, ChronoUnit.DAYS);
    System.out.printf("  %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),
            first, last);
}
 
Example #12
Source File: TestUmmAlQuraChronology.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test (dataProvider="monthDays")
public void test_valueRange_monthDays(int year, int month, int maxlength) {
    ChronoLocalDate date = HijrahChronology.INSTANCE.date(year, month, 1);
    ValueRange range = null;
    for (int i=1; i<=12; i++) {
        range = date.range(ChronoField.DAY_OF_MONTH);
        date = date.plus(1, ChronoUnit.MONTHS);
        assertEquals(range.getMaximum(), month, maxlength);
    }
}
 
Example #13
Source File: TestNonIsoFormatter.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="format_data")
public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale,
                                     ChronoLocalDate date, String expected) {
    DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
        .withChronology(chrono).withLocale(formatLocale)
        .withDecimalStyle(DecimalStyle.of(numberingLocale));
    String text = dtf.format(date);
    assertEquals(text, expected);
}
 
Example #14
Source File: TestChronoLocalDate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public <D extends ChronoLocalDate> void test_date_checkGenerics_genericsMethod_withType() {
    Chronology chrono = ThaiBuddhistChronology.INSTANCE;
    @SuppressWarnings("unchecked")
    D date = (D) chrono.dateNow();
    date = processOK(date);
    // date = processClassOK(ThaiBuddhistDate.class);  // does not compile (correct)
    date = dateSupplier();

    // date = processWeird(date);  // does not compile (correct)
    // date = processClassWeird(ThaiBuddhistDate.class);  // does not compile (correct)
}
 
Example #15
Source File: TCKChronoPeriod.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="calendars")
public void test_addTo(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    ChronoLocalDate date = chrono.dateNow();
    Temporal result = period.addTo(date);
    assertEquals(result, date.plus(14, MONTHS).plus(3, DAYS));
}
 
Example #16
Source File: TCKTestServiceLoader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
 public void test_TestServiceLoader() {
    Chronology chrono = Chronology.of("Coptic");
    ChronoLocalDate copticDate = chrono.date(1729, 4, 27);
    LocalDate ld = LocalDate.from(copticDate);
    assertEquals(ld, LocalDate.of(2013, 1, 5), "CopticDate does not match LocalDate");
}
 
Example #17
Source File: TestChronoLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public <D extends ChronoLocalDate> void test_date_checkGenerics_genericsMethod_withType() {
    Chronology chrono = ThaiBuddhistChronology.INSTANCE;
    @SuppressWarnings("unchecked")
    D date = (D) chrono.dateNow();
    date = processOK(date);
    // date = processClassOK(ThaiBuddhistDate.class);  // does not compile (correct)
    date = dateSupplier();

    // date = processWeird(date);  // does not compile (correct)
    // date = processClassWeird(ThaiBuddhistDate.class);  // does not compile (correct)
}
 
Example #18
Source File: JulianFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ChronoLocalDate resolve(
        Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
    long value = fieldValues.remove(this);
    Chronology chrono = Chronology.from(partialTemporal);
    if (resolverStyle == ResolverStyle.LENIENT) {
        return chrono.dateEpochDay(Math.subtractExact(value, offset));
    }
    range().checkValidValue(value, this);
    return chrono.dateEpochDay(value - offset);
}
 
Example #19
Source File: TCKChronology.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test_IsoChronology_dateYearDay() {
    Chronology chrono = Chronology.of("ISO");
    ChronoLocalDate date1 = chrono.dateYearDay(IsoEra.CE, 5, 60);
    ChronoLocalDate date2 = chrono.date(IsoEra.CE, 5, 3, 1);
    assertEquals(date1, IsoChronology.INSTANCE.dateYearDay(IsoEra.CE, 5, 60));
    assertEquals(date2, IsoChronology.INSTANCE.dateYearDay(IsoEra.CE, 5, 60));
}
 
Example #20
Source File: TCKChronology.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_ThaiBuddhistChronology_dateYearDay() {
    Chronology chrono = Chronology.of("ThaiBuddhist");
    ChronoLocalDate date1 = chrono.dateYearDay(ThaiBuddhistEra.BE, 2459, 60);
    ChronoLocalDate date2 = chrono.date(ThaiBuddhistEra.BE, 2459, 2, 29);
    assertEquals(date1, ThaiBuddhistChronology.INSTANCE.dateYearDay(ThaiBuddhistEra.BE, 2459, 60));
    assertEquals(date2, ThaiBuddhistChronology.INSTANCE.dateYearDay(ThaiBuddhistEra.BE, 2459, 60));
}
 
Example #21
Source File: JulianFields.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ChronoLocalDate resolve(
        Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
    long value = fieldValues.remove(this);
    Chronology chrono = Chronology.from(partialTemporal);
    if (resolverStyle == ResolverStyle.LENIENT) {
        return chrono.dateEpochDay(Math.subtractExact(value, offset));
    }
    range().checkValidValue(value, this);
    return chrono.dateEpochDay(value - offset);
}
 
Example #22
Source File: TestExampleCode.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_calendarPackageExample() {

    // Enumerate the list of available calendars and print today for each
    Set<Chronology> chronos = Chronology.getAvailableChronologies();
    for (Chronology chrono : chronos) {
        ChronoLocalDate date = chrono.dateNow();
        System.out.printf("   %20s: %s%n", chrono.getId(), date.toString());
    }

    // Print the Thai Buddhist date
    ThaiBuddhistDate now1 = ThaiBuddhistDate.now();
    int day = now1.get(ChronoField.DAY_OF_MONTH);
    int dow = now1.get(ChronoField.DAY_OF_WEEK);
    int month = now1.get(ChronoField.MONTH_OF_YEAR);
    int year = now1.get(ChronoField.YEAR);
    System.out.printf("  Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
            dow, day, month, year);

    // Print today's date and the last day of the year for the Thai Buddhist Calendar.
    ThaiBuddhistDate first = now1
            .with(ChronoField.DAY_OF_MONTH, 1)
            .with(ChronoField.MONTH_OF_YEAR, 1);
    ThaiBuddhistDate last = first
            .plus(1, ChronoUnit.YEARS)
            .minus(1, ChronoUnit.DAYS);
    System.out.printf("  %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),
            first, last);
}
 
Example #23
Source File: IsoFields.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ChronoLocalDate resolve(
        Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
    Long yearLong = fieldValues.get(YEAR);
    Long qoyLong = fieldValues.get(QUARTER_OF_YEAR);
    if (yearLong == null || qoyLong == null) {
        return null;
    }
    int y = YEAR.checkValidIntValue(yearLong);  // always validate
    long doq = fieldValues.get(DAY_OF_QUARTER);
    ensureIso(partialTemporal);
    LocalDate date;
    if (resolverStyle == ResolverStyle.LENIENT) {
        date = LocalDate.of(y, 1, 1).plusMonths(Math.multiplyExact(Math.subtractExact(qoyLong, 1), 3));
        doq = Math.subtractExact(doq, 1);
    } else {
        int qoy = QUARTER_OF_YEAR.range().checkValidIntValue(qoyLong, QUARTER_OF_YEAR);  // validated
        date = LocalDate.of(y, ((qoy - 1) * 3) + 1, 1);
        if (doq < 1 || doq > 90) {
            if (resolverStyle == ResolverStyle.STRICT) {
                rangeRefinedBy(date).checkValidValue(doq, this);  // only allow exact range
            } else {  // SMART
                range().checkValidValue(doq, this);  // allow 1-92 rolling into next quarter
            }
        }
        doq--;
    }
    fieldValues.remove(this);
    fieldValues.remove(YEAR);
    fieldValues.remove(QUARTER_OF_YEAR);
    return date.plusDays(doq);
}
 
Example #24
Source File: Parsed.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void updateCheckConflict(ChronoLocalDate cld) {
    if (date != null) {
        if (cld != null && date.equals(cld) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to two different dates: " + date + " " + cld);
        }
    } else if (cld != null) {
        if (chrono.equals(cld.getChronology()) == false) {
            throw new DateTimeException("ChronoLocalDate must use the effective parsed chronology: " + chrono);
        }
        date = cld;
    }
}
 
Example #25
Source File: WeekFields.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a new week-based-year date of the Chronology, year, week-of-year,
 * and dow of week.
 * @param chrono The chronology of the new date
 * @param yowby the year of the week-based-year
 * @param wowby the week of the week-based-year
 * @param dow the day of the week
 * @return a ChronoLocalDate for the requested year, week of year, and day of week
 */
private ChronoLocalDate ofWeekBasedYear(Chronology chrono,
        int yowby, int wowby, int dow) {
    ChronoLocalDate date = chrono.date(yowby, 1, 1);
    int ldow = localizedDayOfWeek(date);
    int offset = startOfWeekOffset(1, ldow);

    // Clamp the week of year to keep it in the same year
    int yearLen = date.lengthOfYear();
    int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
    wowby = Math.min(wowby, newYearWeek - 1);

    int days = -offset + (dow - 1) + (wowby - 1) * 7;
    return date.plus(days, DAYS);
}
 
Example #26
Source File: TestExampleCode.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_chronoPackageExample() {
    // Print the Thai Buddhist date
    ChronoLocalDate now1 = Chronology.of("ThaiBuddhist").dateNow();
    int day = now1.get(ChronoField.DAY_OF_MONTH);
    int dow = now1.get(ChronoField.DAY_OF_WEEK);
    int month = now1.get(ChronoField.MONTH_OF_YEAR);
    int year = now1.get(ChronoField.YEAR);
    System.out.printf("  Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
            dow, day, month, year);

    // Enumerate the list of available calendars and print today for each
    Set<Chronology> chronos = Chronology.getAvailableChronologies();
    for (Chronology chrono : chronos) {
        ChronoLocalDate date = chrono.dateNow();
        System.out.printf("   %20s: %s%n", chrono.getId(), date.toString());
    }

    // Print today's date and the last day of the year for the Thai Buddhist Calendar.
    ChronoLocalDate first = now1
            .with(ChronoField.DAY_OF_MONTH, 1)
            .with(ChronoField.MONTH_OF_YEAR, 1);
    ChronoLocalDate last = first
            .plus(1, ChronoUnit.YEARS)
            .minus(1, ChronoUnit.DAYS);
    System.out.printf("  %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),
            first, last);
}
 
Example #27
Source File: TCKChronology.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_HijrahChronology_dateYearDay() {
    Chronology chrono = Chronology.of("Hijrah");
    ChronoLocalDate date1 = chrono.dateYearDay(HijrahEra.AH, 1434, 178);
    ChronoLocalDate date2 = chrono.date(HijrahEra.AH, 1434, 7, 1);
    assertEquals(date1, HijrahChronology.INSTANCE.dateYearDay(HijrahEra.AH, 1434, 178));
    assertEquals(date2, HijrahChronology.INSTANCE.dateYearDay(HijrahEra.AH, 1434, 178));
}
 
Example #28
Source File: TestUmmAlQuraChronology.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_chronoFields() {
    ChronoLocalDate hdate = HijrahChronology.INSTANCE.date(1434, 6, 28);
    assertEquals(hdate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), 3);
    assertEquals(hdate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), 7);
    assertEquals(hdate.get(ChronoField.ALIGNED_WEEK_OF_MONTH), 4);
    assertEquals(hdate.get(ChronoField.ALIGNED_WEEK_OF_YEAR), 25);
    assertEquals(hdate.get(ChronoField.ERA), 1);
    assertEquals(hdate.get(ChronoField.YEAR_OF_ERA), 1434);
    assertEquals(hdate.get(ChronoField.MONTH_OF_YEAR), 6);
    assertEquals(hdate.get(ChronoField.DAY_OF_MONTH), 28);
    assertEquals(hdate.get(ChronoField.DAY_OF_WEEK), 3);
    assertEquals(hdate.get(ChronoField.DAY_OF_YEAR), 175);
}
 
Example #29
Source File: TestUmmAlQuraChronology.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_chronoFields() {
    ChronoLocalDate hdate = HijrahChronology.INSTANCE.date(1434, 6, 28);
    assertEquals(hdate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), 3);
    assertEquals(hdate.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), 7);
    assertEquals(hdate.get(ChronoField.ALIGNED_WEEK_OF_MONTH), 4);
    assertEquals(hdate.get(ChronoField.ALIGNED_WEEK_OF_YEAR), 25);
    assertEquals(hdate.get(ChronoField.ERA), 1);
    assertEquals(hdate.get(ChronoField.YEAR_OF_ERA), 1434);
    assertEquals(hdate.get(ChronoField.MONTH_OF_YEAR), 6);
    assertEquals(hdate.get(ChronoField.DAY_OF_MONTH), 28);
    assertEquals(hdate.get(ChronoField.DAY_OF_WEEK), 3);
    assertEquals(hdate.get(ChronoField.DAY_OF_YEAR), 175);
}
 
Example #30
Source File: TCKChronology.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute the number of days from the Epoch and compute the date from the number of days.
 */
@Test(dataProvider = "calendarNameAndType")
public void test_epoch(String name, String alias) {
    Chronology chrono = Chronology.of(name); // a chronology. In practice this is rarely hardcoded
    ChronoLocalDate date1 = chrono.dateNow();
    long epoch1 = date1.getLong(ChronoField.EPOCH_DAY);
    ChronoLocalDate date2 = date1.with(ChronoField.EPOCH_DAY, epoch1);
    assertEquals(date1, date2, "Date from epoch day is not same date: " + date1 + " != " + date2);
    long epoch2 = date1.getLong(ChronoField.EPOCH_DAY);
    assertEquals(epoch1, epoch2, "Epoch day not the same: " + epoch1 + " != " + epoch2);
}