Example usage for java.util Calendar setLenient

List of usage examples for java.util Calendar setLenient

Introduction

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

Prototype

public void setLenient(boolean lenient) 

Source Link

Document

Specifies whether or not date/time interpretation is to be lenient.

Usage

From source file:org.exoplatform.cms.common.CommonUtils.java

/**
 * Get current time GMT/Zulu or UTC,(zone time is 0+GMT)
 * @return Calendar //  w w  w  . ja v  a 2 s .  c  o  m
 */
static public Calendar getGreenwichMeanTime() {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setLenient(false);
    int gmtoffset = calendar.get(Calendar.DST_OFFSET) + calendar.get(Calendar.ZONE_OFFSET);
    calendar.setTimeInMillis(System.currentTimeMillis() - gmtoffset);
    return calendar;
}

From source file:org.exoplatform.contact.CalendarUtils.java

public static Calendar getInstanceTempCalendar() {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setLenient(false);
    int gmtoffset = calendar.get(Calendar.DST_OFFSET) + calendar.get(Calendar.ZONE_OFFSET);
    calendar.setTimeInMillis(System.currentTimeMillis() - gmtoffset);
    return calendar;
}

From source file:org.exoplatform.poll.service.Utils.java

public static Calendar getGreenwichMeanTime() {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setLenient(false);
    int gmtoffset = calendar.get(Calendar.DST_OFFSET) + calendar.get(Calendar.ZONE_OFFSET);
    calendar.setTimeInMillis(System.currentTimeMillis() - gmtoffset);
    return calendar;
}

From source file:org.exoplatform.webui.utils.TimeConvertUtils.java

/**
 * Get current time GMT/Zulu or UTC,(zone time is 0+GMT)
 * @return Calendar /*from  ww  w .ja v a  2s .  co m*/
 */
public static Calendar getGreenwichMeanTime() {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setLenient(false);
    int gmtoffset = calendar.get(Calendar.DST_OFFSET) + calendar.get(Calendar.ZONE_OFFSET);
    calendar.setTimeInMillis(System.currentTimeMillis() - gmtoffset);
    return calendar;
}

From source file:org.jbpm.bpel.xml.util.DatatypeUtil.java

/**
 * Parses the lexical representation of a date/dateTime as per the XML Schema
 * recommendation.// w w  w.jav  a 2  s.c  o m
 * @param text the lexical representation of a date/dateTime
 * @return the date/dateTime the text represents
 * @see <a href="http://www.w3.org/TR/xmlschema-2/#dateTime"> XML Schema Part
 * 2: Datatypes &sect;3.2.7</a>
 */
public static Calendar parseDateTime(String text) {
    Matcher matcher = dateTimePattern.matcher(text);
    if (matcher.matches()) {
        Calendar dateTime = Calendar.getInstance();
        // calendar is initialized with the current time; must be cleared
        dateTime.clear();
        // group 1: year
        dateTime.set(Calendar.YEAR, Integer.parseInt(matcher.group(1)));
        // group 2: month; the month field is zero-based
        dateTime.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1);
        // group 3: day
        dateTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3)));
        // group 4: hour
        String group = matcher.group(4);
        if (group != null) {
            // if the hour is present, so are minutes and seconds
            dateTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(group));
            // group 5: minute
            dateTime.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5)));
            // group 6: second
            dateTime.set(Calendar.SECOND, Integer.parseInt(matcher.group(6)));
            // group 7: milliseconds
            group = matcher.group(7);
            if (group != null) {
                dateTime.set(Calendar.MILLISECOND, Integer.parseInt(group));
            }
        }
        // group 8: timezone
        group = matcher.group(8);
        if (group != null) {
            TimeZone timeZone;
            if (group.equals("Z")) {
                // "Z" means "the zero-length duration timezone"
                timeZone = TimeZone.getTimeZone("GMT+00:00");
            } else {
                timeZone = TimeZone.getTimeZone("GMT" + group);
            }
            dateTime.setTimeZone(timeZone);
        }
        // the schema recommendation mandates strict date interpretation
        dateTime.setLenient(false);
        return dateTime;
    }
    log.debug("invalid dateTime lexical representation: " + text);
    return null;
}

From source file:org.kuali.kfs.gl.batch.service.impl.RunDateServiceImpl.java

/**
 * Determines if the given calendar time is before the given cutoff time
 * /*from   ww  w.  java  2 s . c om*/
 * @param currentCal the current time
 * @param cutoffTime the "start of the day" cut off time
 * @return true if the current time is before the cutoff, false otherwise
 */
protected boolean isCurrentDateBeforeCutoff(Calendar currentCal, CutoffTime cutoffTime) {
    if (cutoffTime != null) {
        // if cutoff date is not properly defined
        // 24 hour clock (i.e. hour is 0 - 23)

        // clone the calendar so we get the same month, day, year
        // then change the hour, minute, second fields
        // then see if the cutoff is before or after
        Calendar cutoffCal = (Calendar) currentCal.clone();
        cutoffCal.setLenient(false);
        cutoffCal.set(Calendar.HOUR_OF_DAY, cutoffTime.hour);
        cutoffCal.set(Calendar.MINUTE, cutoffTime.minute);
        cutoffCal.set(Calendar.SECOND, cutoffTime.second);
        cutoffCal.set(Calendar.MILLISECOND, 0);

        return currentCal.before(cutoffCal);
    }
    // if cutoff date is not properly defined, then it is considered to be after the cutoff
    return false;
}

From source file:org.kuali.kfs.module.ld.batch.service.impl.LaborScrubberProcess.java

protected boolean isCurrentDateBeforeCutoff(Calendar currentCal) {
    if (cutoffHour != null && cutoffMinute != null && cutoffSecond != null) {
        // if cutoff date is not properly defined
        // 24 hour clock (i.e. hour is 0 - 23)

        // clone the calendar so we get the same month, day, year
        // then change the hour, minute, second fields
        // then see if the cutoff is before or after
        Calendar cutoffTime = (Calendar) currentCal.clone();
        cutoffTime.setLenient(false);
        cutoffTime.set(Calendar.HOUR_OF_DAY, cutoffHour);
        cutoffTime.set(Calendar.MINUTE, cutoffMinute);
        cutoffTime.set(Calendar.SECOND, cutoffSecond);
        cutoffTime.set(Calendar.MILLISECOND, 0);

        return currentCal.before(cutoffTime);
    }/*from w  w  w.j  av a 2s.  c om*/
    // if cutoff date is not properly defined, then it is considered to be after the cutoff
    return false;
}

From source file:org.kuali.kfs.module.purap.batch.service.impl.PurapRunDateServiceImpl.java

/**
 * Determines if the given calendar time is before the given cutoff time
 * // w w w. j  a  va2s  . c  o m
 * @param currentCal the current time
 * @param cutoffTime the "start of the day" cut off time
 * @return true if the current time is before the cutoff, false otherwise
 */
protected boolean isCurrentDateAfterCutoff(Calendar currentCal, CutoffTime cutoffTime) {
    if (cutoffTime != null) {
        // if cutoff date is not properly defined
        // 24 hour clock (i.e. hour is 0 - 23)

        // clone the calendar so we get the same month, day, year
        // then change the hour, minute, second fields
        // then see if the cutoff is before or after
        Calendar cutoffCal = (Calendar) currentCal.clone();
        cutoffCal.setLenient(false);
        cutoffCal.set(Calendar.HOUR_OF_DAY, cutoffTime.hour);
        cutoffCal.set(Calendar.MINUTE, cutoffTime.minute);
        cutoffCal.set(Calendar.SECOND, cutoffTime.second);
        cutoffCal.set(Calendar.MILLISECOND, 0);

        return currentCal.after(cutoffCal);
    }
    // if cutoff date is not properly defined, then it is considered to be before the cutoff, that is, no cutoff date will be applied
    return false;
}

From source file:org.kuali.rice.core.impl.datetime.DateTimeServiceImpl.java

protected Date parse(String dateString, String pattern) throws ParseException {
    if (!StringUtils.isBlank(dateString)) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        dateFormat.setLenient(false);// ww w. j a va2 s  . co  m
        ParsePosition parsePosition = new ParsePosition(0);
        Date testDate = dateFormat.parse(dateString, parsePosition);

        // Ensure that the entire date String can be parsed by the current format.
        if (testDate == null) {
            throw new ParseException("The date that you provided is invalid.", parsePosition.getErrorIndex());
        } else if (parsePosition.getIndex() != dateString.length()) {
            throw new ParseException("The date that you provided is invalid.", parsePosition.getIndex());
        }

        // Ensure that the date's year lies between 1000 and 9999, to help prevent database-related date errors.
        Calendar testCalendar = Calendar.getInstance();
        testCalendar.setLenient(false);
        testCalendar.setTime(testDate);
        if (testCalendar.get(Calendar.YEAR) < 1000 || testCalendar.get(Calendar.YEAR) > 9999) {
            throw new ParseException("The date that you provided is not between the years 1000 and 9999.", -1);
        }

        if (testCalendar.get(Calendar.YEAR) == 1970 && !pattern.contains("y".toLowerCase())) {
            Calendar curCalendar = Calendar.getInstance();
            curCalendar.setTime(new java.util.Date());
            testCalendar.set(Calendar.YEAR, curCalendar.get(Calendar.YEAR));
            testDate = testCalendar.getTime();
        }

        return testDate;
    }
    return null;
}

From source file:org.kuali.rice.kew.util.Utilities.java

public static boolean checkDateRanges(String fromDate, String toDate) {
    try {/*from w w w  . j a va  2  s.c  o m*/
        Date parsedDate = CoreApiServiceLocator.getDateTimeService().convertToDate(fromDate.trim());
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setLenient(false);
        fromCalendar.setTime(parsedDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);
        parsedDate = CoreApiServiceLocator.getDateTimeService().convertToDate(toDate.trim());
        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setLenient(false);
        toCalendar.setTime(parsedDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);
        if (fromCalendar.after(toCalendar)) {
            return false;
        }
        return true;
    } catch (Exception ex) {
        return false;
    }
}