Search in sources :

Example 6 with DateFormat

use of android.icu.text.DateFormat in project j2objc by google.

the class CalendarRegressionTest method Test4125881.

/**
 * Check isLeapYear for BC years.
 */
@Test
public void Test4125881() {
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
    cal.clear();
    for (int y = -20; y <= 10; ++y) {
        cal.set(Calendar.ERA, y < 1 ? GregorianCalendar.BC : GregorianCalendar.AD);
        cal.set(Calendar.YEAR, y < 1 ? 1 - y : y);
        logln(y + " = " + fmt.format(cal.getTime()) + " " + cal.isLeapYear(y));
        if (cal.isLeapYear(y) != ((y + 40) % 4 == 0))
            errln("Leap years broken");
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 7 with DateFormat

use of android.icu.text.DateFormat in project j2objc by google.

the class CalendarRegressionTest method TestYearJump3279.

@Test
public void TestYearJump3279() {
    final long time = 1041148800000L;
    Calendar c = new GregorianCalendar();
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
    c.setTimeInMillis(time);
    int year1 = c.get(Calendar.YEAR);
    logln("time: " + fmt.format(new Date(c.getTimeInMillis())));
    logln("setting DOW to " + c.getFirstDayOfWeek());
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    logln("week: " + c.getTime());
    logln("week adjust: " + fmt.format(new Date(c.getTimeInMillis())));
    int year2 = c.get(Calendar.YEAR);
    if (year1 != year2) {
        errln("Error: adjusted day of week, and year jumped from " + year1 + " to " + year2);
    } else {
        logln("Year remained " + year2 + " - PASS.");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 8 with DateFormat

use of android.icu.text.DateFormat in project j2objc by google.

the class CalendarRegressionTest method Test4167060.

/**
 * Calendar.getActualMaximum(YEAR) works wrong.
 */
@Test
public void Test4167060() {
    int field = Calendar.YEAR;
    DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G", Locale.US);
    GregorianCalendar[] calendars = { new GregorianCalendar(100, Calendar.NOVEMBER, 1), new GregorianCalendar(-99, /* 100BC */
    Calendar.JANUARY, 1), new GregorianCalendar(1996, Calendar.FEBRUARY, 29) };
    String[] id = { "Hybrid", "Gregorian", "Julian" };
    for (int k = 0; k < 3; ++k) {
        logln("--- " + id[k] + " ---");
        for (int j = 0; j < calendars.length; ++j) {
            GregorianCalendar calendar = calendars[j];
            if (k == 1) {
                calendar.setGregorianChange(new Date(Long.MIN_VALUE));
            } else if (k == 2) {
                calendar.setGregorianChange(new Date(Long.MAX_VALUE));
            }
            format.setCalendar((Calendar) calendar.clone());
            Date dateBefore = calendar.getTime();
            int maxYear = calendar.getActualMaximum(field);
            logln("maxYear: " + maxYear + " for " + format.format(calendar.getTime()));
            logln("date before: " + format.format(dateBefore));
            int[] years = { 2000, maxYear - 1, maxYear, maxYear + 1 };
            for (int i = 0; i < years.length; i++) {
                boolean valid = years[i] <= maxYear;
                calendar.set(field, years[i]);
                Date dateAfter = calendar.getTime();
                int newYear = calendar.get(field);
                // restore calendar for next
                calendar.setTime(dateBefore);
                // use
                logln(" Year " + years[i] + (valid ? " ok " : " bad") + " => " + format.format(dateAfter));
                if (valid && newYear != years[i]) {
                    errln("  FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
                } else if (!valid && newYear == years[i]) {
                    // We no longer require strict year maxima. That is, the
                    // calendar
                    // algorithm may work for values > the stated maximum.
                    // errln(" FAIL: " + newYear + " should be invalid");
                    logln("  Note: " + newYear + " > maximum, but still valid");
                }
            }
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 9 with DateFormat

use of android.icu.text.DateFormat in project j2objc by google.

the class CopticTest method TestYear.

@Test
public void TestYear() {
    // Gregorian Calendar
    Calendar gCal = new GregorianCalendar();
    Date gToday = gCal.getTime();
    gCal.add(GregorianCalendar.MONTH, 2);
    Date gFuture = gCal.getTime();
    DateFormat gDF = DateFormat.getDateInstance(gCal, DateFormat.FULL);
    logln("gregorian calendar: " + gDF.format(gToday) + " + 2 months = " + gDF.format(gFuture));
    // Coptic Calendar
    CopticCalendar cCal = new CopticCalendar();
    Date cToday = cCal.getTime();
    cCal.add(CopticCalendar.MONTH, 2);
    Date cFuture = cCal.getTime();
    DateFormat cDF = DateFormat.getDateInstance(cCal, DateFormat.FULL);
    logln("coptic calendar: " + cDF.format(cToday) + " + 2 months = " + cDF.format(cFuture));
    // EthiopicCalendar
    EthiopicCalendar eCal = new EthiopicCalendar();
    Date eToday = eCal.getTime();
    // add 2 months
    eCal.add(EthiopicCalendar.MONTH, 2);
    eCal.setAmeteAlemEra(false);
    Date eFuture = eCal.getTime();
    DateFormat eDF = DateFormat.getDateInstance(eCal, DateFormat.FULL);
    logln("ethiopic calendar: " + eDF.format(eToday) + " + 2 months = " + eDF.format(eFuture));
}
Also used : EthiopicCalendar(android.icu.util.EthiopicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) CopticCalendar(android.icu.util.CopticCalendar) EthiopicCalendar(android.icu.util.EthiopicCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) CopticCalendar(android.icu.util.CopticCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 10 with DateFormat

use of android.icu.text.DateFormat in project j2objc by google.

the class DataDrivenCalendarTest method testConvert.

void testConvert(String caseString, CalendarFieldsSet fromSet, Calendar fromCalendar, CalendarFieldsSet toSet, Calendar toCalendar, boolean forward) {
    String thisString = caseString + (forward ? "forward" : "reverse") + " " + fromCalendar.getType() + "->" + toCalendar.getType() + " ";
    fromCalendar.clear();
    fromSet.setOnCalendar(fromCalendar);
    CalendarFieldsSet diffSet = new CalendarFieldsSet();
    diffSet.clear();
    // Is the calendar sane at the first?
    if (!fromSet.matches(fromCalendar, diffSet)) {
        String diffs = diffSet.diffFrom(fromSet);
        errln((String) "FAIL: " + thisString + ", SOURCE calendar was not set: Differences: " + diffs);
    } else {
        logln("PASS: " + thisString + " SOURCE calendar match.");
    }
    // logln("Set Source calendar: " + from);
    Date fromTime = fromCalendar.getTime();
    diffSet.clear();
    // Is the calendar sane after being set?
    if (!fromSet.matches(fromCalendar, diffSet)) {
        String diffs = diffSet.diffFrom(fromSet);
        errln((String) "FAIL: " + thisString + ", SET SOURCE calendar was not set: Differences: " + diffs);
    } else {
        logln("PASS: " + thisString + " SET SOURCE calendar match.");
    }
    toCalendar.clear();
    toCalendar.setTime(fromTime);
    diffSet.clear();
    if (!toSet.matches(toCalendar, diffSet)) {
        String diffs = diffSet.diffFrom(toSet);
        errln((String) "FAIL: " + thisString + ", Differences: " + diffs);
        DateFormat fmt = new SimpleDateFormat(new String("EEE MMM dd yyyy G"));
        String fromString = fmt.format(fromTime);
        logln("Source Time: " + fromString + ", Source Calendar: " + fromCalendar.getType());
    } else {
        logln("PASS: " + thisString + " match.");
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

DateFormat (android.icu.text.DateFormat)97 SimpleDateFormat (android.icu.text.SimpleDateFormat)80 Test (org.junit.Test)78 Date (java.util.Date)67 GregorianCalendar (android.icu.util.GregorianCalendar)44 ChineseDateFormat (android.icu.text.ChineseDateFormat)37 Calendar (android.icu.util.Calendar)37 ULocale (android.icu.util.ULocale)33 JapaneseCalendar (android.icu.util.JapaneseCalendar)27 IslamicCalendar (android.icu.util.IslamicCalendar)23 ChineseCalendar (android.icu.util.ChineseCalendar)22 ParseException (java.text.ParseException)21 BuddhistCalendar (android.icu.util.BuddhistCalendar)19 TimeZone (android.icu.util.TimeZone)19 HebrewCalendar (android.icu.util.HebrewCalendar)17 Locale (java.util.Locale)17 ParsePosition (java.text.ParsePosition)15 FieldPosition (java.text.FieldPosition)13 IOException (java.io.IOException)10 SimpleTimeZone (android.icu.util.SimpleTimeZone)7