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

The following examples show how to use java.util.Calendar#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: PastTest.java    From oval with Eclipse Public License 2.0 6 votes vote down vote up
public void testPast() {
   final PastCheck isInPast = new PastCheck();
   super.testCheck(isInPast);
   assertTrue(isInPast.isSatisfied(null, null, null, null));

   final Calendar cal = Calendar.getInstance();
   cal.roll(Calendar.YEAR, -1);
   assertTrue(isInPast.isSatisfied(null, cal, null, null));
   assertTrue(isInPast.isSatisfied(null, cal.getTime(), null, null));
   assertTrue(isInPast.isSatisfied(null, DateFormat.getDateTimeInstance().format(cal.getTime()), null, null));

   cal.roll(Calendar.YEAR, 2);
   assertFalse(isInPast.isSatisfied(null, cal, null, null));
   assertFalse(isInPast.isSatisfied(null, cal.getTime(), null, null));
   assertFalse(isInPast.isSatisfied(null, DateFormat.getDateTimeInstance().format(cal.getTime()), null, null));

   assertFalse(isInPast.isSatisfied(null, "bla", null, null));
}
 
Example 2
Source File: TaskQueryTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = { "org/activiti/engine/test/api/task/TaskQueryTest.testProcessDefinition.bpmn20.xml" })
public void testTaskDueDate() throws Exception {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
  Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

  // Set due-date on task
  Date dueDate = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse("01/02/2003 01:12:13");
  task.setDueDate(dueDate);
  taskService.saveTask(task);

  assertEquals(1, taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskDueDate(dueDate).count());

  Calendar otherDate = Calendar.getInstance();
  otherDate.add(Calendar.YEAR, 1);
  assertEquals(0, taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskDueDate(otherDate.getTime()).count());

  Calendar priorDate = Calendar.getInstance();
  priorDate.setTime(dueDate);
  priorDate.roll(Calendar.YEAR, -1);

  assertEquals(1, taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskDueAfter(priorDate.getTime()).count());

  assertEquals(1, taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskDueBefore(otherDate.getTime()).count());
}
 
Example 3
Source File: DatePickerMainDemoFragment.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private void initSettings() {
  today = MaterialDatePicker.todayInUtcMilliseconds();
  Calendar calendar = getClearedUtc();
  calendar.setTimeInMillis(today);
  calendar.roll(Calendar.MONTH, 1);
  nextMonth = calendar.getTimeInMillis();

  calendar.setTimeInMillis(today);
  calendar.set(Calendar.MONTH, Calendar.JANUARY);
  janThisYear = calendar.getTimeInMillis();
  calendar.setTimeInMillis(today);
  calendar.set(Calendar.MONTH, Calendar.DECEMBER);
  decThisYear = calendar.getTimeInMillis();

  calendar.setTimeInMillis(today);
  calendar.roll(Calendar.YEAR, 1);
  oneYearForward = calendar.getTimeInMillis();

  todayPair = new Pair<>(today, today);
  nextMonthPair = new Pair<>(nextMonth, nextMonth);
}
 
Example 4
Source File: DateUtil.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
public static int getCurrentMonthDay() {
    Calendar a = Calendar.getInstance();
    a.set(Calendar.DATE, 1);
    a.roll(Calendar.DATE, -1);
    int maxDate = a.get(Calendar.DATE);
    return maxDate;
}
 
Example 5
Source File: DateUtil.java    From quickhybrid-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 得到指定月的天数
 */
public static int getMonthLastDay(int year, int month) {
    Calendar a = Calendar.getInstance();
    a.set(Calendar.YEAR, year);
    a.set(Calendar.MONTH, month - 1);
    a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
    a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
    int maxDate = a.get(Calendar.DATE);
    return maxDate;
}
 
Example 6
Source File: ChannelFactoryTest.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
public static Channel createTestChannel(Org org, ChannelFamily cfam) throws Exception {
    String label = "channellabel" + TestUtils.randomString().toLowerCase();
    String basedir = "TestChannel basedir";
    String name = "ChannelName" + TestUtils.randomString();
    String summary = "TestChannel summary";
    String description = "TestChannel description";
    Date lastmodified = new Date();
    Date created = new Date();
    Date modified = new Date();
    String gpgurl = "https://gpg.url";
    String gpgid = "B3BCE11D";
    String gpgfp = "AAAA BBBB CCCC DDDD EEEE FFFF 7777 8888 9999 0000";
    Calendar cal = Calendar.getInstance();
    cal.roll(Calendar.DATE, true);
    Date endoflife = new Date(System.currentTimeMillis() + Integer.MAX_VALUE);

    Long testid = new Long(500);
    String query = "ChannelArch.findById";
    ChannelArch arch = (ChannelArch) TestUtils.lookupFromCacheById(testid, query);
    Channel c = new Channel();
    c.setOrg(org);
    c.setLabel(label);
    c.setBaseDir(basedir);
    c.setName(name);
    c.setSummary(summary);
    c.setDescription(description);
    c.setLastModified(lastmodified);
    c.setCreated(created);
    c.setModified(modified);
    c.setGPGKeyUrl(gpgurl);
    c.setGPGKeyId(gpgid);
    c.setGPGKeyFp(gpgfp);
    c.setEndOfLife(endoflife);
    c.setChannelArch(arch);
    c.setChannelFamily(cfam);
    ChannelFactory.save(c);
    return c;
}
 
Example 7
Source File: Bug8152077.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testRoll(int amount, int[] params, int[] sequence, int startIndex) {
    int year = params[0];
    int month = params[1];
    int dayOfMonth = params[2];
    int hourOfDay = sequence[startIndex];
    Calendar cal = new GregorianCalendar(year, month, dayOfMonth,
                                         hourOfDay, 0, 0);
    int ampm = cal.get(AM_PM);

    int length = sequence.length;
    int count = length * 2;
    int field = (length > 12) ? HOUR_OF_DAY : HOUR;

    System.out.printf("roll(%s, %2d) starting from %s%n",
                      (field == HOUR) ? "HOUR" : "HOUR_OF_DAY", amount, cal.getTime());
    for (int i = 0; i < count; i++) {
        int index = (amount > 0) ? (startIndex + i + 1) % length
                                 : Math.floorMod(startIndex - i - 1, length);
        int expected = sequence[index];
        if (field == HOUR) {
            expected %= 12;
        }
        cal.roll(field, amount);
        int value = cal.get(field);
        if (value != expected) {
            System.out.println("Unexpected field value: got=" + value
                                + ", expected=" + expected);
            errors++;
        }
        if (cal.get(DAY_OF_MONTH) != dayOfMonth) {
            System.out.println("DAY_OF_MONTH changed: " + dayOfMonth
                                + " to " + cal.get(DAY_OF_MONTH));
        }
        if (field == HOUR && cal.get(AM_PM) != ampm) {
            System.out.println("AM_PM changed: " + ampm + " to " + cal.get(AM_PM));
            errors++;
        }
    }
}
 
Example 8
Source File: DateCalUtils.java    From FoxBPM with Apache License 2.0 5 votes vote down vote up
/**
 * 得到指定月的天数
 * */
public static int getMonthLastDay(int year, int month)
{
	Calendar a = Calendar.getInstance();
	a.set(Calendar.YEAR, year);
	a.set(Calendar.MONTH, month - 1);
	a.set(Calendar.DATE, 1);//把日期设置为当月第一天
	a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
	int maxDate = a.get(Calendar.DATE);
	return maxDate;
}
 
Example 9
Source File: TimeUtil.java    From ONE-Unofficial with Apache License 2.0 5 votes vote down vote up
/**
 * 将指定日期进行偏移,获取偏移后的日期,eg:2015-09-06
 *
 * @param date   要偏移的日期,需是"2015-09-06"格式
 * @param offset 偏移的值,可正可负
 * @return
 */
public static String getPreviousDate(String date, int offset) {
    try {
        Date d = format_date.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.roll(Calendar.DAY_OF_MONTH, -offset + 1);
        return format_date.format(calendar.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 10
Source File: YamlTesterDateTimeTest.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testDateTimeTag() {
    Calendar cal = getCalendar();
    String time = formatDateTime(cal);
    testdt(time);
    cal = getCalendar();
    cal.roll(Calendar.SECOND, -30);
    time = formatDateTime(cal);
    testdt(time);
    cal = getCalendar();
    cal.roll(Calendar.SECOND, 30);
    time = formatDateTime(cal);
    testdt(time);
}
 
Example 11
Source File: UIFenixCalendar.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void setToFirstDayOfNextMonth(Calendar date) {
    if (date.get(Calendar.MONTH) < 11) {
        date.roll(Calendar.MONTH, true);
    } else {
        date.roll(Calendar.MONTH, true);
        date.roll(Calendar.YEAR, true);
    }
    date.set(Calendar.DAY_OF_MONTH, 1);
}
 
Example 12
Source File: TimeUtil.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当月的 天数
 */
public static int getCurrentMonthDay() {
    Calendar a = Calendar.getInstance();
    a.set(Calendar.DATE, 1);
    a.roll(Calendar.DATE, -1);
    return a.get(Calendar.DATE);
}
 
Example 13
Source File: TestDateAndCalendar.java    From JavaExercises with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        System.out.println("Testing Date\n");
        
        Date today = new Date();
        System.out.println(String.format("%tc", today));
        System.out.println(String.format("%tr", today));
        System.out.println(String.format("%tA %tB %td", today, today, today));
        System.out.println(String.format("%tA %<tB %<td", today));

        System.out.println("\nTesting Calendar\n");
        
        Calendar cal = Calendar.getInstance();
        cal.set(2016, 6, 10, 13, 12);
        long day1 = cal.getTimeInMillis();
        day1 += 1000 * 60 * 60;
        cal.setTimeInMillis(day1);
        System.out.println("new hour "+ cal.get(cal.HOUR_OF_DAY));
        
        cal.add(cal.DATE, 35) ;
        System.out.println("add 35 days " + cal.getTime());

        cal.roll(cal.DATE, 35);
        System.out.println("roll 35 days" + cal.getTime());
        
        cal.set(cal.DATE, 1);
        System.out.println("set to 1 " + cal.getTime());

    }
 
Example 14
Source File: Utils.java    From Easer with GNU General Public License v3.0 5 votes vote down vote up
static void scheduleAlarmEveryday(AlarmManager alarmManager, PendingIntent pendingIntent) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0); // 00:00
    calendar.roll(Calendar.DAY_OF_YEAR, 1); // "Tomorrow" (with extra care from the next code block)
    if (calendar.get(Calendar.DAY_OF_YEAR) == 0) // First day of year, but year is not rolled
        calendar.roll(Calendar.YEAR, 1);
    // calendar == Tomorrow 00:00
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingIntent);
}
 
Example 15
Source File: ProjectCalendar.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method calculates the absolute number of days between two dates.
 * Note that where two date objects are provided that fall on the same
 * day, this method will return one not zero. Note also that this method
 * assumes that the dates are passed in the correct order, i.e.
 * startDate < endDate.
 *
 * @param startDate Start date
 * @param endDate End date
 * @return number of days in the date range
 */
private int getDaysInRange(Date startDate, Date endDate)
{
   int result;
   Calendar cal = DateHelper.popCalendar(endDate);
   int endDateYear = cal.get(Calendar.YEAR);
   int endDateDayOfYear = cal.get(Calendar.DAY_OF_YEAR);

   cal.setTime(startDate);

   if (endDateYear == cal.get(Calendar.YEAR))
   {
      result = (endDateDayOfYear - cal.get(Calendar.DAY_OF_YEAR)) + 1;
   }
   else
   {
      result = 0;
      do
      {
         result += (cal.getActualMaximum(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR)) + 1;
         cal.roll(Calendar.YEAR, 1);
         cal.set(Calendar.DAY_OF_YEAR, 1);
      }
      while (cal.get(Calendar.YEAR) < endDateYear);
      result += endDateDayOfYear;
   }
   DateHelper.pushCalendar(cal);

   return result;
}
 
Example 16
Source File: DateUtil.java    From classchecks with Apache License 2.0 5 votes vote down vote up
public static Date getLastDayOfMonth(Integer year, Integer month) {
	Calendar calendar = Calendar.getInstance();
	if (year == null) {
		year = calendar.get(Calendar.YEAR);
	}
	if (month == null) {
		month = calendar.get(Calendar.MONTH);
	}
	calendar.set(year, month, 1);
	calendar.roll(Calendar.DATE, -1);
	return calendar.getTime();
}
 
Example 17
Source File: ChannelFactoryTest.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
public static Channel createTestChannel(Org org, ChannelArch arch, ChannelFamily cfam) throws Exception {
    String label = "channellabel" + TestUtils.randomString().toLowerCase();
    String basedir = "TestChannel basedir";
    String name = "ChannelName" + TestUtils.randomString();
    String summary = "TestChannel summary";
    String description = "TestChannel description";
    Date lastmodified = new Date();
    Date created = new Date();
    Date modified = new Date();
    String gpgurl = "https://gpg.url";
    String gpgid = "B3BCE11D";
    String gpgfp = "AAAA BBBB CCCC DDDD EEEE FFFF 7777 8888 9999 0000";
    Calendar cal = Calendar.getInstance();
    cal.roll(Calendar.DATE, true);
    Date endoflife = new Date(System.currentTimeMillis() + Integer.MAX_VALUE);

    Channel c = new Channel();
    c.setOrg(org);
    c.setLabel(label);
    c.setBaseDir(basedir);
    c.setName(name);
    c.setSummary(summary);
    c.setDescription(description);
    c.setLastModified(lastmodified);
    c.setCreated(created);
    c.setModified(modified);
    c.setGPGKeyUrl(gpgurl);
    c.setGPGKeyId(gpgid);
    c.setGPGKeyFp(gpgfp);
    c.setEndOfLife(endoflife);
    c.setChannelArch(arch);
    c.setChannelFamily(cfam);
    ChannelFactory.save(c);
    return c;
}
 
Example 18
Source File: SystemOverviewActionTest.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
public void testSystemActive() throws Exception {
    Calendar pcal = Calendar.getInstance();
    pcal.setTime(new Timestamp(System.currentTimeMillis()));
    pcal.roll(Calendar.MINUTE, -5);

    s.getServerInfo().setCheckin(pcal.getTime());
    TestUtils.saveAndFlush(s);
    actionPerform();
    assertEquals(request.getAttribute("systemInactive"), Boolean.FALSE);
}
 
Example 19
Source File: HistoryListFragment.java    From Linphone4Android with GNU General Public License v3.0 4 votes vote down vote up
private boolean isYesterday(Calendar cal) {
	Calendar yesterday = Calendar.getInstance();
	yesterday.roll(Calendar.DAY_OF_MONTH, -1);
	return isSameDay(cal, yesterday);
}
 
Example 20
Source File: DateUtil.java    From AsuraFramework with Apache License 2.0 3 votes vote down vote up
/**
 * 
 * 获取指定日期所在月的最后一天
 *
 * @author zhangshaobin
 * @created 2012-11-5 下午7:04:10
 *
 * @param date
 * @return	以"年年年年-月月-日日"格式返回当指定月最后一天的日期
 */
public static String getLastDayOfMonth(final Date date) {
	final Calendar ca = Calendar.getInstance();
	ca.setTime(date);
	ca.set(Calendar.DAY_OF_MONTH, 1);
	ca.roll(Calendar.DAY_OF_MONTH, -1);
	//		return dateFormat.format(ca.getTime());
	return getDateFormat().format(ca.getTime());
}