Example usage for java.util Calendar before

List of usage examples for java.util Calendar before

Introduction

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

Prototype

public boolean before(Object when) 

Source Link

Document

Returns whether this Calendar represents a time before the time represented by the specified Object.

Usage

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.DateUtils.java

/**
 * Return <code>true</code> if the date represented by the passed parameters is strictly before the given upper limit,
 * <code>false</code> otherwise.
 *
 * Note: All parameters are 1-based meaning 1 for Jan, 2 for Feb etc unlike Java Calendar API.
 *
 * @param day date value for the date/*from w  w  w .j  av a 2s.  com*/
 * @param month month value for the date
 * @param year year value for the date
 * @param strictUpperLimit the strict upper limit for the given date
 * @return <code>true</code> if the date represented by the passed parameters is strictly before the given upper limit,
 * <code>false</code> otherwise
 */
public static boolean isDateStrictlyBeforeGivenTime(final String day, final String month, final String year,
        final Calendar strictUpperLimit) {

    boolean result = true;

    final Calendar calendarFromDate = Calendar.getInstance();
    calendarFromDate.setLenient(false);
    calendarFromDate.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day), 0, 0, 0);
    calendarFromDate.set(Calendar.MILLISECOND, 0);

    if (!calendarFromDate.before(strictUpperLimit)) {
        result = false;
    }

    return result;
}

From source file:com.augmentum.common.util.DateUtil.java

public static int getDaysBetween(Date startDate, Date endDate, TimeZone timeZone) {

    Calendar startCal = new GregorianCalendar(timeZone);
    startCal.setTime(DateUtils.truncate(startDate, Calendar.DAY_OF_MONTH));

    Calendar endCal = new GregorianCalendar(timeZone);
    endCal.setTime(DateUtils.truncate(endDate, Calendar.DAY_OF_MONTH));

    int daysBetween = 0;
    while (startCal.before(endCal)) {
        daysBetween++;/*  ww w.j av a  2 s.  c o m*/

        startCal.add(Calendar.DAY_OF_MONTH, 1);
    }

    return daysBetween;
}

From source file:net.qvex.dommel.data.DommelDataService.java

public static int daysBetween(Calendar startCal, Calendar endCal) {

    Calendar date = (Calendar) startCal.clone();
    int daysBetween = 0;
    while (date.before(endCal)) {
        date.add(Calendar.DAY_OF_MONTH, 1);
        daysBetween++;/* w  w  w.  j  a va 2 s .c  o  m*/
    }
    return daysBetween;
}

From source file:org.apache.synapse.eventing.builders.SubscriptionMessageBuilder.java

/**
 * (01) <s12:Envelope//  w  ww.ja  v  a 2 s .  c  o  m
 * (02)     xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
 * (03)     xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
 * (04)     xmlns:wse="http://schemas.xmlsoap.org/ws/2004/08/eventing"
 * (05)     xmlns:ew="http://www.example.com/warnings" >
 * (06)   <s12:Header>
 * (07)     <wsa:Action>
 * (08)       http://schemas.xmlsoap.org/ws/2004/08/eventing/Subscribe
 * (09)     </wsa:Action>
 * (10)     <wsa:MessageID>
 * (11)       uuid:e1886c5c-5e86-48d1-8c77-fc1c28d47180
 * (12)     </wsa:MessageID>
 * (13)     <wsa:ReplyTo>
 * (14)      <wsa:Address>http://www.example.com/MyEvEntsink</wsa:Address>
 * (15)      <wsa:ReferenceProperties>
 * (16)        <ew:MySubscription>2597</ew:MySubscription>
 * (17)      </wsa:ReferenceProperties>
 * (18)     </wsa:ReplyTo>
 * (19)     <wsa:To>http://www.example.org/oceanwatch/EventSource</wsa:To>
 * (20)   </s12:Header>
 * (21)   <s12:Body>
 * (22)     <wse:Subscribe>
 * (23)       <wse:EndTo>
 * (24)         <wsa:Address>
 * (25)           http://www.example.com/MyEventSink
 * (26)         </wsa:Address>
 * (27)         <wsa:ReferenceProperties>
 * (28)           <ew:MySubscription>2597</ew:MySubscription>
 * (29)         </wsa:ReferenceProperties>
 * (30)       </wse:EndTo>
 * (31)       <wse:Delivery>
 * (32)         <wse:NotifyTo>
 * (33)           <wsa:Address>
 * (34)             http://www.other.example.com/OnStormWarning
 * (35)           </wsa:Address>
 * (36)           <wsa:ReferenceProperties>
 * (37)             <ew:MySubscription>2597</ew:MySubscription>
 * (38)           </wsa:ReferenceProperties>
 * (39)         </wse:NotifyTo>
 * (40)       </wse:Delivery>
 * (41)       <wse:Expires>2004-06-26T21:07:00.000-08:00</wse:Expires>
 * (42)       <wse:Filter xmlns:ow="http://www.example.org/oceanwatch"
 * (43)           Dialect="http://www.example.org/topicFilter" >
 * (44)         weather.storms
 * (45)       </wse:Filter>
 * (46)     </wse:Subscribe>
 * (47)   </s12:Body>
 * (48) </s12:Envelope>
 *
 * @param mc The MessageContext from which to create the SynapseSubscription
 * @return The SynapseSubscription
 */
public static SynapseSubscription createSubscription(MessageContext mc) {
    SynapseSubscription subscription = null;
    OMElement notifyToElem;
    OMElement elem = mc.getEnvelope().getBody().getFirstChildWithName(SUBSCRIBE_QNAME);
    if (elem != null) {
        OMElement deliveryElem = elem.getFirstChildWithName(DELIVERY_QNAME);
        if (deliveryElem != null) {
            notifyToElem = deliveryElem.getFirstChildWithName(NOTIFY_TO_QNAME);
            if (notifyToElem != null) {
                subscription = new SynapseSubscription(EventingConstants.WSE_DEFAULT_DELIVERY_MODE);
                subscription.setAddressUrl(notifyToElem.getFirstElement().getText());
                subscription.setEndpointUrl(notifyToElem.getFirstElement().getText());
                subscription.setSubManUrl(mc.getTo().getAddress());

            } else {
                handleException("NotifyTo element not found in the subscription message");
            }
        } else {
            handleException("Delivery element is not found in the subscription message");
        }

        OMElement filterElem = elem.getFirstChildWithName(FILTER_QNAME);
        if (subscription != null && filterElem != null) {
            OMAttribute dialectAttr = filterElem.getAttribute(ATT_DIALECT);
            if (dialectAttr != null && dialectAttr.getAttributeValue() != null) {
                subscription.setFilterDialect(dialectAttr.getAttributeValue());
                subscription.setFilterValue(filterElem.getText());
            } else {
                handleException("Error in creating subscription. Filter dialect not defined");
            }
        }
        OMElement expiryElem = elem.getFirstChildWithName(EXPIRES);
        if (expiryElem != null) {
            Calendar calendarExpires = null;
            try {
                if (expiryElem.getText().startsWith("P")) {
                    calendarExpires = ConverterUtil.convertToDuration(expiryElem.getText()).getAsCalendar();
                } else {
                    calendarExpires = ConverterUtil.convertToDateTime(expiryElem.getText());
                }
            } catch (Exception e) {
                log.error("Error converting the expiration date ," + e.toString());
                setExpirationFault(subscription);
            }
            Calendar calendarNow = Calendar.getInstance();
            if ((isValidDate(expiryElem.getText(), calendarExpires)) && (calendarNow.before(calendarExpires))) {
                subscription.setExpires(calendarExpires);
            } else {
                setExpirationFault(subscription);
            }
        }
    } else {
        handleException("Subscribe element is required as the payload of the subscription message");
    }
    return subscription;
}

From source file:org.apache.synapse.eventing.builders.SubscriptionMessageBuilder.java

/**
 * (01) <s12:Envelope//from  w  w w  .j  a  v  a 2s . co m
 * (02)     xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
 * (03)     xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
 * (04)     xmlns:wse="http://schemas.xmlsoap.org/ws/2004/08/eventing"
 * (05)     xmlns:ow="http://www.example.org/oceanwatch" >
 * (06)   <s12:Header>
 * (07)     <wsa:Action>
 * (08)       http://schemas.xmlsoap.org/ws/2004/08/eventing/Renew
 * (09)     </wsa:Action>
 * (10)     <wsa:MessageID>
 * (11)       uuid:bd88b3df-5db4-4392-9621-aee9160721f6
 * (12)     </wsa:MessageID>
 * (13)     <wsa:ReplyTo>
 * (14)      <wsa:Address>http://www.example.com/MyEventSink</wsa:Address>
 * (15)     </wsa:ReplyTo>
 * (16)     <wsa:To>
 * (17)       http://www.example.org/oceanwatch/SubscriptionManager
 * (18)     </wsa:To>
 * (19)     <wse:Identifier>
 * (20)       uuid:22e8a584-0d18-4228-b2a8-3716fa2097fa
 * (21)     </wse:Identifier>
 * (22)   </s12:Header>
 * (23)   <s12:Body>
 * (24)     <wse:Renew>
 * (25)       <wse:Expires>2004-06-26T21:07:00.000-08:00</wse:Expires>
 * (26)     </wse:Renew>
 * (27)   </s12:Body>
 * (28) </s12:Envelope>
 *
 * @param mc MessageContext from which to create the SynapseSubscription
 * @return The SynapseSubscription
 */
public static SynapseSubscription createRenewSubscribeMessage(MessageContext mc) {
    SynapseSubscription subscription = new SynapseSubscription();
    OMElement elem = mc.getEnvelope().getHeader().getFirstChildWithName(IDENTIFIER);
    String id = elem.getText();
    subscription.setId(id);
    subscription.setAddressUrl(mc.getTo().getAddress());
    OMElement renewElem = mc.getEnvelope().getBody().getFirstChildWithName(RENEW);
    if (renewElem != null) {
        OMElement expiryElem = renewElem.getFirstChildWithName(EXPIRES);
        if (expiryElem != null) {
            if (!(expiryElem.getText().startsWith("*"))) {
                Calendar calendarExpires = null;
                try {
                    if (expiryElem.getText().startsWith("P")) {
                        calendarExpires = ConverterUtil.convertToDuration(expiryElem.getText()).getAsCalendar();
                    } else {
                        calendarExpires = ConverterUtil.convertToDateTime(expiryElem.getText());
                    }
                } catch (Exception e) {
                    setExpirationFault(subscription);
                }
                Calendar calendarNow = Calendar.getInstance();
                if ((isValidDate(expiryElem.getText(), calendarExpires))
                        && (calendarNow.before(calendarExpires))) {
                    subscription.setExpires(calendarExpires);
                } else {
                    setExpirationFault(subscription);
                }

                subscription.setExpires(calendarExpires);
            } else {
                setExpirationFault(subscription);
            }
        } else {
            setExpirationFault(subscription);
        }
    }
    return subscription;
}

From source file:org.apache.gobblin.service.modules.orchestration.AzkabanAjaxAPIClient.java

/***
 * Generate a random scheduled time between specified execution time window in the Azkaban compatible format
 * which is: hh,mm,a,z Eg. ScheduleTime=12,00,PM,PDT
 *
 * @param windowStartHour Window start hour in 24 hr (HH) format (inclusive)
 * @param windowEndHour Window end hour in 24 hr (HH) format (exclusive)
 * @param delayMinutes If current time is within window, then additional delay for bootstrapping if desired
 * @return Scheduled time string of the format hh,mm,a,z
 *//*  w w  w  .  jav  a  2 s .  c  o m*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "DMI_RANDOM_USED_ONLY_ONCE", justification = "As expected for randomization")
public static String getScheduledTimeInAzkabanFormat(int windowStartHour, int windowEndHour, int delayMinutes) {
    // Validate
    if (windowStartHour < 0 || windowEndHour > 23 || windowStartHour >= windowEndHour) {
        throw new IllegalArgumentException(
                "Window start should be less than window end, and both should be between " + "0 and 23");
    }
    if (delayMinutes < 0 || delayMinutes > 59) {
        throw new IllegalArgumentException("Delay in minutes should be between 0 and 59 (inclusive)");
    }

    // Setup window
    Calendar windowStartTime = Calendar.getInstance();
    windowStartTime.set(Calendar.HOUR_OF_DAY, windowStartHour);
    windowStartTime.set(Calendar.MINUTE, 0);
    windowStartTime.set(Calendar.SECOND, 0);

    Calendar windowEndTime = Calendar.getInstance();
    windowEndTime.set(Calendar.HOUR_OF_DAY, windowEndHour);
    windowEndTime.set(Calendar.MINUTE, 0);
    windowEndTime.set(Calendar.SECOND, 0);

    // Check if current time is between windowStartTime and windowEndTime, then let the execution happen
    // after delayMinutes minutes
    Calendar now = Calendar.getInstance();
    if (now.after(windowStartTime) && now.before(windowEndTime)) {
        // Azkaban takes a few seconds / a minute to bootstrap,
        // so extra few minutes get the first execution to run instantly
        now.add(Calendar.MINUTE, delayMinutes);

        return new SimpleDateFormat("hh,mm,a,z").format(now.getTime());
    }

    // Current time is not between windowStartTime and windowEndTime, so get random execution time for next day
    int allowedSchedulingWindow = (int) ((windowEndTime.getTimeInMillis() - windowStartTime.getTimeInMillis())
            / MILLISECONDS_IN_HOUR);
    int randomHourInWindow = new Random(System.currentTimeMillis()).nextInt(allowedSchedulingWindow);
    int randomMinute = new Random(System.currentTimeMillis()).nextInt(60);
    windowStartTime.add(Calendar.HOUR, randomHourInWindow);
    windowStartTime.set(Calendar.MINUTE, randomMinute);

    return new SimpleDateFormat("hh,mm,a,z").format(windowStartTime.getTime());
}

From source file:fedora.server.security.servletfilters.CacheElement.java

private static final String compareForExpiration(Calendar first, Calendar second) {
    String rc = null;//  ww  w.j  a  va 2 s  .  co m
    if (first.before(second)) {
        rc = "expires in " + difference(first, second);
    } else {
        rc = "expired " + difference(second, first) + " ago";
    }
    return rc;
}

From source file:org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils.java

/**
 * Applies the config to the given calendar.
 *//*from  w  ww  .ja  va  2s  .  c  o m*/
public static Calendar applyConfig(Calendar cal, AstroChannelConfig config) {
    Calendar cCal = cal;
    if (config.getOffset() != null && config.getOffset() != 0) {
        Calendar cOffset = Calendar.getInstance();
        cOffset.setTime(cCal.getTime());
        cOffset.add(Calendar.MINUTE, config.getOffset());
        cCal = cOffset;
    }

    Calendar cEarliest = adjustTime(cCal, getMinutesFromTime(config.getEarliest()));
    if (cCal.before(cEarliest)) {
        return cEarliest;
    }
    Calendar cLatest = adjustTime(cCal, getMinutesFromTime(config.getLatest()));
    if (cCal.after(cLatest)) {
        return cLatest;
    }

    return cCal;
}

From source file:osh.busdriver.MieleGatewayBusDriver.java

static private StartTimeDetails createStartTimeDetails(final UUID devUUID, long timestamp,
        MieleDeviceHomeBusData dev) {//  w ww  .j  a  va2 s  .  c  om

    StartTimeDetails startDetails = new StartTimeDetails(devUUID, timestamp);
    startDetails.setStartTime(-1);

    if (dev.getDeviceDetails() != null) {
        MieleDuration mieleStartTime = dev.getDeviceDetails().getStartTime();

        if (mieleStartTime != null) {
            int starttime = mieleStartTime.duration();

            if (starttime >= 0) {
                Calendar calNow = Calendar.getInstance();
                Calendar calStartTime = (Calendar) calNow.clone();

                calStartTime.set(Calendar.MINUTE, starttime % 60);
                calStartTime.set(Calendar.HOUR_OF_DAY, starttime / 60);

                if (calStartTime.before(calNow)) {
                    calStartTime.add(Calendar.DAY_OF_YEAR, 1);
                }

                startDetails.setStartTime(calStartTime.getTimeInMillis() / 1000L);
            }
        }
    }

    return startDetails;
}

From source file:net.granoeste.commons.util.DateUtils.java

/**
 * 2?????//from   w w w  . ja  va2  s .  c om
 *
 * @param date1 1 java.util.Date
 * @param date2 2 java.util.Date
 * @return 2????
 */
public static int diffMonth(final Date date1, final Date date2) {
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    cal1.set(Calendar.DATE, 1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    cal2.set(Calendar.DATE, 1);
    int count = 0;
    if (cal1.before(cal2)) {
        while (cal1.before(cal2)) {
            cal1.add(Calendar.MONTH, 1);
            count--;
        }
    } else {
        count--;
        while (!cal1.before(cal2)) {
            cal1.add(Calendar.MONTH, -1);
            count++;
        }
    }
    return count;
}