Search in sources :

Example 16 with GregorianCalendar

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

the class CalendarRegressionTest method Test4096231.

@Test
public void Test4096231() {
    TimeZone GMT = TimeZone.getTimeZone("GMT");
    TimeZone PST = TimeZone.getTimeZone("PST");
    int sec = 0, min = 0, hr = 0, day = 1, month = 10, year = 1997;
    Calendar cal1 = new GregorianCalendar(PST);
    cal1.setTime(new Date(880698639000L));
    int p;
    logln("PST 1 is: " + (p = cal1.get(Calendar.HOUR_OF_DAY)));
    cal1.setTimeZone(GMT);
    // Issue 1: Changing the timezone doesn't change the
    // represented time.
    int h1, h2;
    logln("GMT 1 is: " + (h1 = cal1.get(Calendar.HOUR_OF_DAY)));
    cal1.setTime(new Date(880698639000L));
    logln("GMT 2 is: " + (h2 = cal1.get(Calendar.HOUR_OF_DAY)));
    // to 4177484.
    if (p == h1 || h1 != h2)
        errln("Fail: Hour same in different zones");
    Calendar cal2 = new GregorianCalendar(GMT);
    Calendar cal3 = new GregorianCalendar(PST);
    cal2.set(Calendar.MILLISECOND, 0);
    cal3.set(Calendar.MILLISECOND, 0);
    cal2.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH), cal1.get(Calendar.DAY_OF_MONTH), cal1.get(Calendar.HOUR_OF_DAY), cal1.get(Calendar.MINUTE), cal1.get(Calendar.SECOND));
    long t1, t2, t3, t4;
    logln("RGMT 1 is: " + (t1 = cal2.getTime().getTime()));
    cal3.set(year, month, day, hr, min, sec);
    logln("RPST 1 is: " + (t2 = cal3.getTime().getTime()));
    cal3.setTimeZone(GMT);
    logln("RGMT 2 is: " + (t3 = cal3.getTime().getTime()));
    cal3.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH), cal1.get(Calendar.DAY_OF_MONTH), cal1.get(Calendar.HOUR_OF_DAY), cal1.get(Calendar.MINUTE), cal1.get(Calendar.SECOND));
    // Issue 2: Calendar continues to use the timezone in its
    // constructor for set() conversions, regardless
    // of calls to setTimeZone()
    logln("RGMT 3 is: " + (t4 = cal3.getTime().getTime()));
    if (t1 == t2 || t1 != t4 || t2 != t3)
        errln("Fail: Calendar zone behavior faulty");
}
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 17 with GregorianCalendar

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

the class CalendarRegressionTest method Test4162587.

/**
 * Calendar and Date HOUR broken. If HOUR is out-of-range, Calendar and Date
 * classes will misbehave.
 */
@Test
public void Test4162587() {
    TimeZone tz = TimeZone.getTimeZone("PST");
    TimeZone.setDefault(tz);
    GregorianCalendar cal = new GregorianCalendar(tz);
    Date d;
    for (int i = 0; i < 5; ++i) {
        if (i > 0)
            logln("---");
        cal.clear();
        cal.set(1998, Calendar.APRIL, 5, i, 0);
        d = cal.getTime();
        String s0 = d.toString();
        logln("0 " + i + ": " + s0);
        cal.clear();
        cal.set(1998, Calendar.APRIL, 4, i + 24, 0);
        d = cal.getTime();
        String sPlus = d.toString();
        logln("+ " + i + ": " + sPlus);
        cal.clear();
        cal.set(1998, Calendar.APRIL, 6, i - 24, 0);
        d = cal.getTime();
        String sMinus = d.toString();
        logln("- " + i + ": " + sMinus);
        if (!s0.equals(sPlus) || !s0.equals(sMinus)) {
            errln("Fail: All three lines must match");
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 18 with GregorianCalendar

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

the class CalendarRegressionTest method Test4167060.

/**
 * Calendar.getActualMaximum(YEAR) works wrong.
 */
@Test
public void Test4167060() {
    int field = Calendar.YEAR;
    DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G", Locale.US);
    GregorianCalendar[] calendars = { new GregorianCalendar(100, Calendar.NOVEMBER, 1), new GregorianCalendar(-99, /* 100BC */
    Calendar.JANUARY, 1), new GregorianCalendar(1996, Calendar.FEBRUARY, 29) };
    String[] id = { "Hybrid", "Gregorian", "Julian" };
    for (int k = 0; k < 3; ++k) {
        logln("--- " + id[k] + " ---");
        for (int j = 0; j < calendars.length; ++j) {
            GregorianCalendar calendar = calendars[j];
            if (k == 1) {
                calendar.setGregorianChange(new Date(Long.MIN_VALUE));
            } else if (k == 2) {
                calendar.setGregorianChange(new Date(Long.MAX_VALUE));
            }
            format.setCalendar((Calendar) calendar.clone());
            Date dateBefore = calendar.getTime();
            int maxYear = calendar.getActualMaximum(field);
            logln("maxYear: " + maxYear + " for " + format.format(calendar.getTime()));
            logln("date before: " + format.format(dateBefore));
            int[] years = { 2000, maxYear - 1, maxYear, maxYear + 1 };
            for (int i = 0; i < years.length; i++) {
                boolean valid = years[i] <= maxYear;
                calendar.set(field, years[i]);
                Date dateAfter = calendar.getTime();
                int newYear = calendar.get(field);
                // restore calendar for next
                calendar.setTime(dateBefore);
                // use
                logln(" Year " + years[i] + (valid ? " ok " : " bad") + " => " + format.format(dateAfter));
                if (valid && newYear != years[i]) {
                    errln("  FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
                } else if (!valid && newYear == years[i]) {
                    // We no longer require strict year maxima. That is, the
                    // calendar
                    // algorithm may work for values > the stated maximum.
                    // errln(" FAIL: " + newYear + " should be invalid");
                    logln("  Note: " + newYear + " > maximum, but still valid");
                }
            }
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 19 with GregorianCalendar

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

the class CalendarRegressionTest method getAssociatedDate.

/**
 * Get the associated date starting from a specified date NOTE: the
 * unnecessary "getTime()'s" below are a work-around for a bug in jdk 1.1.3
 * (and probably earlier versions also)
 * <p>
 *
 * @param d
 *            The date to start from
 */
public static Date getAssociatedDate(Date d) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(d);
    // cal.getTime(); // <--- REMOVE THIS TO SEE BUG
    while (true) {
        int wd = cal.get(Calendar.DAY_OF_WEEK);
        if (wd == Calendar.SATURDAY || wd == Calendar.SUNDAY) {
            cal.add(Calendar.DATE, 1);
        // cal.getTime();
        } else
            break;
    }
    return cal.getTime();
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar)

Example 20 with GregorianCalendar

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

the class IBMCalendarTest method TestWeekShift.

/**
 * setFirstDayOfWeek and setMinimalDaysInFirstWeek may change the
 * field <=> time mapping, since they affect the interpretation of
 * the WEEK_OF_MONTH or WEEK_OF_YEAR fields.
 */
@Test
public void TestWeekShift() {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"), new Locale("en", "US"));
    // Wed Aug 08 01:00:00 PDT 2001
    cal.setTime(new Date(997257600000L));
    // 26 27 28 29 30 31
    for (int pass = 0; pass < 2; ++pass) {
        if (pass == 0) {
            cal.setFirstDayOfWeek(Calendar.WEDNESDAY);
            cal.setMinimalDaysInFirstWeek(4);
        } else {
            cal.setFirstDayOfWeek(Calendar.SUNDAY);
            cal.setMinimalDaysInFirstWeek(4);
        }
        // Force recalc
        cal.add(Calendar.DATE, 1);
        cal.add(Calendar.DATE, -1);
        // Get time -- should not change
        Date time1 = cal.getTime();
        // calendar should do so automatically.
        if (pass == 0) {
            cal.setFirstDayOfWeek(Calendar.THURSDAY);
        } else {
            cal.setMinimalDaysInFirstWeek(5);
        }
        int woy1 = cal.get(Calendar.WEEK_OF_YEAR);
        int wom1 = cal.get(Calendar.WEEK_OF_MONTH);
        // Force recalc
        cal.add(Calendar.DATE, 1);
        cal.add(Calendar.DATE, -1);
        int woy2 = cal.get(Calendar.WEEK_OF_YEAR);
        int wom2 = cal.get(Calendar.WEEK_OF_MONTH);
        Date time2 = cal.getTime();
        if (!time1.equals(time2)) {
            errln("FAIL: shifting week should not alter time");
        } else {
            logln(time1.toString());
        }
        if (woy1 == woy2 && wom1 == wom2) {
            logln("Ok: WEEK_OF_YEAR: " + woy1 + ", WEEK_OF_MONTH: " + wom1);
        } else {
            errln("FAIL: WEEK_OF_YEAR: " + woy1 + " => " + woy2 + ", WEEK_OF_MONTH: " + wom1 + " => " + wom2 + " after week shift");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) 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) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Aggregations

GregorianCalendar (android.icu.util.GregorianCalendar)111 Test (org.junit.Test)102 Date (java.util.Date)63 Calendar (android.icu.util.Calendar)40 SimpleDateFormat (android.icu.text.SimpleDateFormat)34 SimpleTimeZone (android.icu.util.SimpleTimeZone)28 TimeZone (android.icu.util.TimeZone)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