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

The following examples show how to use java.util.Calendar#roll() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: activiti6-boot2   File: TaskQueryTest.java    License: 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 2
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 3
Source Project: oval   File: PastTest.java    License: 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 4
@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 5
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 6
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 7
Source Project: classchecks   File: DateUtil.java    License: 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 8
/**
 * 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 9
Source Project: Easer   File: Utils.java    License: 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 10
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 11
Source Project: CloudReader   File: TimeUtil.java    License: 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 12
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 13
Source Project: MicroCommunity   File: DateUtil.java    License: 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 14
Source Project: ONE-Unofficial   File: TimeUtil.java    License: 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 15
Source Project: FoxBPM   File: DateCalUtils.java    License: 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 16
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 17
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 18
/**
 * 得到指定月的天数
 */
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 19
private boolean isYesterday(Calendar cal) {
	Calendar yesterday = Calendar.getInstance();
	yesterday.roll(Calendar.DAY_OF_MONTH, -1);
	return isSameDay(cal, yesterday);
}
 
Example 20
Source Project: AsuraFramework   File: DateUtil.java    License: 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());
}