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:Main.java

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();

    // print current state of lenient
    boolean b = cal.isLenient();
    System.out.println("Calendar is lenient :" + b);

    // change lenient state
    cal.setLenient(false);

    System.out.println("Lenient after setting :" + cal.isLenient());

}

From source file:com.wiiyaya.framework.common.utils.DateUtils.java

/**
 * 00:00:00//from   ww  w  .  j a  v a 2 s.  c o  m
 *
 * @param date 
 * @return ??
 */
public static Date setDayStart(final Date date) {
    final Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    return c.getTime();
}

From source file:com.wiiyaya.framework.common.utils.DateUtils.java

/**
 * ?23:59:59/*from   w w  w  .j  a  v  a  2 s.c  om*/
 * @param date 
 * @return ??
 */
public static Date setDayEnd(final Date date) {
    final Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    return c.getTime();
}

From source file:Main.java

public static Calendar dateToCalendar(String strYear, String strMonth, String strDay, boolean b) {
    int year, month, day;
    try {/*from  www .j  a v a2 s  . co  m*/
        year = Integer.parseInt(strYear);
        month = Integer.parseInt(strMonth) - 1;
        day = Integer.parseInt(strDay);
    } catch (StringIndexOutOfBoundsException e) {
        return null;
    } catch (NumberFormatException e) {
        return null;
    }
    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    cal.setLenient(b);
    cal.set(year, month, day, 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal;
}

From source file:Main.java

/**
 * Sets the specified field to a date returning a new object.  
 * This does not use a lenient calendar.
 * The original date object is unchanged.
 *
 * @param date  the date, not null/*ww w  .j  a  v a2s  . co  m*/
 * @param calendarField  the calendar field to set the amount to
 * @param amount the amount to set
 * @return a new Date object set with the specified value
 * @throws IllegalArgumentException if the date is null
 * @since 2.4
 */
private static Date set(Date date, int calendarField, int amount) {
    if (date == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    // getInstance() returns a new object, so this method is thread safe.
    Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.setTime(date);
    c.set(calendarField, amount);
    return c.getTime();
}

From source file:io.horizondb.model.core.util.TimeUtils.java

/**
 * Parses the specified <code>String</code> representing a date/time.
 * <p>The supported patterns are: 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss' and 
 * 'yyyy-MM-dd HH:mm:ss.SSS'.</p>//  www .  j av a2  s. co m
 * 
 * @param timeZone the date TimeZone
 * @param dateTime the <code>String</code> representing a date/time.
 * @return the time in millisecond since epoch
 */
public static long parseDateTime(TimeZone timeZone, String dateTime) {

    String pattern = getPattern(dateTime);
    SimpleDateFormat format = new SimpleDateFormat(pattern);

    Calendar calendar = Calendar.getInstance(timeZone);
    calendar.clear();
    calendar.setLenient(false);

    format.setCalendar(calendar);

    Date date = format.parse(dateTime, new ParsePosition(0));

    if (date == null) {
        throw new IllegalArgumentException(format("The value %s cannot be parsed into a valid date", dateTime));
    }
    return date.getTime();
}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

public static boolean isAbsoluteDateInTimezone(Date date, String timeZone) {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(timeZone));
    cal.setLenient(false);
    cal.setTime(date);/*  w w  w  .jav  a2 s  .  c  o m*/
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minutes = cal.get(Calendar.MINUTE);
    int seconds = cal.get(Calendar.SECOND);
    int milliseconds = cal.get(Calendar.MILLISECOND);
    if (hour == 0 && minutes == 0 && seconds == 0 && milliseconds == 0) {
        return true;
    } else {
        return false;
    }
}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

public static Date convertGMTToTimezoneAbsoluteDate(Date dateValue, String sourceSystemTimezone) {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(GMT_TIME_ZONE_STRING));
    cal.setLenient(false);
    cal.setTime(dateValue);/*w w w.j  av  a  2  s  .co m*/
    Calendar newCal = new GregorianCalendar(TimeZone.getTimeZone(sourceSystemTimezone));
    newCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    newCal.set(Calendar.MILLISECOND, 0);
    Date returnDate = newCal.getTime();
    return returnDate;
}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

public static Date convertToGMTAbsoluteDate(Date dateValue, String sourceSystemTimezone) {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(sourceSystemTimezone));
    cal.setLenient(false);
    cal.setTime(dateValue);//  www. j a v a2  s.  c o  m
    Calendar newCal = new GregorianCalendar(TimeZone.getTimeZone(GMT_TIME_ZONE_STRING));
    newCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    newCal.set(Calendar.MILLISECOND, 0);
    Date returnDate = newCal.getTime();
    return returnDate;
}

From source file:Main.java

public static String generateRandomDate() {
    Random r = new Random();
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
    c.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
    c.setLenient(true);
    return DATE_FORMAT.format(c.getTime());
}