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() 

Source Link

Document

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

Usage

From source file:helper.util.DateHelper.java

/**
 * Take a local time calendar and represent it as a UTC day
 * (truncate to day, retain yy/mm/dd from original date)
 **//*from  w ww  .  jav  a2s.  co  m*/
public static Calendar asUtcDay(Calendar localTimeCalendar) {
    Calendar utcDate = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
    utcDate.clear();
    utcDate.set(localTimeCalendar.get(Calendar.YEAR), localTimeCalendar.get(Calendar.MONTH),
            localTimeCalendar.get(Calendar.DATE), 0, 0, 0);
    utcDate.set(Calendar.AM_PM, Calendar.AM);
    utcDate.set(Calendar.MILLISECOND, 0);
    utcDate.set(Calendar.HOUR, 0);
    return utcDate;
}

From source file:helper.util.DateHelper.java

/**
 * Take a local time calendar and represent it as a day
 * (truncate to day, retain yy/mm/dd from original date)
 **///  ww  w .  java  2 s .  c  om
public static Calendar asDay(Calendar localTimeCalendar) {
    Calendar asDay = Calendar.getInstance(localTimeCalendar.getTimeZone());
    asDay.clear();
    asDay.set(localTimeCalendar.get(Calendar.YEAR), localTimeCalendar.get(Calendar.MONTH),
            localTimeCalendar.get(Calendar.DATE), 0, 0, 0);
    asDay.set(Calendar.AM_PM, Calendar.AM);
    asDay.set(Calendar.MILLISECOND, 0);
    asDay.set(Calendar.HOUR, 0);
    return asDay;
}

From source file:com.vaadin.addon.jpacontainer.demo.TestDataGenerator.java

private static Date createRandomDate() {
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.YEAR, 1970 + dateRnd.nextInt(40));
    cal.set(Calendar.MONTH, dateRnd.nextInt(12));
    cal.set(Calendar.DATE, dateRnd.nextInt(cal.getMaximum(Calendar.DATE)) + 1);
    return cal.getTime();
}

From source file:org.wiztools.commons.DateUtil.java

private static Date getDatePlus(final int unit, final Date date, final int quantity) {
    Calendar c = Calendar.getInstance();
    c.clear();
    c.setTime(date);//from   w  w w .ja v  a2 s .co  m
    c.add(unit, quantity);
    return c.getTime();
}

From source file:org.hyperic.hq.measurement.shared.MeasTabManagerUtil.java

public static long getMeasTabEndTime(Calendar cal, long timems) {
    cal.clear();
    cal.setTime(new java.util.Date(timems));
    int incr = 24 / NUMBER_OF_TABLES_PER_DAY;
    for (int i = incr; i <= 24; i += incr) {
        if (cal.get(Calendar.HOUR_OF_DAY) < i) {
            cal.set(Calendar.HOUR_OF_DAY, i - 1);
            break;
        }/*from  w  w  w  .  j  av  a2 s .c  o m*/
    }
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 999);
    return cal.getTimeInMillis();
}

From source file:org.hyperic.hq.measurement.shared.MeasTabManagerUtil.java

public static long getMeasTabStartTime(Calendar cal, long timems) {
    cal.clear();
    cal.setTime(new java.util.Date(timems));
    int incr = 24 / NUMBER_OF_TABLES_PER_DAY;
    for (int i = incr; i <= 24; i += incr) {
        if (cal.get(Calendar.HOUR_OF_DAY) < i) {
            cal.set(Calendar.HOUR_OF_DAY, i - incr);
            break;
        }/*from w  w  w  .  ja  va 2  s . c o  m*/
    }
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

From source file:org.hyperic.hq.measurement.shared.MeasTabManagerUtil.java

public static long getPrevMeasTabTime(Calendar cal, long timems) {
    cal.clear();
    cal.setTime(new java.util.Date(timems));
    long rtn = -1;

    // need to do this because of DST
    // add() and roll() don't work right
    String currTable = getMeasTabname(timems);
    _log.debug("(getPrevMeasTabTime) before -> " + getDateStr(timems) + ", " + currTable);
    String newTable;//from   www.j a  va2  s . c om
    int incr = 24 / NUMBER_OF_TABLES_PER_DAY / 3;
    incr = ((incr < 1) ? 1 : incr) * -1;
    do {
        cal.add(Calendar.HOUR_OF_DAY, incr);
        rtn = cal.getTimeInMillis();
        _log.debug("subtracting 1 hour: " + getDateStr(rtn));
        newTable = getMeasTabname(rtn);
    } while (currTable.equals(newTable));

    _log.debug("(getPrevMeasTabTime) after -> " + getDateStr(rtn) + ", " + newTable);
    return rtn;
}

From source file:org.osaf.caldav4j.util.ICalendarUtils.java

private static void setFields(Date date, int year, int month, int day, int hour, int minutes, int seconds,
        int milliseconds, TimeZone tz, boolean utc) {

    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.setTimeZone(tz == null ? J_TZ_GMT : tz);
    if (date instanceof DateTime) {
        if (utc) {
            ((DateTime) date).setUtc(utc);
        } else if (tz != null) {
            ((DateTime) date).setTimeZone(tz);
        }//www.  j  a va2 s  .c  o m
        calendar.set(Calendar.HOUR, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, seconds);
        calendar.set(Calendar.MILLISECOND, milliseconds);
    }
    date.setTime(calendar.getTimeInMillis());
}

From source file:org.hyperic.hq.measurement.shared.MeasTabManagerUtil.java

private static int getDayOfPeriod(Calendar cal, long timems) {
    int rtn = 0;/*from   w  w  w  .ja  v a 2s .c  om*/
    cal.clear();
    cal.setTime(new java.util.Date(timems));
    Calendar currCal = Calendar.getInstance();
    currCal.setTime(new java.util.Date(timems));
    while (cal.get(Calendar.YEAR) >= _baseCal.get(Calendar.YEAR)) {
        if (cal.get(Calendar.YEAR) == currCal.get(Calendar.YEAR)) {
            rtn += currCal.get(Calendar.DAY_OF_YEAR);
        } else {
            rtn += cal.get(Calendar.DAY_OF_YEAR);
        }

        cal.add(Calendar.YEAR, -1);
        cal.set(Calendar.MONTH, 11);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 0);
    }
    return rtn;
}

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. j a v a 2s.  com*/
 * @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;
}