Example usage for java.util Calendar clear

List of usage examples for java.util Calendar clear

Introduction

In this page you can find the example usage for java.util Calendar clear.

Prototype

public final void clear(int field) 

Source Link

Document

Sets the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined.

Usage

From source file:org.openmrs.module.rwandasphstudyreports.api.impl.CDCReportsServiceImpl.java

private Calendar todayMidNight() {
    Calendar today = Calendar.getInstance(Context.getLocale());

    today.set(Calendar.HOUR_OF_DAY, 0);
    today.clear(Calendar.MINUTE);
    today.clear(Calendar.SECOND);
    today.clear(Calendar.MILLISECOND);
    return today;
}

From source file:cn.mljia.common.notify.utils.DateUtils.java

/**
 * ???/* ww w  . j a v  a  2s.co  m*/
 * 
 * @param date
 *            1
 * @param otherDate
 *            2
 * @param withUnit
 *            ??Calendar field?
 * @return 0, 0 ??0
 */
public static int compareDate(Date date, Date otherDate, int withUnit) {
    Calendar dateCal = Calendar.getInstance();
    dateCal.setTime(date);
    Calendar otherDateCal = Calendar.getInstance();
    otherDateCal.setTime(otherDate);

    switch (withUnit) {
    case Calendar.YEAR:
        dateCal.clear(Calendar.MONTH);
        otherDateCal.clear(Calendar.MONTH);
    case Calendar.MONTH:
        dateCal.set(Calendar.DATE, 1);
        otherDateCal.set(Calendar.DATE, 1);
    case Calendar.DATE:
        dateCal.set(Calendar.HOUR_OF_DAY, 0);
        otherDateCal.set(Calendar.HOUR_OF_DAY, 0);
    case Calendar.HOUR:
        dateCal.clear(Calendar.MINUTE);
        otherDateCal.clear(Calendar.MINUTE);
    case Calendar.MINUTE:
        dateCal.clear(Calendar.SECOND);
        otherDateCal.clear(Calendar.SECOND);
    case Calendar.SECOND:
        dateCal.clear(Calendar.MILLISECOND);
        otherDateCal.clear(Calendar.MILLISECOND);
    case Calendar.MILLISECOND:
        break;
    default:
        throw new IllegalArgumentException("withUnit ?? " + withUnit + " ????");
    }
    return dateCal.compareTo(otherDateCal);
}

From source file:is.idega.idegaweb.egov.bpm.scheduler.SchedulerTest.java

@Test
public void testTimerELPlusCreation() {
    ProcessDefinition processDefinition = ProcessDefinition
            .parseXmlString("<process-definition>" + "  <start-state>" + "    <transition to='get old' />"
                    + "  </start-state>" + "  <state name='get old'>" + "    <timer name='pension' "
                    + "           duedate='#{dateOfBirth} + 65 years' "
                    + "           transition='time-out-transition' >"
                    + "      <action class='the-remainder-action-class-name' />" + "    </timer>" + "  </state>"
                    + "</process-definition>");

    JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
    try {//  w w  w  . jav  a 2s . c  om
        TestSchedulerService testSchedulerService = (TestSchedulerService) jbpmContext.getServices()
                .getSchedulerService();

        ProcessInstance processInstance = new ProcessInstance(processDefinition);

        Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.set(1971, 1, 12, 2, 10, 0);
        dateOfBirth.clear(Calendar.MILLISECOND);
        processInstance.getContextInstance().setVariable("dateOfBirth", dateOfBirth.getTime());

        processInstance.signal();

        assertEquals(1, testSchedulerService.createdTimers.size());
        Timer scheduledTimer = (Timer) testSchedulerService.createdTimers.get(0);
        assertEquals("pension", scheduledTimer.getName());
        assertEquals(processDefinition.getNode("get old"), scheduledTimer.getGraphElement());
        assertNotNull(scheduledTimer.getDueDate());

        Calendar dateOfPension = Calendar.getInstance();
        dateOfPension.set(2036, 1, 12, 2, 10, 0);
        dateOfPension.clear(Calendar.MILLISECOND);

        assertEquals(dateOfPension.getTime(), scheduledTimer.getDueDate());
        assertEquals("the-remainder-action-class-name",
                scheduledTimer.getAction().getActionDelegation().getClassName());
        assertSame(processInstance.getRootToken(), scheduledTimer.getToken());
        assertEquals("time-out-transition", scheduledTimer.getTransitionName());
    } finally {
        jbpmContext.close();
    }
}

From source file:is.idega.idegaweb.egov.bpm.scheduler.SchedulerTest.java

@Test
public void testTimerELCreation() {
    ProcessDefinition processDefinition = ProcessDefinition
            .parseXmlString("<process-definition>" + "  <start-state>" + "    <transition to='get old' />"
                    + "  </start-state>" + "  <state name='get old'>" + "    <timer name='pension' "
                    + "           duedate='#{dateOfPension}' " + "           transition='time-out-transition' >"
                    + "      <action class='the-remainder-action-class-name' />" + "    </timer>" + "  </state>"
                    + "</process-definition>");

    JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
    try {/*from w ww.  j  a v a  2s . c  o  m*/
        TestSchedulerService testSchedulerService = (TestSchedulerService) jbpmContext.getServices()
                .getSchedulerService();

        ProcessInstance processInstance = new ProcessInstance(processDefinition);

        Calendar dateOfPension = Calendar.getInstance();
        dateOfPension.set(2036, 1, 12, 2, 10, 0);
        dateOfPension.clear(Calendar.MILLISECOND);
        processInstance.getContextInstance().setVariable("dateOfPension", dateOfPension.getTime());

        processInstance.signal();

        assertEquals(1, testSchedulerService.createdTimers.size());
        Timer scheduledTimer = (Timer) testSchedulerService.createdTimers.get(0);
        assertEquals("pension", scheduledTimer.getName());
        assertEquals(processDefinition.getNode("get old"), scheduledTimer.getGraphElement());

        Calendar dateOfPensionTest = Calendar.getInstance();
        dateOfPensionTest.clear(Calendar.MILLISECOND);
        dateOfPensionTest.set(2036, 1, 12, 2, 10, 0);

        assertEquals(dateOfPensionTest.getTime(), scheduledTimer.getDueDate());
        assertNotNull(scheduledTimer.getDueDate());
        assertEquals("the-remainder-action-class-name",
                scheduledTimer.getAction().getActionDelegation().getClassName());
        assertSame(processInstance.getRootToken(), scheduledTimer.getToken());
        assertEquals("time-out-transition", scheduledTimer.getTransitionName());
    } finally {
        jbpmContext.close();
    }
}

From source file:is.idega.idegaweb.egov.bpm.scheduler.SchedulerTest.java

@Test
public void testTimerELCalendarCreation() {
    ProcessDefinition processDefinition = ProcessDefinition
            .parseXmlString("<process-definition>" + "  <start-state>" + "    <transition to='get old' />"
                    + "  </start-state>" + "  <state name='get old'>" + "    <timer name='pension' "
                    + "           duedate='#{dateOfPension}' " + "           transition='time-out-transition' >"
                    + "      <action class='the-remainder-action-class-name' />" + "    </timer>" + "  </state>"
                    + "</process-definition>");

    JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
    try {/*from  w  w  w.j a v  a 2 s .  com*/
        TestSchedulerService testSchedulerService = (TestSchedulerService) jbpmContext.getServices()
                .getSchedulerService();

        ProcessInstance processInstance = new ProcessInstance(processDefinition);

        Calendar dateOfPension = Calendar.getInstance();
        dateOfPension.set(2036, 1, 12, 2, 10, 0);
        dateOfPension.clear(Calendar.MILLISECOND);
        processInstance.getContextInstance().setVariable("dateOfPension", dateOfPension);

        processInstance.signal();

        assertEquals(1, testSchedulerService.createdTimers.size());
        Timer scheduledTimer = (Timer) testSchedulerService.createdTimers.get(0);
        assertEquals("pension", scheduledTimer.getName());
        assertEquals(processDefinition.getNode("get old"), scheduledTimer.getGraphElement());

        Calendar dateOfPensionTest = Calendar.getInstance();
        dateOfPensionTest.clear(Calendar.MILLISECOND);
        dateOfPensionTest.set(2036, 1, 12, 2, 10, 0);

        assertEquals(dateOfPensionTest.getTime(), scheduledTimer.getDueDate());
        assertNotNull(scheduledTimer.getDueDate());
        assertEquals("the-remainder-action-class-name",
                scheduledTimer.getAction().getActionDelegation().getClassName());
        assertSame(processInstance.getRootToken(), scheduledTimer.getToken());
        assertEquals("time-out-transition", scheduledTimer.getTransitionName());
    } finally {
        jbpmContext.close();
    }
}

From source file:is.idega.idegaweb.egov.bpm.scheduler.SchedulerTest.java

@Test
public void testTimerELMinusCreation() {
    ProcessDefinition processDefinition = ProcessDefinition
            .parseXmlString("<process-definition>" + "  <start-state>" + "    <transition to='get old' />"
                    + "  </start-state>" + "  <state name='get old'>" + "    <timer name='pensionReminder' "
                    + "           duedate='#{dateOfPension} - 1 year' "
                    + "           transition='time-out-transition' >"
                    + "      <action class='the-remainder-action-class-name' />" + "    </timer>" + "  </state>"
                    + "</process-definition>");

    JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
    try {//from w  w  w .  jav a2s .co  m
        TestSchedulerService testSchedulerService = (TestSchedulerService) jbpmContext.getServices()
                .getSchedulerService();

        ProcessInstance processInstance = new ProcessInstance(processDefinition);

        Calendar dateOfPension = Calendar.getInstance();
        dateOfPension.set(2036, 1, 12, 2, 10, 0);
        dateOfPension.clear(Calendar.MILLISECOND);
        processInstance.getContextInstance().setVariable("dateOfPension", dateOfPension.getTime());

        processInstance.signal();

        assertEquals(1, testSchedulerService.createdTimers.size());
        Timer scheduledTimer = (Timer) testSchedulerService.createdTimers.get(0);
        assertEquals("pensionReminder", scheduledTimer.getName());
        assertEquals(processDefinition.getNode("get old"), scheduledTimer.getGraphElement());
        assertNotNull(scheduledTimer.getDueDate());

        Calendar dateOfPensionReminder = Calendar.getInstance();
        dateOfPensionReminder.set(2035, 1, 12, 2, 10, 0);
        dateOfPensionReminder.clear(Calendar.MILLISECOND);

        assertEquals(dateOfPensionReminder.getTime(), scheduledTimer.getDueDate());
        assertEquals("the-remainder-action-class-name",
                scheduledTimer.getAction().getActionDelegation().getClassName());
        assertSame(processInstance.getRootToken(), scheduledTimer.getToken());
        assertEquals("time-out-transition", scheduledTimer.getTransitionName());
    } finally {
        jbpmContext.close();
    }
}

From source file:com.appeligo.alerts.KeywordAlertChecker.java

/**
 * @param timeZone the user's timezone/*w  ww . ja  v a  2  s. c om*/
 * @param startOfDay We're using the value of earliestSmsTime (user account setting) as a starting point,
 * so anything before this time is credited to the previous day)
 * @return 12am (midnight) using system time (not user time) because "Date" objects stored in SQL
 * come back in system time.
 */
private Date calculateDay(TimeZone timeZone, Time startOfDay) {
    Calendar cal = Calendar.getInstance(timeZone);
    Calendar start = Calendar.getInstance();
    start.setTimeInMillis(startOfDay.getTime());
    cal.add(Calendar.HOUR_OF_DAY, 0 - start.get(Calendar.HOUR_OF_DAY));
    cal.add(Calendar.MINUTE, 0 - start.get(Calendar.MINUTE));
    cal.add(Calendar.SECOND, 0 - start.get(Calendar.SECOND));
    cal.clear(Calendar.MILLISECOND);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.HOUR_OF_DAY);
    cal.clear(Calendar.HOUR);
    cal.clear(Calendar.AM_PM);
    TimeZone systemTimeZone = TimeZone.getDefault();
    long now = System.currentTimeMillis();
    long difference = systemTimeZone.getOffset(now) - timeZone.getOffset(now);
    return new Date(cal.getTimeInMillis() - difference);
}

From source file:figs.treeVisualization.gui.PhyloDateAxis.java

/**
 * Returns the previous "standard" date, for a given date and tick unit.
 *
 * @param date  the reference date.//from  w w w .j  av  a  2 s . co  m
 * @param unit  the tick unit.
 *
 * @return The previous "standard" date.
 */
@Override
protected Date previousStandardDate(Date date, DateTickUnit unit) {

    int hours;
    int days;
    int months;
    int years;

    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    calendar.setTime(getMinimumDate());
    int current = calendar.get(unit.getCalendarField());

    // We only care about DAY, MONTH, YEAR
    DateTickMarkPosition tickMarkPosition = this.getTickMarkPosition();
    switch (unit.getUnit()) {
    case (DateTickUnit.DAY):
        years = calendar.get(Calendar.YEAR);
        months = calendar.get(Calendar.MONTH);

        if (tickMarkPosition == DateTickMarkPosition.START) {
            hours = 0;
        } else if (tickMarkPosition == DateTickMarkPosition.MIDDLE) {
            hours = 12;
        } else {
            hours = 23;
        }
        calendar.clear(Calendar.MILLISECOND);
        calendar.set(years, months, current, hours, 0, 0);

        long result = calendar.getTime().getTime();
        if (result > date.getTime()) {
            // move it back a day
            calendar.set(years, months, current - 1, hours, 0, 0);
        }
        return calendar.getTime();
    case (DateTickUnit.MONTH):
        years = calendar.get(Calendar.YEAR);
        calendar.clear(Calendar.MILLISECOND);
        calendar.set(years, current, 1, 0, 0, 0);
        // TODO:
        /*
        Month month = new Month(calendar.getTime());
        Date standardDate = calculateDateForPosition(
            month, tickMarkPosition
        );
        long millis = standardDate.getTime();
        if (millis > date.getTime()) {
            month = (Month) month.previous();
            standardDate = calculateDateForPosition(
                month, tickMarkPosition
            );
        }
        return standardDate;
        */
        return calendar.getTime();
    case (DateTickUnit.YEAR):
        if (tickMarkPosition == DateTickMarkPosition.START) {
            months = 0;
            days = 1;
        } else if (tickMarkPosition == DateTickMarkPosition.MIDDLE) {
            months = 6;
            days = 1;
        } else {
            months = 11;
            days = 31;
        }
        calendar.clear(Calendar.MILLISECOND);
        calendar.set(current, months, days, 0, 0, 0);
        return calendar.getTime();
    default:
        return calendar.getTime();
    }

}

From source file:org.kuali.kra.s2s.generator.impl.PHS398FellowshipSupplementalV1_1Generator.java

private BudgetDecimal getNumberOfMonths(Date dateStart, Date dateEnd) {
    BudgetDecimal monthCount = BudgetDecimal.ZERO;
    int fullMonthCount = 0;

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    startDate.setTime(dateStart);/*from  w  w  w  .j a  va  2 s.c o m*/
    endDate.setTime(dateEnd);

    startDate.clear(Calendar.HOUR);
    startDate.clear(Calendar.MINUTE);
    startDate.clear(Calendar.SECOND);
    startDate.clear(Calendar.MILLISECOND);

    endDate.clear(Calendar.HOUR);
    endDate.clear(Calendar.MINUTE);
    endDate.clear(Calendar.SECOND);
    endDate.clear(Calendar.MILLISECOND);

    if (startDate.after(endDate)) {
        return BudgetDecimal.ZERO;
    }
    int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
    startMonthDays++;
    int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
    BudgetDecimal startMonthFraction = new BudgetDecimal(startMonthDays)
            .divide(new BudgetDecimal(startMonthMaxDays));

    int endMonthDays = endDate.get(Calendar.DATE);
    int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE);

    BudgetDecimal endMonthFraction = new BudgetDecimal(endMonthDays).divide(new BudgetDecimal(endMonthMaxDays));

    startDate.set(Calendar.DATE, 1);
    endDate.set(Calendar.DATE, 1);

    while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
        startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1);
        fullMonthCount++;
    }
    fullMonthCount = fullMonthCount - 1;
    monthCount = monthCount.add(new BudgetDecimal(fullMonthCount)).add(startMonthFraction)
            .add(endMonthFraction);
    return monthCount;
}

From source file:org.kuali.coeus.s2sgen.impl.generate.support.PHS398FellowshipSupplementalV1_1Generator.java

private ScaleTwoDecimal getNumberOfMonths(Date dateStart, Date dateEnd) {
    BigDecimal monthCount = ScaleTwoDecimal.ZERO.bigDecimalValue();
    int fullMonthCount = 0;

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    startDate.setTime(dateStart);/*from w w  w  .j a v a 2 s .co m*/
    endDate.setTime(dateEnd);

    startDate.clear(Calendar.HOUR);
    startDate.clear(Calendar.MINUTE);
    startDate.clear(Calendar.SECOND);
    startDate.clear(Calendar.MILLISECOND);

    endDate.clear(Calendar.HOUR);
    endDate.clear(Calendar.MINUTE);
    endDate.clear(Calendar.SECOND);
    endDate.clear(Calendar.MILLISECOND);

    if (startDate.after(endDate)) {
        return ScaleTwoDecimal.ZERO;
    }
    int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
    startMonthDays++;
    int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
    BigDecimal startMonthFraction = new ScaleTwoDecimal(startMonthDays).bigDecimalValue()
            .divide(new ScaleTwoDecimal(startMonthMaxDays).bigDecimalValue(), RoundingMode.HALF_UP);

    int endMonthDays = endDate.get(Calendar.DATE);
    int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE);

    BigDecimal endMonthFraction = new ScaleTwoDecimal(endMonthDays).bigDecimalValue()
            .divide(new ScaleTwoDecimal(endMonthMaxDays).bigDecimalValue(), RoundingMode.HALF_UP);

    startDate.set(Calendar.DATE, 1);
    endDate.set(Calendar.DATE, 1);

    while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
        startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1);
        fullMonthCount++;
    }
    fullMonthCount = fullMonthCount - 1;
    monthCount = monthCount.add(new ScaleTwoDecimal(fullMonthCount).bigDecimalValue()).add(startMonthFraction)
            .add(endMonthFraction);
    return new ScaleTwoDecimal(monthCount);
}