Search in sources :

Example 81 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class IBMCalendarTest method quasiGregorianTest.

/**
 * Run a test of a quasi-Gregorian calendar.  This is a calendar
 * that behaves like a Gregorian but has different year/era mappings.
 * The int[] data array should have the format:
 *
 * { era, year, gregorianYear, month, dayOfMonth, ... }
 */
void quasiGregorianTest(Calendar cal, int[] data) {
    // As of JDK 1.4.1_01, using the Sun JDK GregorianCalendar as
    // a reference throws us off by one hour.  This is most likely
    // due to the JDK 1.4 incorporation of historical time zones.
    // java.util.Calendar grego = java.util.Calendar.getInstance();
    Calendar grego = Calendar.getInstance();
    for (int i = 0; i < data.length; ) {
        int era = data[i++];
        int year = data[i++];
        int gregorianYear = data[i++];
        int month = data[i++];
        int dayOfMonth = data[i++];
        grego.clear();
        grego.set(gregorianYear, month, dayOfMonth);
        Date D = grego.getTime();
        cal.clear();
        cal.set(Calendar.ERA, era);
        cal.set(year, month, dayOfMonth);
        Date d = cal.getTime();
        if (d.equals(D)) {
            logln("OK: " + era + ":" + year + "/" + (month + 1) + "/" + dayOfMonth + " => " + d);
        } else {
            errln("Fail: " + era + ":" + year + "/" + (month + 1) + "/" + dayOfMonth + " => " + d + ", expected " + D);
        }
        cal.clear();
        cal.setTime(D);
        int e = cal.get(Calendar.ERA);
        int y = cal.get(Calendar.YEAR);
        if (y == year && e == era) {
            logln("OK: " + D + " => " + cal.get(Calendar.ERA) + ":" + cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE));
        } else {
            logln("Fail: " + D + " => " + cal.get(Calendar.ERA) + ":" + cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) + ", expected " + era + ":" + year + "/" + (month + 1) + "/" + dayOfMonth);
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) TaiwanCalendar(android.icu.util.TaiwanCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date)

Example 82 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class IBMCalendarTest method TestJapanese.

/**
 * Verify that JapaneseCalendar shifts years to Japanese Eras but otherwise
 * behaves like GregorianCalendar.
 */
@Test
public void TestJapanese() {
    // First make sure this test works for GregorianCalendar
    int[] control = { GregorianCalendar.AD, 1868, 1868, Calendar.SEPTEMBER, 8, GregorianCalendar.AD, 1868, 1868, Calendar.SEPTEMBER, 9, GregorianCalendar.AD, 1869, 1869, Calendar.JUNE, 4, GregorianCalendar.AD, 1912, 1912, Calendar.JULY, 29, GregorianCalendar.AD, 1912, 1912, Calendar.JULY, 30, GregorianCalendar.AD, 1912, 1912, Calendar.AUGUST, 1 };
    quasiGregorianTest(new GregorianCalendar(), control);
    int[] data = { JapaneseCalendar.MEIJI, 1, 1868, Calendar.SEPTEMBER, 8, JapaneseCalendar.MEIJI, 1, 1868, Calendar.SEPTEMBER, 9, JapaneseCalendar.MEIJI, 2, 1869, Calendar.JUNE, 4, JapaneseCalendar.MEIJI, 45, 1912, Calendar.JULY, 29, JapaneseCalendar.TAISHO, 1, 1912, Calendar.JULY, 30, JapaneseCalendar.TAISHO, 1, 1912, Calendar.AUGUST, 1 };
    quasiGregorianTest(new JapaneseCalendar(), data);
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Test(org.junit.Test)

Example 83 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class DateFormatRegressionTest method TestHindiArabicDigits.

@Test
public void TestHindiArabicDigits() {
    String s;
    char first;
    String what;
    {
        DateFormat df = DateFormat.getInstance(new GregorianCalendar(), new ULocale("hi_IN@numbers=deva"));
        what = "Gregorian Calendar, hindi";
        s = df.format(new Date(0));
        /* 31/12/1969 */
        logln(what + "=" + s);
        first = s.charAt(0);
        if (first < kHindiZero || first > (kHindiZero + 9)) {
            errln(what + "- wrong digit,  got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new IslamicCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 21/10/1989 */
        what = "Islamic Calendar, Arabic";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong digit, got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new GregorianCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "Gregorian,  ar_IQ, df.getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new GregorianCalendar(), new Locale("mt", "MT"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "Gregorian,  mt_MT, df.getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kLatinZero || first > (kLatinZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new IslamicCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "Islamic calendar, ar_IQ, df.getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "ar_IQ, getDateTimeInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new JapaneseCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "ar_IQ, Japanese Calendar, getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) JapaneseCalendar(android.icu.util.JapaneseCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) Test(org.junit.Test)

Example 84 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class DateFormatRegressionTest method Test_GEec.

@Test
public void Test_GEec() {
    class PatternAndResult {

        private String pattern;

        private String result;

        PatternAndResult(String pat, String res) {
            pattern = pat;
            result = res;
        }

        public String getPattern() {
            return pattern;
        }

        public String getResult() {
            return result;
        }
    }
    final PatternAndResult[] tests = { new PatternAndResult("dd MMM yyyy GGG", "02 Jul 2008 AD"), new PatternAndResult("dd MMM yyyy GGGGG", "02 Jul 2008 A"), new PatternAndResult("e dd MMM yyyy", "4 02 Jul 2008"), new PatternAndResult("ee dd MMM yyyy", "04 02 Jul 2008"), new PatternAndResult("c dd MMM yyyy", "4 02 Jul 2008"), new PatternAndResult("cc dd MMM yyyy", "4 02 Jul 2008"), new PatternAndResult("eee dd MMM yyyy", "Wed 02 Jul 2008"), new PatternAndResult("EEE dd MMM yyyy", "Wed 02 Jul 2008"), new PatternAndResult("EE dd MMM yyyy", "Wed 02 Jul 2008"), new PatternAndResult("eeee dd MMM yyyy", "Wednesday 02 Jul 2008"), new PatternAndResult("eeeee dd MMM yyyy", "W 02 Jul 2008"), new PatternAndResult("e ww YYYY", "4 27 2008"), new PatternAndResult("c ww YYYY", "4 27 2008") };
    ULocale loc = ULocale.ENGLISH;
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = new GregorianCalendar(tz, loc);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", loc);
    for (int i = 0; i < tests.length; i++) {
        PatternAndResult item = tests[i];
        dateFormat.applyPattern(item.getPattern());
        // 2008 July 02 5 AM PDT
        cal.set(2008, 6, 2, 5, 0);
        StringBuffer buf = new StringBuffer(32);
        FieldPosition fp = new FieldPosition(DateFormat.YEAR_FIELD);
        dateFormat.format(cal, buf, fp);
        if (buf.toString().compareTo(item.getResult()) != 0) {
            errln("for pattern " + item.getPattern() + ", expected " + item.getResult() + ", got " + buf);
        }
        ParsePosition pos = new ParsePosition(0);
        dateFormat.parse(item.getResult(), cal, pos);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DATE);
        if (year != 2008 || month != 6 || day != 2) {
            errln("use pattern " + item.getPattern() + " to parse " + item.getResult() + ", expected y2008 m6 d2, got " + year + " " + month + " " + day);
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 85 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class DateFormatRegressionTest method Test4210209.

/**
 * j32 {JDK Bug 4210209 4209272}
 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
 */
@Test
public void Test4210209() {
    String pattern = "MMM d, yyyy";
    DateFormat fmt = new SimpleDateFormat(pattern, Locale.US);
    DateFormat disp = new SimpleDateFormat("MMM dd yyyy GG", Locale.US);
    Calendar calx = fmt.getCalendar();
    calx.setLenient(false);
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2000, Calendar.FEBRUARY, 29);
    Date d = calendar.getTime();
    String s = fmt.format(d);
    logln(disp.format(d) + " f> " + pattern + " => \"" + s + "\"");
    ParsePosition pos = new ParsePosition(0);
    d = fmt.parse(s, pos);
    logln("\"" + s + "\" p> " + pattern + " => " + (d != null ? disp.format(d) : "null"));
    logln("Parse pos = " + pos.getIndex() + ", error pos = " + pos.getErrorIndex());
    if (pos.getErrorIndex() != -1) {
        errln("FAIL: Error index should be -1");
    }
    // The underlying bug is in GregorianCalendar.  If the following lines
    // succeed, the bug is fixed.  If the bug isn't fixed, they will throw
    // an exception.
    GregorianCalendar cal = new GregorianCalendar();
    cal.clear();
    cal.setLenient(false);
    // This should work!
    cal.set(2000, Calendar.FEBRUARY, 29);
    d = cal.getTime();
    logln("Attempt to set Calendar to Feb 29 2000: " + disp.format(d));
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

GregorianCalendar (android.icu.util.GregorianCalendar)114 Test (org.junit.Test)102 Date (java.util.Date)64 Calendar (android.icu.util.Calendar)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)34 TimeZone (android.icu.util.TimeZone)29 SimpleTimeZone (android.icu.util.SimpleTimeZone)28 DateFormat (android.icu.text.DateFormat)24 JapaneseCalendar (android.icu.util.JapaneseCalendar)17 ULocale (android.icu.util.ULocale)16 ChineseCalendar (android.icu.util.ChineseCalendar)15 IslamicCalendar (android.icu.util.IslamicCalendar)14 BuddhistCalendar (android.icu.util.BuddhistCalendar)12 ChineseDateFormat (android.icu.text.ChineseDateFormat)8 IOException (java.io.IOException)8 ParsePosition (java.text.ParsePosition)8 CalendarAstronomer (android.icu.impl.CalendarAstronomer)7 HebrewCalendar (android.icu.util.HebrewCalendar)7 FieldPosition (java.text.FieldPosition)7 ParseException (java.text.ParseException)6