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.betaconceptframework.astroboa.util.DateUtils.java

public static Calendar clearTimeFromCalendar(Calendar calendar) {
    if (calendar != null) {
        calendar.set(Calendar.HOUR, 0);
        calendar.clear(Calendar.AM_PM); //ALWAYS clear AM_PM before HOUR_OF_DAY
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.clear(Calendar.DST_OFFSET);
        calendar.clear(Calendar.ZONE_OFFSET);
    }//w  w  w .  j  a va 2  s  .co  m

    return calendar;
}

From source file:org.springframework.session.data.codis.CodisSessionExpirationPolicy.java

static long roundUpToNextMinute(long timeInMs) {
    Calendar date = Calendar.getInstance();
    date.setTimeInMillis(timeInMs);//from   w w  w.j a v  a 2  s  . c  o m
    date.add(Calendar.MINUTE, 1);
    date.clear(Calendar.SECOND);
    date.clear(Calendar.MILLISECOND);
    return date.getTimeInMillis();
}

From source file:org.springframework.session.data.redis.RedisSessionExpirationPolicy.java

static long roundUpToNextMinute(long timeInMs) {

    Calendar date = Calendar.getInstance();
    date.setTimeInMillis(timeInMs);//from w  w w  .  j a  v a 2 s . co  m
    date.add(Calendar.MINUTE, 1);
    date.clear(Calendar.SECOND);
    date.clear(Calendar.MILLISECOND);
    return date.getTimeInMillis();
}

From source file:Main.java

/**
 *
 * This method is a utility method to create a new java.sql.Date in one line.
 *
 * @param year/*from  w  w w  . ja v a 2  s . c  om*/
 * @param month
 * @param day
 *
 * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second,
 *         millisecond
 *
 */
public static java.sql.Date newDate(Integer year, Integer month, Integer day) {

    // test for null arguments
    if (year == null) {
        throw new IllegalArgumentException("Argument 'year' passed in was null.");
    }
    if (month == null) {
        throw new IllegalArgumentException("Argument 'month' passed in was null.");
    }
    if (day == null) {
        throw new IllegalArgumentException("Argument 'day' passed in was null.");
    }

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.clear(Calendar.HOUR_OF_DAY);
    calendar.clear(Calendar.MINUTE);
    calendar.clear(Calendar.SECOND);
    calendar.clear(Calendar.MILLISECOND);

    return new java.sql.Date(calendar.getTimeInMillis());
}

From source file:TimeLib.java

/**
 * Get the timestamp for the given year, month, and, day.
 * @param c a Calendar to use to help compute the result. The state of the
 * Calendar will be overwritten./*  ww  w .j  a  va 2 s. c  o m*/
 * @param year the year to look up
 * @param month the month to look up (months start at 0==January)
 * @param day the day to look up
 * @return the timestamp for the given date
 */
public static long getDate(Calendar c, int year, int month, int day) {
    c.clear(Calendar.MILLISECOND);
    c.set(year, month, day, 0, 0, 0);
    return c.getTimeInMillis();
}

From source file:TimeLib.java

/**
 * Get a timestamp for the given hour, minute, and second. The date will
 * be assumed to be January 1, 1970.//from   w w  w  .j a v a2  s  .  c  om
 * @param c a Calendar to use to help compute the result. The state of the
 * Calendar will be overwritten.
 * @param hour the hour, on a 24 hour clock
 * @param minute the minute value
 * @param second the seconds value
 * @return the timestamp for the given date
 */
public static long getTime(Calendar c, int hour, int minute, int second) {
    c.clear(Calendar.MILLISECOND);
    c.set(1970, 0, 1, hour, minute, second);
    return c.getTimeInMillis();
}

From source file:Main.java

/**
 *
 * This method is a utility method to create a new java.sql.Date in one line.
 *
 * @param year//from  w w  w  .  java  2 s . com
 * @param month
 * @param day
 * @param hour
 * @param minute
 * @param second
 *
 * @return a populated java.sql.Date with the year, month, hour, minute, and second populated, with no value for millisecond.
 *
 */
public static java.sql.Date newDate(Integer year, Integer month, Integer day, Integer hour, Integer minute,
        Integer second) {

    // test for null arguments
    if (year == null) {
        throw new IllegalArgumentException("Argument 'year' passed in was null.");
    }
    if (month == null) {
        throw new IllegalArgumentException("Argument 'month' passed in was null.");
    }
    if (day == null) {
        throw new IllegalArgumentException("Argument 'day' passed in was null.");
    }
    if (hour == null) {
        throw new IllegalArgumentException("Argument 'hour' passed in was null.");
    }
    if (minute == null) {
        throw new IllegalArgumentException("Argument 'minute' passed in was null.");
    }
    if (second == null) {
        throw new IllegalArgumentException("Argument 'second' passed in was null.");
    }

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);
    calendar.clear(Calendar.MILLISECOND);

    return new java.sql.Date(calendar.getTimeInMillis());
}

From source file:controllers.core.TimesheetController.java

/**
 * Get the timesheet report of an actor for a given string date.
 * /*from w  w  w . ja  v a2 s.c  o  m*/
 * if the string date is empty: the start date corresponds to the first day
 * (monday) of the current week<br/>
 * else: the start date corresponds to the first day (monday) of the week
 * including the given date
 * 
 * Note: if the report doesn't exist, the system creates it.
 * 
 * @param stringDate
 *            a date in the format yyyy-MM-dd: the system gets the weekly
 *            report including this date, if empty it uses the current date.
 * @param actor
 *            the actor of the timesheet
 */
public static TimesheetReport getTimesheetReport(String stringDate, Actor actor) {

    // get the date: either given as a parameter or the current
    Date date = null;
    if (!stringDate.equals("")) {
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            date = formatter.parse(stringDate);
        } catch (ParseException e) {
            log.error(e.getMessage());
            return null;
        }
    } else {
        date = new Date();
    }

    // get the first day of the week including the date
    // we consider the first day as Monday
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MILLISECOND);
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    Date startDate = cal.getTime();

    TimesheetReport report = TimesheetDao.getTimesheetReportByActorAndStartDate(actor.id, startDate);
    if (report == null) {
        report = new TimesheetReport();
        report.actor = actor;
        report.orgUnit = actor.orgUnit;
        report.type = TimesheetReport.Type.WEEKLY;
        report.startDate = startDate;
        report.status = TimesheetReport.Status.OPEN;
        report.save();
    }

    return report;

}

From source file:org.power.commons.lang.util.time.DateUtils.java

/**
 * ??? ?2004-08-01 11:30:582004-08-01 11:00:00
 *
 * @return Date java.util.Date/*from  w  ww  .  ja v  a2s  . co m*/
 */
public static Date getRoundedHourCurDate() {

    Calendar cal = GregorianCalendar.getInstance();

    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MILLISECOND);

    return cal.getTime();

}

From source file:org.power.commons.lang.util.time.DateUtils.java

/**
 * ??? 2004-08-01 11:30:582004-08-01 11:00:00
 *
 * @param dt Date java.util.Date//from www.  j a  va 2 s  .  c  o m
 * @return Date java.util.Date
 */
public static Date getRoundedHourDate(Date dt) {

    Calendar cal = new GregorianCalendar();

    cal.setTime(dt);

    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MILLISECOND);

    return cal.getTime();
}