Search in sources :

Example 91 with DateFormat

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

the class IntlTestDateFormatAPI method TestEquals.

// Test that the equals method works correctly.
@Test
public void TestEquals() {
    // Create two objects at different system times
    DateFormat a = DateFormat.getInstance();
    Date start = Calendar.getInstance().getTime();
    while (true) {
        // changed to remove compiler warnings.
        if (!start.equals(Calendar.getInstance().getTime())) {
            // Wait for time to change
            break;
        }
    }
    DateFormat b = DateFormat.getInstance();
    if (!(a.equals(b)))
        errln("FAIL: DateFormat objects created at different times are unequal.");
// Why has this test been disabled??? - aliu
// if (b instanceof SimpleDateFormat)
// {
// //double ONE_YEAR = 365*24*60*60*1000.0; //The variable is never used
// try {
// ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
// if (a.equals(b))
// errln("FAIL: DateFormat objects with different two digit start dates are equal.");
// }
// catch (Exception e) {
// errln("FAIL: setTwoDigitStartDate failed.");
// }
// }
}
Also used : DateFormat(android.icu.text.DateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 92 with DateFormat

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

the class IntlTestDateFormatAPIC method TestNameHiding.

/**
 * Test hiding of parse() and format() APIs in the Format hierarchy.
 * We test the entire hierarchy, even though this test is located in
 * the DateFormat API test.
 */
@Test
public void TestNameHiding() {
    // N.B.: This test passes if it COMPILES, since it's a test of
    // compile-time name hiding.
    Date dateObj = new Date(0);
    Number numObj = new Double(3.1415926535897932384626433832795);
    StringBuffer strBuffer = new StringBuffer("");
    String str;
    FieldPosition fpos = new FieldPosition(0);
    ParsePosition ppos = new ParsePosition(0);
    // DateFormat calling Format API
    {
        logln("DateFormat");
        DateFormat dateFmt = DateFormat.getInstance();
        if (dateFmt != null) {
            str = dateFmt.format(dateObj);
            strBuffer = dateFmt.format(dateObj, strBuffer, fpos);
        } else {
            errln("FAIL: Can't create DateFormat");
        }
    }
    // SimpleDateFormat calling Format & DateFormat API
    {
        logln("SimpleDateFormat");
        SimpleDateFormat sdf = new SimpleDateFormat();
        // Format API
        str = sdf.format(dateObj);
        strBuffer = sdf.format(dateObj, strBuffer, fpos);
        // DateFormat API
        strBuffer = sdf.format(new Date(0), strBuffer, fpos);
        str = sdf.format(new Date(0));
        try {
            sdf.parse(str);
            sdf.parse(str, ppos);
        } catch (java.text.ParseException pe) {
            System.out.println(pe);
        }
    }
    // NumberFormat calling Format API
    {
        logln("NumberFormat");
        NumberFormat fmt = NumberFormat.getInstance();
        if (fmt != null) {
            str = fmt.format(numObj);
            strBuffer = fmt.format(numObj, strBuffer, fpos);
        } else {
            errln("FAIL: Can't create NumberFormat");
        }
    }
    // DecimalFormat calling Format & NumberFormat API
    {
        logln("DecimalFormat");
        DecimalFormat fmt = new DecimalFormat();
        // Format API
        str = fmt.format(numObj);
        strBuffer = fmt.format(numObj, strBuffer, fpos);
        // NumberFormat API
        str = fmt.format(2.71828);
        str = fmt.format(1234567);
        strBuffer = fmt.format(1.41421, strBuffer, fpos);
        strBuffer = fmt.format(9876543, strBuffer, fpos);
        Number obj = fmt.parse(str, ppos);
        try {
            obj = fmt.parse(str);
            if (obj == null) {
                errln("FAIL: The format object could not parse the string : " + str);
            }
        } catch (java.text.ParseException pe) {
            System.out.println(pe);
        }
    }
// ICU4J have not the classes ChoiceFormat and MessageFormat
/*
        // ChoiceFormat calling Format & NumberFormat API
        {
            logln("ChoiceFormat");
            ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
            // Format API
            str = fmt.format(numObj);
            strBuffer = fmt.format(numObj, strBuffer, fpos);
            // NumberFormat API
            str = fmt.format(2.71828);
            str = fmt.format(1234567);
            strBuffer = fmt.format(1.41421, strBuffer, fpos);
            strBuffer = fmt.format(9876543, strBuffer, fpos);
            Number obj = fmt.parse(str, ppos);
            try {
                obj = fmt.parse(str);
            } catch (java.text.ParseException pe) {
                System.out.println(pe);
            }
        }
    
        
        // MessageFormat calling Format API
        {
            logln("MessageFormat");
            MessageFormat fmt = new MessageFormat("");
            // Format API
            // We use dateObj, which MessageFormat should reject.
            // We're testing name hiding, not the format method.
            try {
                str = fmt.format(dateObj);
            } catch (Exception e) {
                //e.printStackTrace();
            }
            try {
                strBuffer = fmt.format(dateObj, strBuffer, fpos);
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
        */
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Date(java.util.Date) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 93 with DateFormat

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

the class CompatibilityTest method TestComputeJulianDay4406.

@Test
public void TestComputeJulianDay4406() {
    // jb4406 is probably not a bug, this is to document the behavior
    GregorianCalendar cal = new GregorianCalendar();
    final int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
    logln("julian day value jumps at changeover");
    for (int day = 12; day < 18; ++day) {
        cal.set(1582, 9, day);
        logln("[" + day + "] " + (cal.getTimeInMillis() / MILLIS_IN_DAY));
    }
    logln("\njulian days not accurate before 1 March 0004");
    for (int day = 1; day < 3; ++day) {
        cal.set(1, 0, day);
        logln("[" + day + "] " + (cal.getTimeInMillis() / MILLIS_IN_DAY));
    }
    DateFormat fmt = cal.getDateTimeFormat(DateFormat.LONG, 0, Locale.getDefault());
    logln("\nswitchover in 1582");
    cal.set(1582, 9, 4);
    logln(fmt.format(cal));
    cal.add(Calendar.DATE, 1);
    logln(fmt.format(cal));
    cal.set(Calendar.JULIAN_DAY, 1721426);
    logln(fmt.format(cal));
    logln("\nlate switchover - proleptic Julian");
    cal.set(1582, 9, 4);
    cal.setGregorianChange(new Date(Long.MAX_VALUE));
    logln(fmt.format(cal));
    cal.add(Calendar.DATE, 1);
    logln(fmt.format(cal));
    cal.set(Calendar.JULIAN_DAY, 1721426);
    logln(fmt.format(cal));
    logln("\nearly switchover - proleptic Gregorian");
    cal.set(1582, 9, 4);
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    logln(fmt.format(cal));
    cal.add(Calendar.DATE, 1);
    logln(fmt.format(cal));
    cal.set(Calendar.JULIAN_DAY, 1721426);
    logln(fmt.format(cal));
}
Also used : DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 94 with DateFormat

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

the class CalendarTestFmwk method doTestCases.

/**
 * Iterates through a list of calendar <code>TestCase</code> objects and
 * makes sure that the time-to-fields and fields-to-time calculations work
 * correnctly for the values in each test case.
 */
protected void doTestCases(TestCase[] cases, Calendar cal) {
    cal.setTimeZone(UTC);
    // Get a format to use for printing dates in the calendar system we're testing
    DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.SHORT, -1, Locale.getDefault());
    final String pattern = (cal instanceof ChineseCalendar) ? "E MMl/dd/y G HH:mm:ss.S z" : "E, MM/dd/yyyy G HH:mm:ss.S z";
    ((SimpleDateFormat) format).applyPattern(pattern);
    // This format is used for printing Gregorian dates.
    DateFormat gregFormat = new SimpleDateFormat(pattern);
    gregFormat.setTimeZone(UTC);
    GregorianCalendar pureGreg = new GregorianCalendar(UTC);
    pureGreg.setGregorianChange(new Date(Long.MIN_VALUE));
    DateFormat pureGregFmt = new SimpleDateFormat("E M/d/yyyy G");
    pureGregFmt.setCalendar(pureGreg);
    // Now iterate through the test cases and see what happens
    for (int i = 0; i < cases.length; i++) {
        logln("\ntest case: " + i);
        TestCase test = cases[i];
        // 
        // First we want to make sure that the millis -> fields calculation works
        // test.applyTime will call setTime() on the calendar object, and
        // test.fieldsEqual will retrieve all of the field values and make sure
        // that they're the same as the ones in the testcase
        // 
        test.applyTime(cal);
        if (!test.fieldsEqual(cal, this)) {
            errln("Fail: (millis=>fields) " + gregFormat.format(test.getTime()) + " => " + format.format(cal.getTime()) + ", expected " + test);
        }
        // 
        // If that was OK, check the fields -> millis calculation
        // test.applyFields will set all of the calendar's fields to
        // match those in the test case.
        // 
        cal.clear();
        test.applyFields(cal);
        if (!test.equals(cal)) {
            errln("Fail: (fields=>millis) " + test + " => " + pureGregFmt.format(cal.getTime()) + ", expected " + pureGregFmt.format(test.getTime()));
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 95 with DateFormat

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

the class ChineseTest method TestCoverage.

// public void TestFindLeapMonths() {
// ChineseCalendar cal = new ChineseCalendar();
// cal.setTime(new Date(2000-1900, Calendar.JANUARY, 1));
// long end = new Date(2100-1900, Calendar.JANUARY, 1).getTime();
// ChineseDateFormat fmt = (ChineseDateFormat) DateFormat.getInstance(cal);
// fmt.applyPattern("u-MMl-dd, 'Year' y, 'Cycle' G");
// while (cal.getTimeInMillis() < end) {
// if (cal.get(ChineseCalendar.IS_LEAP_MONTH) != 0) {
// cal.set(Calendar.DAY_OF_MONTH, 1);
// logln(cal.getTime() + " = " + fmt.format(cal.getTime()));
// cal.set(Calendar.DAY_OF_MONTH, 29);
// }
// cal.add(Calendar.DAY_OF_YEAR, 25);
// }
// }
@Test
public void TestCoverage() {
    // Coverage for constructors
    {
        // new ChineseCalendar(Date)
        ChineseCalendar cal = new ChineseCalendar(new Date());
        if (cal == null) {
            errln("could not create ChineseCalendar with Date");
        }
    }
    {
        // new ChineseCalendar(int year, int month, int isLeapMonth, int date)
        ChineseCalendar cal = new ChineseCalendar(23, Calendar.JULY, 1, 2);
        if (cal == null) {
            errln("could not create ChineseCalendar with year,month,isLeapMonth,date");
        }
        // Make sure the given values are properly set
        if (cal.get(Calendar.YEAR) != 23 || cal.get(Calendar.MONTH) != Calendar.JULY || cal.get(Calendar.IS_LEAP_MONTH) != 1 || cal.get(Calendar.DATE) != 2 || cal.get(Calendar.MILLISECONDS_IN_DAY) != 0) {
            errln("ChineseCalendar was initialized incorrectly with year,month,isLeapMonth,date");
        }
    }
    {
        // new ChineseCalendar(int year, int month, int isLeapMonth, int date, int hour, int minute, int second)
        ChineseCalendar cal = new ChineseCalendar(23, Calendar.JULY, 1, 2, 12, 34, 56);
        if (cal == null) {
            errln("could not create ChineseCalendar with year,month,isLeapMonth,date,hour,minute,second");
        }
        // Make sure the given values are properly set
        if (cal.get(Calendar.YEAR) != 23 || cal.get(Calendar.MONTH) != Calendar.JULY || cal.get(Calendar.IS_LEAP_MONTH) != 1 || cal.get(Calendar.DATE) != 2 || cal.get(Calendar.HOUR_OF_DAY) != 12 || cal.get(Calendar.MINUTE) != 34 || cal.get(Calendar.SECOND) != 56 || cal.get(Calendar.MILLISECOND) != 0) {
            errln("ChineseCalendar was initialized incorrectly with year,month,isLeapMonth,date,hour,minute,second");
        }
    }
    {
        // new ChineseCalendar(Locale)
        ChineseCalendar cal = new ChineseCalendar(Locale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with Locale");
        }
    }
    {
        // new ChineseCalendar(ULocale)
        ChineseCalendar cal = new ChineseCalendar(ULocale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with ULocale");
        }
    }
    {
        // new ChineseCalendar(TimeZone)
        ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with TimeZone");
        }
    }
    {
        // new ChineseCalendar(TimeZone, Locale)
        ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault(), Locale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with TimeZone,Locale");
        }
    }
    {
        // new ChineseCalendar(TimeZone, ULocale)
        ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault(), ULocale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with TimeZone,ULocale");
        }
    }
    // Note: ICU 50 or later versions, DateFormat.getInstance(ChineseCalendar) no longer
    // returns an instance of ChineseDateFormat. Chinese calendar formatting support was
    // changed and integrated into SimpleDateFormat since ICU 49. Also, ChineseDateFormat
    // specific pattern letter "l" is no longer used by the new implementation.
    // ChineseCalendar cal = new ChineseCalendar();
    // DateFormat format = DateFormat.getInstance(cal);
    // if(!(format instanceof ChineseDateFormat)){
    // errln("DateFormat.getInstance("+cal+") did not return a ChineseDateFormat");
    // }
    // ChineseDateFormat fmt = (ChineseDateFormat)format;
    // fmt.applyPattern("llyyll");
    // Date time = getDate(2100, Calendar.JANUARY, 1);
    // String str = fmt.format(time);
    // try {
    // Date e = fmt.parse(str);
    // logln("chinese calendar time: " + time + " result: " + str + " --> " + e);
    // } catch (java.text.ParseException ex) {
    // logln(ex.getMessage()); // chinese calendar can't parse this, no error for now
    // }
    // new ChineseCalendar(TimeZone,ULocale)
    ChineseCalendar ccal2 = new ChineseCalendar(TimeZone.getDefault(), ULocale.CHINA);
    if (ccal2 == null) {
        errln("could not create ChineseCalendar with TimeZone ULocale");
    } else {
        DateFormat fmt2 = DateFormat.getDateInstance(ccal2, DateFormat.DEFAULT, ULocale.CHINA);
        Date time2 = getDate(2001, Calendar.MAY, 23);
        String str2 = fmt2.format(time2);
        logln("Chinese calendar time: " + time2 + " result: " + str2);
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) Date(java.util.Date) Test(org.junit.Test)

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