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

The following examples show how to use android.icu.util.Calendar#MARCH . 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 5 votes vote down vote up
@Test
public void TestActualMinMax() {
    Calendar cal = new GregorianCalendar(1967, Calendar.MARCH, 10);
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setMinimalDaysInFirstWeek(3);

    if (cal.getActualMinimum(Calendar.DAY_OF_MONTH) != 1)
        errln("Actual minimum date for 3/10/1967 should have been 1; got " +
              cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 31)
        errln("Actual maximum date for 3/10/1967 should have been 31; got " +
              cal.getActualMaximum(Calendar.DAY_OF_MONTH));

    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 28)
        errln("Actual maximum date for 2/10/1967 should have been 28; got " +
              cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 365)
        errln("Number of days in 1967 should have been 365; got " +
              cal.getActualMaximum(Calendar.DAY_OF_YEAR));

    cal.set(Calendar.YEAR, 1968);
    if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 29)
        errln("Actual maximum date for 2/10/1968 should have been 29; got " +
              cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 366)
        errln("Number of days in 1968 should have been 366; got " +
              cal.getActualMaximum(Calendar.DAY_OF_YEAR));
    // Using week settings of SUNDAY/3 (see above)
    if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 52)
        errln("Number of weeks in 1968 should have been 52; got " +
              cal.getActualMaximum(Calendar.WEEK_OF_YEAR));

    cal.set(Calendar.YEAR, 1976);
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); // Added - Liu 11/6/00
    // Using week settings of SUNDAY/3 (see above)
    if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 53)
        errln("Number of weeks in 1976 should have been 53; got " +
              cal.getActualMaximum(Calendar.WEEK_OF_YEAR));
}
 
Example 6
Source File: TimeZoneRuleTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestT8943() {
    String id = "Ekaterinburg Time";
    String stdName = "Ekaterinburg Standard Time";
    String dstName = "Ekaterinburg Daylight Time";

    InitialTimeZoneRule initialRule = new InitialTimeZoneRule(stdName, 18000000, 0);
    RuleBasedTimeZone rbtz = new RuleBasedTimeZone(id, initialRule);

    DateTimeRule dtRule = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 10800000, DateTimeRule.WALL_TIME);
    AnnualTimeZoneRule atzRule = new AnnualTimeZoneRule(stdName, 18000000, 0, dtRule, 2000, 2010);
    rbtz.addTransitionRule(atzRule);

    dtRule = new DateTimeRule(Calendar.MARCH, -1, Calendar.SUNDAY, 7200000, DateTimeRule.WALL_TIME);
    atzRule = new AnnualTimeZoneRule(dstName, 18000000, 3600000, dtRule, 2000, 2010);
    rbtz.addTransitionRule(atzRule);

    dtRule = new DateTimeRule(Calendar.JANUARY, 1, 0, DateTimeRule.WALL_TIME);
    atzRule = new AnnualTimeZoneRule(stdName, 21600000, 0, dtRule, 2011, AnnualTimeZoneRule.MAX_YEAR);
    rbtz.addTransitionRule(atzRule);

    dtRule = new DateTimeRule(Calendar.JANUARY, 1, 1, DateTimeRule.WALL_TIME);
    atzRule = new AnnualTimeZoneRule(dstName, 21600000, 0, dtRule, 2011, AnnualTimeZoneRule.MAX_YEAR);
    rbtz.addTransitionRule(atzRule);

    int[] expected = {21600000, 0};
    int[] offsets = new int[2];
    try {
        rbtz.getOffset(1293822000000L /* 2010-12-31 19:00:00 UTC */, false, offsets);
        if (offsets[0] != expected[0] || offsets[1] != expected[1]) {
            errln("Fail: Wrong offsets: " + offsets[0] + "/" + offsets[1] + " Expected: " + expected[0] + "/" + expected[1]);
        }
    } catch (Exception e) {
        errln("Fail: Exception thrown - " + e.getMessage());
    }
}
 
Example 7
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Bug 4107276
 */
@Test
public void TestDSTSavings() {
    // It might be better to find a way to integrate this test into the main TimeZone
    // tests above, but I don't have time to figure out how to do this (or if it's
    // even really a good idea).  Let's consider that a future.  --rtg 1/27/98
    SimpleTimeZone tz = new SimpleTimeZone(-5 * millisPerHour, "dstSavingsTest",
                                           Calendar.MARCH, 1, 0, 0, Calendar.SEPTEMBER, 1, 0, 0,
                                           (int)(0.5 * millisPerHour));

    if (tz.getRawOffset() != -5 * millisPerHour)
        errln("Got back a raw offset of " + (tz.getRawOffset() / millisPerHour) +
              " hours instead of -5 hours.");
    if (!tz.useDaylightTime())
        errln("Test time zone should use DST but claims it doesn't.");
    if (tz.getDSTSavings() != 0.5 * millisPerHour)
        errln("Set DST offset to 0.5 hour, but got back " + (tz.getDSTSavings() /
                                                             millisPerHour) + " hours instead.");

    int offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JANUARY, 1,
                              Calendar.THURSDAY, 10 * millisPerHour);
    if (offset != -5 * millisPerHour)
        errln("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got "
              + (offset / millisPerHour) + " hours.");

    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JUNE, 1, Calendar.MONDAY,
                          10 * millisPerHour);
    if (offset != -4.5 * millisPerHour)
        errln("The offset for 10 AM, 6/1/98 should have been -4.5 hours, but we got "
              + (offset / millisPerHour) + " hours.");

    tz.setDSTSavings(millisPerHour);
    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JANUARY, 1,
                          Calendar.THURSDAY, 10 * millisPerHour);
    if (offset != -5 * millisPerHour)
        errln("The offset for 10 AM, 1/1/98 should have been -5 hours, but we got "
              + (offset / millisPerHour) + " hours.");

    offset = tz.getOffset(GregorianCalendar.AD, 1998, Calendar.JUNE, 1, Calendar.MONDAY,
                          10 * millisPerHour);
    if (offset != -4 * millisPerHour)
        errln("The offset for 10 AM, 6/1/98 (with a 1-hour DST offset) should have been -4 hours, but we got "
              + (offset / millisPerHour) + " hours.");
}
 
Example 8
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestObservesDaylightTime() {
    boolean observesDaylight;
    long current = System.currentTimeMillis();

    // J2ObjC change: time zones going through adjustments are problematic when comparing
    // the ICU tz data vs. the operating system tz data.
    Set<String> unstableTzids = new HashSet<>();
    // https://github.com/eggert/tz/blob/379f7ba9b81eee97f0209a322540b4a522122b5d/africa#L847
    unstableTzids.add("Africa/Casablanca");
    unstableTzids.add("Africa/El_Aaiun");

    String[] tzids = TimeZone.getAvailableIDs();
    for (String tzid : tzids) {
        // OlsonTimeZone
        TimeZone tz = TimeZone.getTimeZone(tzid, TimeZone.TIMEZONE_ICU);
        observesDaylight = tz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [OlsonTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }

        // RuleBasedTimeZone
        RuleBasedTimeZone rbtz = createRBTZ((BasicTimeZone)tz, current);
        boolean observesDaylightRBTZ = rbtz.observesDaylightTime();
        if (observesDaylightRBTZ != isDaylightTimeAvailable(rbtz, current)) {
            errln("Fail: [RuleBasedTimeZone] observesDaylightTime() returned " + observesDaylightRBTZ + " for " + rbtz.getID());
        } else if (observesDaylight != observesDaylightRBTZ) {
            errln("Fail: RuleBasedTimeZone " + rbtz.getID() + " returns " + observesDaylightRBTZ + ", but different from match OlsonTimeZone");
        }

        // JavaTimeZone
        tz = TimeZone.getTimeZone(tzid, TimeZone.TIMEZONE_JDK);
        observesDaylight = tz.observesDaylightTime();
        if (!unstableTzids.contains(tzid)
            && observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [JavaTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }

        // VTimeZone
        tz = VTimeZone.getTimeZone(tzid);
        observesDaylight = tz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(tz, current)) {
            errln("Fail: [VTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + tzid);
        }
    }

    // SimpleTimeZone
    SimpleTimeZone[] stzs = {
        new SimpleTimeZone(0, "STZ0"),
        new SimpleTimeZone(-5*60*60*1000, "STZ-5D", Calendar.MARCH, 2, Calendar.SUNDAY, 2*60*60*1000,
                Calendar.NOVEMBER, 1, Calendar.SUNDAY, 2*60*60*1000),
    };
    for (SimpleTimeZone stz : stzs) {
        observesDaylight = stz.observesDaylightTime();
        if (observesDaylight != isDaylightTimeAvailable(stz, current)) {
            errln("Fail: [SimpleTimeZone] observesDaylightTime() returned " + observesDaylight + " for " + stz.getID());
        }
    }
}
 
Example 9
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * SimpleTimeZone accepts illegal DST savings values.  These values
 * must be non-zero.  There is no upper limit at this time.
 */
@Test
public void Test4154525() {
    final int GOOD = 1, BAD = 0;
    int[] DATA = {
        1, GOOD,
        0, BAD,
        -1, BAD,
        60*60*1000, GOOD,
        Integer.MIN_VALUE, BAD,
        // Integer.MAX_VALUE, ?, // no upper limit on DST savings at this time
    };
    for (int i=0; i<DATA.length; i+=2) {
        int savings = DATA[i];
        boolean valid = DATA[i+1] == GOOD;
        String method = null;
        for (int j=0; j<2; ++j) {
            try {
                switch (j) {
                case 0:
                    method = "constructor";
                    SimpleTimeZone z = new SimpleTimeZone(0, "id",
                        Calendar.JANUARY, 1, 0, 0,
                        Calendar.MARCH, 1, 0, 0,
                        savings); // <- what we're interested in
                    break;
                case 1:
                    method = "setDSTSavings()";
                    z = new SimpleTimeZone(0, "GMT");
                    z.setDSTSavings(savings);
                    break;
                }
                if (valid) {
                    logln("Pass: DST savings of " + savings + " accepted by " + method);
                } else {
                    errln("Fail: DST savings of " + savings + " accepted by " + method);
                }
            } catch (IllegalArgumentException e) {
                if (valid) {
                    errln("Fail: DST savings of " + savings + " to " + method + " gave " + e);
                } else {
                    logln("Pass: DST savings of " + savings + " to " + method + " gave " + e);
                }
            }
        }
    }
}
 
Example 10
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);
}
 
Example 11
Source File: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test new rule formats.
 */
@Test
public void TestNewRules()
{
    //logln(Locale.getDefault().getDisplayName());
    //logln(TimeZone.getDefault().getID());
    //logln(new Date(0));

    if (true)
    {
        // Doesn't matter what the default TimeZone is here, since we
        // are creating our own TimeZone objects.

        SimpleTimeZone tz;
        java.util.Calendar tempcal = java.util.Calendar.getInstance();
        tempcal.clear();

        logln("-----------------------------------------------------------------");
        logln("Aug 2ndTues .. Mar 15");
        tz = new SimpleTimeZone(-8*ONE_HOUR, "Test_1",
                                Calendar.AUGUST, 2, Calendar.TUESDAY, 2*ONE_HOUR,
                                Calendar.MARCH, 15, 0, 2*ONE_HOUR);
        //logln(tz.toString());
        logln("========================================");
        tempcal.set(1997, 0, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 858416400000L);
        logln("========================================");
        tempcal.set(1997, 6, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 871380000000L);

        logln("-----------------------------------------------------------------");
        logln("Apr Wed>=14 .. Sep Sun<=20");
        tz = new SimpleTimeZone(-8*ONE_HOUR, "Test_2",
                                Calendar.APRIL, 14, -Calendar.WEDNESDAY, 2*ONE_HOUR,
                                Calendar.SEPTEMBER, -20, -Calendar.SUNDAY, 2*ONE_HOUR);
        //logln(tz.toString());
        logln("========================================");
        tempcal.set(1997, 0, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 861184800000L);
        logln("========================================");
        tempcal.set(1997, 6, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 874227600000L);
    }

    /*
      if (true)
      {
      logln("========================================");
      logln("Stepping using millis");
      _testUsingMillis(new Date(97,0,1), false);
      }

      if (true)
      {
      logln("========================================");
      logln("Stepping using fields");
      _testUsingFields(1997, false);
      }

      if (false)
      {
      cal.clear();
      cal.set(1997, 3, 5, 10, 0);
      //    cal.inDaylightTime();
      logln("Date = " + cal.getTime());
      logln("Millis = " + cal.getTime().getTime()/3600000);
      }
      */
}