Java Code Examples for java.util.GregorianCalendar#roll()

The following examples show how to use java.util.GregorianCalendar#roll() . 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: DoNotDisturbTile.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
private Uri getTimeUntilNextAlarmCondition() {
    try {
        GregorianCalendar weekRange = new GregorianCalendar();
        final long now = weekRange.getTimeInMillis();
        setToMidnight(weekRange);
        weekRange.roll(Calendar.DATE, 6);
        final long nextAlarmMs = (long) XposedHelpers.callMethod(mZenCtrl, "getNextAlarm");
        if (nextAlarmMs > 0) {
            GregorianCalendar nextAlarm = new GregorianCalendar();
            nextAlarm.setTimeInMillis(nextAlarmMs);
            setToMidnight(nextAlarm);
            if (weekRange.compareTo(nextAlarm) >= 0) {
                Object condition = XposedHelpers.callStaticMethod(getZenModeConfigClass(), "toNextAlarmCondition",
                        mContext, now, nextAlarmMs, SysUiManagers.KeyguardMonitor.getCurrentUserId());
                return (Uri) XposedHelpers.getObjectField(condition, "id");
            }
        }
        return null;
    } catch (Throwable t) {
        XposedBridge.log(t);
        return null;
    }
}
 
Example 2
Source File: DateUtils.java    From wasindoor with Apache License 2.0 5 votes vote down vote up
/**
 * 获取任意两个日期间的天数
 * @param tStartDate 起始日期,
 * @param tEndDate  结束日期
 * @return 天数
 */
public static int getDayCount(Date tStartDate, Date tEndDate) {
    int iRetVal = 0;
    GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.
                                 getInstance();
    calendar.setTime(tStartDate);
    GregorianCalendar calendar2 = (GregorianCalendar) GregorianCalendar.
                                  getInstance();
    calendar2.setTime(tEndDate);
    int iMaxDays = 0;

    while (calendar.before(calendar2)) {
        if (calendar.isLeapYear(calendar.get(GregorianCalendar.YEAR))) {
            iMaxDays = 366;
        } else {
            iMaxDays = 365;
        }
        ++iRetVal;
        calendar.roll(GregorianCalendar.DAY_OF_YEAR, true);

        if (calendar.get(GregorianCalendar.DAY_OF_YEAR) == iMaxDays) {
            calendar.roll(GregorianCalendar.YEAR, 1);
            calendar.set(GregorianCalendar.MONTH, GregorianCalendar.JANUARY);
            calendar.set(GregorianCalendar.DAY_OF_MONTH, 1);
        }
    }
    return iRetVal;
}
 
Example 3
Source File: GregorianCalendarTester.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void test_rollAdd() {
    GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
    GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
    GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
    calendarExpected.roll(Calendar.MONTH, 8);
    Date expectedDate = calendarExpected.getTime();
    assertEquals(expectedDate, calendarDemo.rollAdd(calendarActual, 8));
}
 
Example 4
Source File: GregorianCalendarTester.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void test_whenRollUpOneMonth_thenYearIsUnchanged() {
    final int rolledUpMonthJuly = 7, orginalYear2018 = 2018;
    GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
    calendarExpected.roll(Calendar.MONTH, 1);
    assertEquals(calendarExpected.get(Calendar.MONTH), rolledUpMonthJuly);
    assertEquals(calendarExpected.get(Calendar.YEAR), orginalYear2018);
}
 
Example 5
Source File: GregorianCalendarTester.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void test_whenRollDownOneMonth_thenYearIsUnchanged() {
    final int rolledDownMonthJune = 5, orginalYear2018 = 2018;
    GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
    calendarExpected.roll(Calendar.MONTH, -1);
    assertEquals(calendarExpected.get(Calendar.MONTH), rolledDownMonthJune);
    assertEquals(calendarExpected.get(Calendar.YEAR), orginalYear2018);
}
 
Example 6
Source File: GregorianCalendarTester.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void test_rollSubtract() {
    GregorianCalendarExample calendarDemo = new GregorianCalendarExample();
    GregorianCalendar calendarActual = new GregorianCalendar(2018, 6, 28);
    GregorianCalendar calendarExpected = new GregorianCalendar(2018, 6, 28);
    calendarExpected.roll(Calendar.MONTH, -8);
    Date expectedDate = calendarExpected.getTime();
    assertEquals(expectedDate, calendarDemo.rollAdd(calendarActual, -8));
}
 
Example 7
Source File: GregorianCalendarExample.java    From tutorials with MIT License 4 votes vote down vote up
public Date rollAdd(GregorianCalendar calendar, int amount) {
    calendar.roll(GregorianCalendar.MONTH, amount);
    return calendar.getTime();
}
 
Example 8
Source File: Schedule.java    From fosstrak-epcis with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Calculates the next scheduled time after the given time. Algorithm idea:<br> -
 * start with biggest time unit (i.e. year) of the given time <br> - if the
 * time unit is valid (e.g. the time unit matches the <br>
 * scheduled time, this is implicitly true if the time in the <br>
 * schedule was omitted) *and* there exists a valid smaller time <br>
 * unit, *then* return this time unit <br> - do this recursively for all
 * time units <br> - month needs to be special cased because of dayOfWeek
 * 
 * @param time
 *            Time after which next scheduled time should be returned.
 * @return The next scheduled time after 'time'.
 * @throws ImplementationException
 *             Almost any kind of error.
 */
public GregorianCalendar nextScheduledTime(final GregorianCalendar time) throws ImplementationExceptionResponse {
    GregorianCalendar nextSchedule = (GregorianCalendar) time.clone();
    // look at year
    while (!monthMadeValid(nextSchedule)) {
        nextSchedule.roll(YEAR, true);
        setFieldsToMinimum(nextSchedule, MONTH);

    }
    return nextSchedule;
}