Java Code Examples for android.icu.util.Calendar#FEBRUARY

The following examples show how to use android.icu.util.Calendar#FEBRUARY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatePickerCalendarDelegate.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public static int getDaysInMonth(int month, int year) {
    switch (month) {
        case Calendar.JANUARY:
        case Calendar.MARCH:
        case Calendar.MAY:
        case Calendar.JULY:
        case Calendar.AUGUST:
        case Calendar.OCTOBER:
        case Calendar.DECEMBER:
            return 31;
        case Calendar.APRIL:
        case Calendar.JUNE:
        case Calendar.SEPTEMBER:
        case Calendar.NOVEMBER:
            return 30;
        case Calendar.FEBRUARY:
            return (year % 4 == 0) ? 29 : 28;
        default:
            throw new IllegalArgumentException("Invalid Month");
    }
}
 
Example 2
Source File: SimpleMonthView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static int getDaysInMonth(int month, int year) {
    switch (month) {
        case Calendar.JANUARY:
        case Calendar.MARCH:
        case Calendar.MAY:
        case Calendar.JULY:
        case Calendar.AUGUST:
        case Calendar.OCTOBER:
        case Calendar.DECEMBER:
            return 31;
        case Calendar.APRIL:
        case Calendar.JUNE:
        case Calendar.SEPTEMBER:
        case Calendar.NOVEMBER:
            return 30;
        case Calendar.FEBRUARY:
            return (year % 4 == 0) ? 29 : 28;
        default:
            throw new IllegalArgumentException("Invalid Month");
    }
}
 
Example 3
Source File: DatePickerCalendarDelegate.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
public static int getDaysInMonth(int month, int year) {
    switch (month) {
        case Calendar.JANUARY:
        case Calendar.MARCH:
        case Calendar.MAY:
        case Calendar.JULY:
        case Calendar.AUGUST:
        case Calendar.OCTOBER:
        case Calendar.DECEMBER:
            return 31;
        case Calendar.APRIL:
        case Calendar.JUNE:
        case Calendar.SEPTEMBER:
        case Calendar.NOVEMBER:
            return 30;
        case Calendar.FEBRUARY:
            return (year % 4 == 0) ? 29 : 28;
        default:
            throw new IllegalArgumentException("Invalid Month");
    }
}
 
Example 4
Source File: SimpleMonthView.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
private static int getDaysInMonth(int month, int year) {
    switch (month) {
        case Calendar.JANUARY:
        case Calendar.MARCH:
        case Calendar.MAY:
        case Calendar.JULY:
        case Calendar.AUGUST:
        case Calendar.OCTOBER:
        case Calendar.DECEMBER:
            return 31;
        case Calendar.APRIL:
        case Calendar.JUNE:
        case Calendar.SEPTEMBER:
        case Calendar.NOVEMBER:
            return 30;
        case Calendar.FEBRUARY:
            return (year % 4 == 0) ? 29 : 28;
        default:
            throw new IllegalArgumentException("Invalid Month");
    }
}
 
Example 5
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestAdd520() {
    int y = 1997, m = Calendar.FEBRUARY, d = 1;
    GregorianCalendar temp = new GregorianCalendar( y, m, d );
    check520(temp, y, m, d);

    temp.add( Calendar.YEAR, 1 );
    y++;
    check520(temp, y, m, d);

    temp.add( Calendar.MONTH, 1 );
    m++;
    check520(temp, y, m, d);

    temp.add( Calendar.DATE, 1 );
    d++;
    check520(temp, y, m, d);

    temp.add( Calendar.DATE, 2 );
    d += 2;
    check520(temp, y, m, d);

    temp.add( Calendar.DATE, 28 );
    d = 1; ++m;
    check520(temp, y, m, d);
}
 
Example 6
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Adding 12 months behaves differently from adding 1 year
 */
@Test
public void Test4165343() {
    GregorianCalendar calendar = new GregorianCalendar(1996, Calendar.FEBRUARY, 29);
    Date start = calendar.getTime();
    logln("init date: " + start);
    calendar.add(Calendar.MONTH, 12); 
    Date date1 = calendar.getTime();
    logln("after adding 12 months: " + date1);
    calendar.setTime(start);
    calendar.add(Calendar.YEAR, 1);
    Date date2 = calendar.getTime();
    logln("after adding one year : " + date2);
    if (date1.equals(date2)) {
        logln("Test passed");
    } else {
        errln("Test failed");
    }
}
 
Example 7
Source File: DangiTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestInitWithCurrentTime() {
    // If the chinese calendar current millis isn't called, the default year is wrong.
    // this test is assuming the 'year' is the current cycle
    // so when we cross a cycle boundary, the target will need to change
    // that shouldn't be for awhile yet... 

    Calendar cc = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    cc.set(Calendar.EXTENDED_YEAR, 4338);
    cc.set(Calendar.MONTH, 0);
     // need to set leap month flag off, otherwise, the test case always fails when
     // current time is in a leap month
    cc.set(Calendar.IS_LEAP_MONTH, 0);
    cc.set(Calendar.DATE, 19);
    cc.set(Calendar.HOUR_OF_DAY, 0);
    cc.set(Calendar.MINUTE, 0);
    cc.set(Calendar.SECOND, 0);
    cc.set(Calendar.MILLISECOND, 0);

    cc.add(Calendar.DATE, 1);
 
    Calendar cal = new GregorianCalendar(2005, Calendar.FEBRUARY, 28);
    Date target = cal.getTime();
    Date result = cc.getTime();

    assertEquals("chinese and gregorian date should match", target, result);
}
 
Example 8
Source File: ChineseTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestInitWithCurrentTime() {
    // jb4555
    // if the chinese calendar current millis isn't called, the default year is wrong.
    // this test is assuming the 'year' is the current cycle
    // so when we cross a cycle boundary, the target will need to change
    // that shouldn't be for awhile yet... 

    ChineseCalendar cc = new ChineseCalendar();
    cc.set(Calendar.YEAR, 22);
    cc.set(Calendar.MONTH, 0);
     // need to set leap month flag off, otherwise, the test case always fails when
     // current time is in a leap month
    cc.set(Calendar.IS_LEAP_MONTH, 0);
    cc.set(Calendar.DATE, 19);
    cc.set(Calendar.HOUR_OF_DAY, 0);
    cc.set(Calendar.MINUTE, 0);
    cc.set(Calendar.SECOND, 0);
    cc.set(Calendar.MILLISECOND, 0);

    cc.add(Calendar.DATE, 1);
 
    Calendar cal = new GregorianCalendar(2005, Calendar.FEBRUARY, 28);
    Date target = cal.getTime();
    Date result = cc.getTime();

    assertEquals("chinese and gregorian date should match", target, result);
}
 
Example 9
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestT5555() throws Exception
{
    Calendar cal = Calendar.getInstance();
    
    // Set date to Wednesday, February 21, 2007
    cal.set(2007, Calendar.FEBRUARY, 21);

    try {
        // Advance month by three years
        cal.add(Calendar.MONTH, 36);
        
        // Move to last Wednesday of month.
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
        
        cal.getTime();
    } catch (Exception e) {
        errln("Got an exception calling getTime().");
    }
    
    int yy, mm, dd, ee;
    
    yy = cal.get(Calendar.YEAR);
    mm = cal.get(Calendar.MONTH);
    dd = cal.get(Calendar.DATE);
    ee = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
    
    if (yy != 2010 || mm != Calendar.FEBRUARY || dd != 24 || ee != Calendar.WEDNESDAY) {
        errln("Got date " + yy + "/" + (mm + 1) + "/" + dd + ", expected 2010/2/24");
    }
}
 
Example 10
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
                calendar.setTime(dateBefore); // restore calendar for next
                                              // 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");
                }
            }
        }
    }
}
 
Example 11
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestFebruary() {
    // Time zone with daylight savings time from the first Sunday in November
    // to the last Sunday in February.
    // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
    //
    // Note: In tzdata2007h, the rule had changed, so no actual zones uses
    // lastSun in Feb anymore.
    SimpleTimeZone tz1 = new SimpleTimeZone(
                       -3 * MILLIS_PER_HOUR,                    // raw offset: 3h before (west of) GMT
                       "nov-feb",
                       Calendar.NOVEMBER, 1, Calendar.SUNDAY,   // start: November, first, Sunday
                       0,                                       //        midnight wall time
                       Calendar.FEBRUARY, -1, Calendar.SUNDAY,  // end:   February, last, Sunday
                       0);                                      //        midnight wall time

    // Now hardcode the same rules as for Brazil in tzdata 2006n, so that
    // we cover the intended code even when in the future zoneinfo hardcodes
    // these transition dates.
    SimpleTimeZone tz2= new SimpleTimeZone(
                       -3 * MILLIS_PER_HOUR,                    // raw offset: 3h before (west of) GMT
                       "nov-feb2",
                       Calendar.NOVEMBER, 1, -Calendar.SUNDAY,  // start: November, 1 or after, Sunday
                       0,                                       //        midnight wall time
                       Calendar.FEBRUARY, -29, -Calendar.SUNDAY,// end:   February, 29 or before, Sunday
                       0);                                      //        midnight wall time

    // Gregorian calendar with the UTC time zone for getting sample test date/times.
    GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
    // "Unable to create the UTC calendar: %s"

    int[] data = {
        // UTC time (6 fields) followed by
        // expected time zone offset in hours after GMT (negative=before GMT).
        // int year, month, day, hour, minute, second, offsetHours
        2006, Calendar.NOVEMBER,  5, 02, 59, 59, -3,
        2006, Calendar.NOVEMBER,  5, 03, 00, 00, -2,
        2007, Calendar.FEBRUARY, 25, 01, 59, 59, -2,
        2007, Calendar.FEBRUARY, 25, 02, 00, 00, -3,

        2007, Calendar.NOVEMBER,  4, 02, 59, 59, -3,
        2007, Calendar.NOVEMBER,  4, 03, 00, 00, -2,
        2008, Calendar.FEBRUARY, 24, 01, 59, 59, -2,
        2008, Calendar.FEBRUARY, 24, 02, 00, 00, -3,

        2008, Calendar.NOVEMBER,  2, 02, 59, 59, -3,
        2008, Calendar.NOVEMBER,  2, 03, 00, 00, -2,
        2009, Calendar.FEBRUARY, 22, 01, 59, 59, -2,
        2009, Calendar.FEBRUARY, 22, 02, 00, 00, -3,

        2009, Calendar.NOVEMBER,  1, 02, 59, 59, -3,
        2009, Calendar.NOVEMBER,  1, 03, 00, 00, -2,
        2010, Calendar.FEBRUARY, 28, 01, 59, 59, -2,
        2010, Calendar.FEBRUARY, 28, 02, 00, 00, -3
    };

    TimeZone timezones[] = { tz1, tz2 };

    TimeZone tz;
    Date dt;
    int t, i, raw, dst;
    int[] offsets = new int[2]; // raw = offsets[0], dst = offsets[1]
    for (t = 0; t < timezones.length; ++t) {
        tz = timezones[t];
        for (i = 0; i < data.length; i+=7) {
            gc.set(data[i], data[i+1], data[i+2],
                   data[i+3], data[i+4], data[i+5]);
            dt = gc.getTime();
            tz.getOffset(dt.getTime(), false, offsets);
            raw = offsets[0];
            dst = offsets[1];
            if ((raw + dst) != data[i+6] * MILLIS_PER_HOUR) {
                errln("test case " + t + "." + (i/7) + ": " +
                      "tz.getOffset(" + data[i] + "-" + (data[i+1] + 1) + "-" + data[i+2] + " " +
                      data[i+3] + ":" + data[i+4] + ":" + data[i+5] +
                      ") returns " + raw + "+" + dst + " != " + data[i+6] * MILLIS_PER_HOUR);
            }
        }
    }
}
 
Example 12
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * TimeZone broken at midnight.  The TimeZone code fails to handle
 * transitions at midnight correctly.
 */
@Test
public void Test4162593() {
    SimpleDateFormat fmt = new SimpleDateFormat("z", Locale.US);
    final int ONE_HOUR = 60*60*1000;
    final float H = (float) ONE_HOUR;
    TimeZone initialZone = TimeZone.getDefault();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm z");

    SimpleTimeZone asuncion = new SimpleTimeZone(-4*ONE_HOUR, "America/Asuncion" /*PY%sT*/,
        Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR,
        Calendar.MARCH, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR);

    /* Zone
     * Starting time
     * Transition expected between start+1H and start+2H
     */
    Object[] DATA = {
        new SimpleTimeZone(2*ONE_HOUR, "Asia/Damascus" /*EE%sT*/,
            Calendar.APRIL, 1, 0 /*DOM*/, 0*ONE_HOUR,
            Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR),
        new int[] {1998, Calendar.SEPTEMBER, 30, 22, 0},
        Boolean.TRUE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 28, 22, 0},
        Boolean.FALSE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 29, 22, 0},
        Boolean.TRUE,
    };

    String[] zone = new String[4];

    for (int j=0; j<DATA.length; j+=3) {
        TimeZone tz = (TimeZone)DATA[j];
        TimeZone.setDefault(tz);
        fmt.setTimeZone(tz);
        sdf.setTimeZone(tz);

        // Must construct the Date object AFTER setting the default zone
        int[] p = (int[])DATA[j+1];
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(p[0], p[1], p[2], p[3], p[4]);
        long start = cal.getTime().getTime();
        boolean transitionExpected = ((Boolean)DATA[j+2]).booleanValue();

        logln(tz.getID() + ":");
        for (int i=0; i<4; ++i) {
            Date d = new Date(start + i*ONE_HOUR);
            zone[i] = fmt.format(d);
            logln("" + i + ": " + sdf.format(d) + " => " + zone[i] +
                  " (" + d.getTime()/H + ")");
        }
        cal.set(p[0], p[1], p[2], 0, 0);
        for (int i=0; i<4; ++i) {
            int h = 22+i;
            int dom = p[2]+(h>=24?1:0);
            h %= 24;
            int ms = h*ONE_HOUR;
            cal.clear();
            cal.set(p[0], p[1], dom, 0, 0);
            int off = tz.getOffset(GregorianCalendar.AD,
                                   cal.get(Calendar.YEAR),
                                   cal.get(Calendar.MONTH),
                                   cal.get(Calendar.DATE),
                                   cal.get(Calendar.DAY_OF_WEEK),
                                   ms);
            cal.add(Calendar.HOUR, h);
            int dstOffset = cal.get(Calendar.DST_OFFSET);
            logln("h=" + h + "; dom=" + dom +
                  "; ZONE_OFFSET=" + cal.get(Calendar.ZONE_OFFSET)/H +
                  "; DST_OFFSET=" + dstOffset/H +
                  "; getOffset()=" + off/H +
                  " (" + cal.getTime().getTime()/H + ")");
        }
        if (zone[0].equals(zone[1]) &&
            (zone[1].equals(zone[2]) != transitionExpected) &&
            zone[2].equals(zone[3])) {
            logln("Ok: transition " + transitionExpected);
        } else {
            errln("FAIL: expected " +
                  (transitionExpected?"transition":"no transition"));
        }
    }

// restore the initial time zone so that this test case
// doesn't affect the others.
TimeZone.setDefault(initialZone);
}