下面列出了java.util.GregorianCalendar#roll ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
/**
* 获取任意两个日期间的天数
* @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;
}
@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));
}
@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);
}
@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);
}
@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));
}
public Date rollAdd(GregorianCalendar calendar, int amount) {
calendar.roll(GregorianCalendar.MONTH, amount);
return calendar.getTime();
}
/**
* 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;
}