Search in sources :

Example 1 with JapaneseCalendar

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

the class JapaneseTest method TestJapaneseYear3282.

@Test
public void TestJapaneseYear3282() {
    Calendar c = Calendar.getInstance(ULocale.ENGLISH);
    c.set(2003, Calendar.SEPTEMBER, 25);
    JapaneseCalendar jcal = new JapaneseCalendar();
    // jcal.setTime(new Date(1187906308151L));  alternate value
    jcal.setTime(c.getTime());
    logln("Now is: " + jcal.getTime());
    c.setTime(jcal.getTime());
    int nowYear = c.get(Calendar.YEAR);
    logln("Now year: " + nowYear);
    SimpleDateFormat jdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(jcal, SimpleDateFormat.DEFAULT, Locale.getDefault());
    jdf.applyPattern("G yy/MM/dd");
    String text = jdf.format(jcal.getTime());
    logln("Now is: " + text + " (in Japan)");
    try {
        Date date = jdf.parse(text);
        logln("But is this not the date?: " + date);
        c.setTime(date);
        int thenYear = c.get(Calendar.YEAR);
        logln("Then year: " + thenYear);
        if (thenYear != nowYear) {
            errln("Nowyear " + nowYear + " is not thenyear " + thenYear);
        } else {
            logln("Nowyear " + nowYear + " == thenyear " + thenYear);
        }
    } catch (java.text.ParseException ex) {
        ex.printStackTrace();
    }
}
Also used : Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 2 with JapaneseCalendar

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

the class JapaneseTest method Test3860.

@Test
public void Test3860() {
    ULocale loc = new ULocale("ja_JP@calendar=japanese");
    Calendar cal = new JapaneseCalendar(loc);
    DateFormat enjformat = cal.getDateTimeFormat(0, 0, new ULocale("en_JP@calendar=japanese"));
    DateFormat format = cal.getDateTimeFormat(0, 0, loc);
    // Note: just 'y' doesn't work here.
    ((SimpleDateFormat) format).applyPattern("y.M.d");
    ParsePosition pos = new ParsePosition(0);
    // after the start of heisei accession.  Jan 1, 1H wouldn't work  because it is actually showa 64
    Date aDate = format.parse("1.1.9", pos);
    String inEn = enjformat.format(aDate);
    cal.clear();
    cal.setTime(aDate);
    int gotYear = cal.get(Calendar.YEAR);
    int gotEra = cal.get(Calendar.ERA);
    int expectYear = 1;
    int expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != expectYear) || (gotEra != expectEra)) {
        errln("Expected year " + expectYear + ", era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    } else {
        logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    }
    // Test parse with missing era (should default to current era, heisei)
    // Test parse with incomplete information
    logln("Testing parse w/ just year...");
    Calendar cal2 = new JapaneseCalendar(loc);
    SimpleDateFormat fmt = new SimpleDateFormat("y", loc);
    SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian"));
    cal2.clear();
    String samplestr = "1";
    logln("Test Year: " + samplestr);
    try {
        aDate = fmt.parse(samplestr);
    } catch (ParseException pe) {
        errln("Error parsing " + samplestr);
    }
    ParsePosition pp = new ParsePosition(0);
    fmt.parse(samplestr, cal2, pp);
    logln("cal2 after 1 parse:");
    String str = fmt2.format(aDate);
    logln("as Gregorian Calendar: " + str);
    cal2.setTime(aDate);
    gotYear = cal2.get(Calendar.YEAR);
    gotEra = cal2.get(Calendar.ERA);
    expectYear = 1;
    expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != 1) || (gotEra != expectEra)) {
        errln("parse " + samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear + " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str + ")");
    } else {
        logln(" year: " + gotYear + ", era: " + gotEra);
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 3 with JapaneseCalendar

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

the class JapaneseTest method TestLimits.

/**
 * Test limits of the Japanese calendar
 */
@Test
public void TestLimits() {
    Calendar cal = Calendar.getInstance();
    cal.set(1988, Calendar.DECEMBER, 1);
    JapaneseCalendar jcal = new JapaneseCalendar();
    doLimitsTest(jcal, null, cal.getTime());
    doTheoreticalLimitsTest(jcal, true);
}
Also used : Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Test(org.junit.Test)

Example 4 with JapaneseCalendar

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

the class JapaneseTest method Test5345parse.

@Test
public void Test5345parse() {
    // Test parse with incomplete information
    // DateFormat.LONG, Locale.US);
    DateFormat fmt2 = DateFormat.getDateInstance();
    JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US"));
    SimpleDateFormat fmt = (SimpleDateFormat) c.getDateTimeFormat(1, 1, new ULocale("en_US@calendar=japanese"));
    fmt.applyPattern("G y");
    logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
    // SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
    // 1868-09-08 00:00 Pacific Time (GMT-07:52:58)
    long aDateLong = -3197117222000L;
    if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
        // Java time zone implementation does not support LMTs
        // 1868-09-08 00:00 Pacific Time (GMT-08:00)
        aDateLong = -3197116800000L;
    }
    Date aDate = new Date(aDateLong);
    logln("aDate: " + aDate.toString() + ", from " + aDateLong);
    String str;
    str = fmt2.format(aDate);
    logln("Test Date: " + str);
    str = fmt.format(aDate);
    logln("as Japanese Calendar: " + str);
    String expected = "Meiji 1";
    if (!str.equals(expected)) {
        errln("FAIL: Expected " + expected + " but got " + str);
    }
    Date otherDate;
    try {
        otherDate = fmt.parse(expected);
        if (!otherDate.equals(aDate)) {
            String str3;
            // ParsePosition pp;
            Date dd = fmt.parse(expected);
            str3 = fmt.format(otherDate);
            long oLong = otherDate.getTime();
            long aLong = otherDate.getTime();
            errln("FAIL: Parse incorrect of " + expected + ":  wanted " + aDate + " (" + aLong + "), but got " + " " + otherDate + " (" + oLong + ") = " + str3 + " not " + dd.toString());
        } else {
            logln("Parsed OK: " + expected);
        }
    } catch (java.text.ParseException pe) {
        errln("FAIL: ParseException: " + pe.toString());
        pe.printStackTrace();
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 5 with JapaneseCalendar

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

the class JapaneseTest method Test5345calendar.

@Test
public void Test5345calendar() {
    logln("** testIncompleteCalendar()");
    // Test calendar with incomplete information
    JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault());
    logln("test clear");
    c.clear();
    // Showa 45 = Gregorian 1970
    int[] expected0 = { Calendar.ERA, 234, Calendar.YEAR, 45 };
    checkExpected(c, expected0);
    logln("test setting era");
    c.clear();
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
    int[] expectedA = { Calendar.ERA, JapaneseCalendar.MEIJI };
    checkExpected(c, expectedA);
    logln("test setting era and year and month and date");
    c.clear();
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
    c.set(Calendar.YEAR, 1);
    c.set(Calendar.MONTH, Calendar.JANUARY);
    c.set(Calendar.DATE, 1);
    int[] expectedC = { Calendar.ERA, JapaneseCalendar.MEIJI - 1 };
    checkExpected(c, expectedC);
    logln("test setting  year and month and date THEN era");
    c.clear();
    c.set(Calendar.YEAR, 1);
    c.set(Calendar.MONTH, Calendar.JANUARY);
    c.set(Calendar.DATE, 1);
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
    checkExpected(c, expectedC);
    logln("test setting era and year");
    c.clear();
    c.set(Calendar.YEAR, 1);
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
    int[] expectedB = { Calendar.ERA, JapaneseCalendar.MEIJI, Calendar.YEAR, 1 };
    checkExpected(c, expectedB);
}
Also used : JapaneseCalendar(android.icu.util.JapaneseCalendar) Test(org.junit.Test)

Aggregations

JapaneseCalendar (android.icu.util.JapaneseCalendar)10 Test (org.junit.Test)10 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 Calendar (android.icu.util.Calendar)6 ULocale (android.icu.util.ULocale)6 DateFormat (android.icu.text.DateFormat)5 GregorianCalendar (android.icu.util.GregorianCalendar)5 Date (java.util.Date)5 IslamicCalendar (android.icu.util.IslamicCalendar)4 BuddhistCalendar (android.icu.util.BuddhistCalendar)3 ParseException (java.text.ParseException)3 GlobalizationPreferences (android.icu.util.GlobalizationPreferences)2 TimeZone (android.icu.util.TimeZone)2 ParsePosition (java.text.ParsePosition)2 ChineseDateFormat (android.icu.text.ChineseDateFormat)1 ChineseCalendar (android.icu.util.ChineseCalendar)1 HebrewCalendar (android.icu.util.HebrewCalendar)1 FieldPosition (java.text.FieldPosition)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1