Search in sources :

Example 61 with GregorianCalendar

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

the class CalendarRegressionTest method Test4145983.

/**
 * Maximum value for YEAR field wrong.
 */
@Test
public void Test4145983() {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date[] DATES = { new Date(Long.MAX_VALUE), new Date(Long.MIN_VALUE) };
    for (int i = 0; i < DATES.length; ++i) {
        calendar.setTime(DATES[i]);
        int year = calendar.get(Calendar.YEAR);
        int maxYear = calendar.getMaximum(Calendar.YEAR);
        if (year > maxYear) {
            errln("Failed for " + DATES[i].getTime() + " ms: year=" + year + ", maxYear=" + maxYear);
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 62 with GregorianCalendar

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

the class CalendarRegressionTest method Test4136399.

/**
 * Calendar and GregorianCalendar hashCode() methods need improvement.
 * Calendar needs a good implementation that subclasses can override, and
 * GregorianCalendar should use that implementation.
 */
@Test
public void Test4136399() {
    /*
         * Note: This test is actually more strict than it has to be.
         * Technically, there is no requirement that unequal objects have
         * unequal hashes. We only require equal objects to have equal hashes.
         * It is desirable for unequal objects to have distributed hashes, but
         * there is no hard requirement here.
         * 
         * In this test we make assumptions about certain attributes of calendar
         * objects getting represented in the hash, which need not always be the
         * case (although it does work currently with the given test).
         */
    Calendar a = Calendar.getInstance();
    Calendar b = (Calendar) a.clone();
    if (a.hashCode() != b.hashCode()) {
        errln("Calendar hash code unequal for cloned objects");
    }
    TimeZone atz1 = a.getTimeZone();
    TimeZone atz2 = (TimeZone) atz1.clone();
    if (!atz1.equals(atz2)) {
        errln("The clone timezones are not equal");
    }
    if (atz1.hashCode() != atz2.hashCode()) {
        errln("TimeZone hash code unequal for cloned objects");
    }
    b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores minimal days in first week");
    }
    b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());
    // Next day
    b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1);
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores first day of week");
    }
    b.setFirstDayOfWeek(a.getFirstDayOfWeek());
    b.setLenient(!a.isLenient());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores lenient setting");
    }
    b.setLenient(a.isLenient());
    // Assume getTimeZone() returns a reference, not a clone
    // of a reference -- this is true as of this writing
    TimeZone atz = a.getTimeZone();
    TimeZone btz = b.getTimeZone();
    btz.setRawOffset(atz.getRawOffset() + 60 * 60 * 1000);
    if (atz.hashCode() == btz.hashCode()) {
        errln(atz.hashCode() + "==" + btz.hashCode());
    }
    if (a.getTimeZone() != b.getTimeZone() && a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores zone");
    }
    b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());
    GregorianCalendar c = new GregorianCalendar();
    GregorianCalendar d = (GregorianCalendar) c.clone();
    if (c.hashCode() != d.hashCode()) {
        errln("GregorianCalendar hash code unequal for clones objects");
    }
    Date cutover = c.getGregorianChange();
    d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60 * 1000));
    if (c.hashCode() == d.hashCode()) {
        errln("GregorianCalendar hash code ignores cutover");
    }
}
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 63 with GregorianCalendar

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

the class EthiopicTest method TestEraStart.

// basic check to see that we print out eras ok
// eventually should modify to use locale strings and formatter appropriate to coptic calendar
@Test
public void TestEraStart() {
    SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd, yyyy GG");
    fmt.setCalendar(new EthiopicCalendar());
    EthiopicCalendar cal = new EthiopicCalendar(1, 0, 1);
    assertEquals("Ethiopic Date", "Wed Jan 01, 0001 AD", fmt.format(cal));
    cal.set(Calendar.ERA, 0);
    cal.set(Calendar.YEAR, 5500);
    assertEquals("Ethiopic Date", "Tue Jan 01, 5500 BC", fmt.format(cal));
    // The gregorian calendar gets off by two days when
    // the date gets low, unless the gregorian changeover is set to
    // very early.  The funny thing is, it's ok for dates in the year
    // 283, but not in the year 7, and it claims to be ok until the year 4.
    // should track down when the dates start to differ...
    GregorianCalendar gc = new GregorianCalendar();
    // act like proleptic Gregorian
    gc.setGregorianChange(new Date(Long.MIN_VALUE));
    gc.setTime(cal.getTime());
    fmt.setCalendar(new GregorianCalendar());
    assertEquals("Gregorian Date", "Tue Aug 28, 0007 AD", fmt.format(gc));
}
Also used : EthiopicCalendar(android.icu.util.EthiopicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 64 with GregorianCalendar

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

the class HolidayTest method init.

@Before
public void init() throws Exception {
    if (cal == null) {
        cal = new GregorianCalendar(1, 0, 1);
        longTimeAgo = cal.getTime();
        now = new Date();
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Before(org.junit.Before)

Example 65 with GregorianCalendar

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

the class IndianTest 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));
    // Indian Calendar
    IndianCalendar iCal = new IndianCalendar();
    Date iToday = iCal.getTime();
    iCal.add(IndianCalendar.MONTH, 2);
    Date iFuture = iCal.getTime();
    DateFormat iDF = DateFormat.getDateInstance(iCal, DateFormat.FULL);
    logln("Indian calendar: " + iDF.format(iToday) + " + 2 months = " + iDF.format(iFuture));
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) IndianCalendar(android.icu.util.IndianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) IndianCalendar(android.icu.util.IndianCalendar) 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