Search in sources :

Example 51 with GregorianCalendar

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

the class DateFormatTest method TestTimeZoneDisplayName.

@Test
public void TestTimeZoneDisplayName() {
    Calendar cal = new GregorianCalendar();
    SimpleDateFormat testfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    testfmt.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
    for (int i = 0; i < fallbackTests.length; ++i) {
        String[] info = fallbackTests[i];
        logln(info[0] + ";" + info[1] + ";" + info[2] + ";" + info[3]);
        long time = 0;
        try {
            Date testd = testfmt.parse(info[2]);
            time = testd.getTime();
        } catch (ParseException pe) {
            errln("Failed to parse test date data");
            continue;
        }
        ULocale l = new ULocale(info[0]);
        TimeZone tz = TimeZone.getTimeZone(info[1]);
        SimpleDateFormat fmt = new SimpleDateFormat(info[3], l);
        cal.setTimeInMillis(time);
        cal.setTimeZone(tz);
        String result = fmt.format(cal);
        if (!result.equals(info[4])) {
            errln(info[0] + ";" + info[1] + ";" + info[2] + ";" + info[3] + " expected: '" + info[4] + "' but got: '" + result + "'");
        }
    }
}
Also used : TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) BuddhistCalendar(android.icu.util.BuddhistCalendar) HebrewCalendar(android.icu.util.HebrewCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 52 with GregorianCalendar

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

the class DateFormatTest method TestParseMultiPatternMatch.

@Test
public void TestParseMultiPatternMatch() {
    class TestMultiPatternMatchItem {

        public boolean leniency;

        public String parseString;

        public String pattern;

        // null indicates expected error
        public String expectedResult;

        // Simple constructor
        public TestMultiPatternMatchItem(boolean len, String parString, String patt, String expResult) {
            leniency = len;
            pattern = patt;
            parseString = parString;
            expectedResult = expResult;
        }
    }
    ;
    final TestMultiPatternMatchItem[] items = { // leniency    parse String                  pattern                 expected result
    new TestMultiPatternMatchItem(true, "2013-Sep 13", "yyyy-MMM dd", "2013-Sep 13"), new TestMultiPatternMatchItem(true, "2013-September 14", "yyyy-MMM dd", "2013-Sep 14"), new TestMultiPatternMatchItem(false, "2013-September 15", "yyyy-MMM dd", null), new TestMultiPatternMatchItem(false, "2013-September 16", "yyyy-MMMM dd", "2013-September 16"), new TestMultiPatternMatchItem(true, "2013-Sep 17", "yyyy-LLL dd", "2013-Sep 17"), new TestMultiPatternMatchItem(true, "2013-September 18", "yyyy-LLL dd", "2013-Sep 18"), new TestMultiPatternMatchItem(false, "2013-September 19", "yyyy-LLL dd", null), new TestMultiPatternMatchItem(false, "2013-September 20", "yyyy-LLLL dd", "2013-September 20"), new TestMultiPatternMatchItem(true, "2013 Sat Sep 21", "yyyy EEE MMM dd", "2013 Sat Sep 21"), new TestMultiPatternMatchItem(true, "2013 Sunday Sep 22", "yyyy EEE MMM dd", "2013 Sun Sep 22"), new TestMultiPatternMatchItem(false, "2013 Monday Sep 23", "yyyy EEE MMM dd", null), new TestMultiPatternMatchItem(false, "2013 Tuesday Sep 24", "yyyy EEEE MMM dd", "2013 Tuesday Sep 24"), new TestMultiPatternMatchItem(true, "2013 Wed Sep 25", "yyyy eee MMM dd", "2013 Wed Sep 25"), new TestMultiPatternMatchItem(true, "2013 Thu Sep 26", "yyyy eee MMM dd", "2013 Thu Sep 26"), new TestMultiPatternMatchItem(false, "2013 Friday Sep 27", "yyyy eee MMM dd", null), new TestMultiPatternMatchItem(false, "2013 Saturday Sep 28", "yyyy eeee MMM dd", "2013 Saturday Sep 28"), new TestMultiPatternMatchItem(true, "2013 Sun Sep 29", "yyyy ccc MMM dd", "2013 Sun Sep 29"), new TestMultiPatternMatchItem(true, "2013 Monday Sep 30", "yyyy ccc MMM dd", "2013 Mon Sep 30"), new TestMultiPatternMatchItem(false, "2013 Sunday Oct 13", "yyyy ccc MMM dd", null), new TestMultiPatternMatchItem(false, "2013 Monday Oct 14", "yyyy cccc MMM dd", "2013 Monday Oct 14"), new TestMultiPatternMatchItem(true, "2013 Oct 15 Q4", "yyyy MMM dd QQQ", "2013 Oct 15 Q4"), new TestMultiPatternMatchItem(true, "2013 Oct 16 4th quarter", "yyyy MMM dd QQQ", "2013 Oct 16 Q4"), new TestMultiPatternMatchItem(false, "2013 Oct 17 4th quarter", "yyyy MMM dd QQQ", null), new TestMultiPatternMatchItem(false, "2013 Oct 18 Q4", "yyyy MMM dd QQQ", "2013 Oct 18 Q4"), new TestMultiPatternMatchItem(true, "2013 Oct 19 Q4", "yyyy MMM dd qqqq", "2013 Oct 19 4th quarter"), new TestMultiPatternMatchItem(true, "2013 Oct 20 4th quarter", "yyyy MMM dd qqqq", "2013 Oct 20 4th quarter"), new TestMultiPatternMatchItem(false, "2013 Oct 21 Q4", "yyyy MMM dd qqqq", null), new TestMultiPatternMatchItem(false, "2013 Oct 22 4th quarter", "yyyy MMM dd qqqq", "2013 Oct 22 4th quarter") };
    StringBuffer result = new StringBuffer();
    Date d = new Date();
    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US);
    SimpleDateFormat sdfmt = new SimpleDateFormat();
    ParsePosition p = new ParsePosition(0);
    for (TestMultiPatternMatchItem item : items) {
        cal.clear();
        sdfmt.setCalendar(cal);
        sdfmt.applyPattern(item.pattern);
        sdfmt.setLenient(item.leniency);
        sdfmt.setBooleanAttribute(BooleanAttribute.PARSE_MULTIPLE_PATTERNS_FOR_MATCH, item.leniency);
        result.setLength(0);
        p.setIndex(0);
        p.setErrorIndex(-1);
        d = sdfmt.parse(item.parseString, p);
        if (item.expectedResult == null) {
            if (p.getErrorIndex() != -1)
                continue;
            else
                errln("error: unexpected parse success..." + item.parseString + " w/ lenient=" + item.leniency + " should have failed");
        }
        if (p.getErrorIndex() != -1) {
            errln("error: parse error for string " + item.parseString + " -- idx[" + p.getIndex() + "] errIdx[" + p.getErrorIndex() + "]");
            continue;
        }
        cal.setTime(d);
        result = sdfmt.format(cal, result, new FieldPosition(0));
        if (!result.toString().equalsIgnoreCase(item.expectedResult)) {
            errln("error: unexpected format result. expected - " + item.expectedResult + "  but result was - " + result);
        } else {
            logln("formatted results match! - " + result.toString());
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 53 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project android_frameworks_opt_telephony by LineageOS.

the class TimeZoneLookupHelperTest method assertOffsetResultZoneOffsets.

/**
 * Assert the time zone in the OffsetResult has the expected properties at the specified time.
 */
private static void assertOffsetResultZoneOffsets(long time, int expectedOffsetAtTime, Integer expectedDstAtTime, OffsetResult lookupResult) {
    TimeZone timeZone = lookupResult.getTimeZone();
    GregorianCalendar calendar = new GregorianCalendar(timeZone);
    calendar.setTimeInMillis(time);
    int actualOffsetAtTime = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
    assertEquals(expectedOffsetAtTime, actualOffsetAtTime);
    if (expectedDstAtTime != null) {
        Date date = new Date(time);
        assertEquals(expectedDstAtTime > 0, timeZone.inDaylightTime(date));
        // The code under test assumes DST means +1 in all cases,
        // This code makes fewer assumptions.
        assertEquals(expectedDstAtTime.intValue(), calendar.get(Calendar.DST_OFFSET));
    }
}
Also used : TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date)

Example 54 with GregorianCalendar

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

the class CalendarRegressionTest method Test4083167.

@Test
public void Test4083167() {
    TimeZone saveZone = TimeZone.getDefault();
    try {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        Date firstDate = new Date();
        Calendar cal = new GregorianCalendar();
        cal.setTime(firstDate);
        long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L + cal.get(Calendar.MINUTE) * 60000L + cal.get(Calendar.SECOND) * 1000L + cal.get(Calendar.MILLISECOND);
        logln("Current time: " + firstDate.toString());
        for (int validity = 0; validity < 30; validity++) {
            Date lastDate = new Date(firstDate.getTime() + (long) validity * 1000 * 24 * 60 * 60);
            cal.setTime(lastDate);
            long millisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L + cal.get(Calendar.MINUTE) * 60000L + cal.get(Calendar.SECOND) * 1000L + cal.get(Calendar.MILLISECOND);
            if (firstMillisInDay != millisInDay)
                errln("Day has shifted " + lastDate);
        }
    } finally {
        TimeZone.setDefault(saveZone);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 55 with GregorianCalendar

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

the class CalendarRegressionTest method Test4147269.

/**
 * This is a bug in the validation code of GregorianCalendar. As reported,
 * the bug seems worse than it really is, due to a bug in the way the bug
 * report test was written. In reality the bug is restricted to the
 * DAY_OF_YEAR field. - liu 6/29/98
 */
@Test
public void Test4147269() {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setLenient(false);
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    // Arbitrary date
    tempcal.set(1996, Calendar.JANUARY, 3);
    Date date = tempcal.getTime();
    for (int field = 0; field < calendar.getFieldCount(); field++) {
        calendar.setTime(date);
        // Note: In the bug report, getActualMaximum() was called instead
        // of getMaximum() -- this was an error. The validation code doesn't
        // use getActualMaximum(), since that's too costly.
        int max = calendar.getMaximum(field);
        int value = max + 1;
        calendar.set(field, value);
        try {
            // Force time computation
            calendar.getTime();
            // We expect an exception to be thrown. If we fall through
            // to the next line, then we have a bug.
            errln("Test failed with field " + FIELD_NAME[field] + ", date before: " + date + ", date after: " + calendar.getTime() + ", value: " + value + " (max = " + max + ")");
        } catch (IllegalArgumentException e) {
            System.out.print("");
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) 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