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.openmrs.web.dwr.DWRPersonService.java

/**
 * Private method to handle birth date and age input.
 *
 * @param birthdate//from   w ww  .  j av a2  s  .c om
 * @param dateformat
 * @param age
 * @return
 * @throws java.text.ParseException
 */
private Date updateAge(String birthdate, String dateformat, String age) throws java.text.ParseException {
    SimpleDateFormat df = new SimpleDateFormat();
    if (!"".equals(dateformat)) {
        dateformat = dateformat.toLowerCase().replaceAll("m", "M");
    } else {
        dateformat = new String("MM/dd/yyyy");
    }
    df.applyPattern(dateformat);
    Calendar cal = Calendar.getInstance();
    cal.clear(Calendar.HOUR);
    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MILLISECOND);
    if ("".equals(birthdate)) {
        if ("".equals(age)) {
            return cal.getTime();
        }
        try {
            cal.add(Calendar.YEAR, -(Integer.parseInt(age)));
        } catch (NumberFormatException nfe) {
            log.error("Error during adding date into calendar", nfe);
        }
        return cal.getTime();
    } else {
        cal.setTime(df.parse(birthdate));
    }
    return cal.getTime();
}

From source file:org.apache.ranger.audit.utils.RollingTimeUtil.java

private Date computeTopofMonthDate(int months) {

    Date ret = null;/*from   w ww  .ja  v a2s  .com*/

    Calendar calendarMonth = Calendar.getInstance();
    calendarMonth.set(Calendar.DAY_OF_MONTH, 1);
    calendarMonth.add(Calendar.MONTH, months);
    calendarMonth.set(Calendar.HOUR_OF_DAY, 0);
    calendarMonth.clear(Calendar.MINUTE);
    calendarMonth.clear(Calendar.SECOND);
    calendarMonth.clear(Calendar.MILLISECOND);

    ret = calendarMonth.getTime();

    return ret;
}

From source file:org.betaconceptframework.astroboa.portal.utility.CalendarUtils.java

public void 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  ww  .  ja  va  2  s  . c  o m*/

}

From source file:edu.ku.brc.specify.dbsupport.customqueries.CustomStatQueries.java

/**
 * Returns how many items ere cataloged over a perio of time.
 * @param numDays how many days back/*from www .  j a  v a2  s. co  m*/
 * @return true if executed correctly
 */
protected boolean catalogedLastXDays(final int numDays) {
    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();

    startDate.clear(Calendar.HOUR_OF_DAY);
    startDate.clear(Calendar.MINUTE);
    startDate.clear(Calendar.SECOND);

    Calendar today = Calendar.getInstance();
    today.add(Calendar.DAY_OF_MONTH, -numDays);
    startDate.clear();
    startDate.set(Calendar.YEAR, today.get(Calendar.YEAR));
    startDate.set(Calendar.MONTH, today.get(Calendar.MONTH));
    startDate.set(Calendar.DAY_OF_MONTH, today.get(Calendar.DAY_OF_MONTH));

    /*
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    System.out.print(sdf.format(startDate.getTime()));
    System.out.println(" * "+sdf.format(endDate.getTime()));//new Date(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DAY_OF_MONTH))));
    */

    // XXX (need try block)
    Session session = HibernateUtil.getNewSession();

    Calendar startCal = Calendar.getInstance();
    startCal.setTime(startDate.getTime());

    Calendar endCal = Calendar.getInstance();
    endCal.setTime(endDate.getTime());

    Criteria criteria = session.createCriteria(CollectionObject.class);
    criteria.add(Expression.ge("catalogedDate", startCal));
    criteria.add(Expression.le("catalogedDate", endCal));
    criteria.add(Expression.eq("collectionMemberId",
            AppContextMgr.getInstance().getClassObject(Collection.class).getCollectionId()));

    criteria.setProjection(Projections.rowCount());
    resultsList = criteria.list();

    //for (Object data : resultsList)
    //{
    //System.out.println(((CollectionObject)data).getIdentityTitle());
    //System.out.println(data);
    //}
    session.close();

    return true;
}

From source file:org.apache.ranger.audit.utils.RollingTimeUtil.java

private Date computeTopOfWeekDate(int weeks) {
    Date ret = null;/*from w  w  w  .j a v a  2 s. c om*/

    Calendar calendarWeek = Calendar.getInstance();
    calendarWeek.set(Calendar.DAY_OF_WEEK, calendarWeek.getFirstDayOfWeek());
    calendarWeek.add(Calendar.WEEK_OF_YEAR, weeks);
    calendarWeek.set(Calendar.HOUR_OF_DAY, 0);
    calendarWeek.clear(Calendar.MINUTE);
    calendarWeek.clear(Calendar.SECOND);
    calendarWeek.clear(Calendar.MILLISECOND);

    ret = calendarWeek.getTime();

    return ret;
}

From source file:org.eclipse.dirigible.runtime.registry.RegistryServlet.java

private Calendar getCalendar(Date time) {
    Calendar calendar = Calendar.getInstance();
    if (time == null) {
        return calendar;
    }/* ww w  .  j  a va2s.  c om*/
    calendar.setTime(time);
    calendar.clear(Calendar.MILLISECOND);
    return calendar;
}

From source file:org.apache.ranger.audit.utils.RollingTimeUtil.java

private Date computeTopOfYearDate(int years) {
    Date ret = null;/*from w w w .j av  a 2s. c  om*/

    Calendar calendarStart = Calendar.getInstance();
    calendarStart.add(Calendar.YEAR, years);
    calendarStart.set(Calendar.MONTH, 0);
    calendarStart.set(Calendar.DAY_OF_MONTH, 1);
    calendarStart.set(Calendar.HOUR_OF_DAY, 0);
    calendarStart.clear(Calendar.MINUTE);
    calendarStart.clear(Calendar.SECOND);
    calendarStart.clear(Calendar.MILLISECOND);

    ret = calendarStart.getTime();

    return ret;
}

From source file:org.apache.ftpserver.clienttests.ListTest.java

public void testListFile() throws Exception {

    TEST_DIR1.mkdirs();/*  w w  w .  ja va2 s . c  o m*/
    TEST_FILE1.createNewFile();
    TEST_FILE2.createNewFile();

    FTPFile[] files = client.listFiles(TEST_FILE1.getName());

    assertEquals(1, files.length);

    FTPFile file = getFile(files, TEST_FILE1.getName());
    assertEquals(TEST_FILE1.getName(), file.getName());
    assertEquals(0, file.getSize());
    assertEquals("group", file.getGroup());
    assertEquals("user", file.getUser());
    assertTrue(file.isFile());
    assertFalse(file.isDirectory());

    Calendar expectedTimestamp = Calendar.getInstance();
    expectedTimestamp.setTimeInMillis(TEST_FILE1.lastModified());
    // server does not supply seconds and milliseconds
    expectedTimestamp.clear(Calendar.SECOND);
    expectedTimestamp.clear(Calendar.MILLISECOND);

    assertEquals(expectedTimestamp, file.getTimestamp());
}

From source file:it.govpay.web.console.pagamenti.service.DistintaService.java

private void impostaDate(DistintaSearchForm form, DistintaFilter filtro) {
    SelectList<SelectItem> dataPeriodo = form.getDataPeriodo();
    DateTime data = form.getData();//from  www.  j av a 2 s  .c  o m

    Date dataInizio = data.getValue();
    Date dataFine = data.getValue2();

    String periodo = dataPeriodo.getValue() != null ? dataPeriodo.getValue().getValue()
            : DistintaSearchForm.DATA_PERIODO_ULTIMA_SETTIMANA;

    Calendar today = Calendar.getInstance();
    today.set(Calendar.HOUR_OF_DAY, 23);
    today.set(Calendar.MINUTE, 59);
    today.clear(Calendar.SECOND);
    today.clear(Calendar.MILLISECOND);

    //ultima settimana
    if (DistintaSearchForm.DATA_PERIODO_ULTIMA_SETTIMANA.equals(periodo)) {
        Calendar lastWeek = (Calendar) today.clone();
        Calendar c = Calendar.getInstance();
        dataFine = c.getTime();
        lastWeek.set(Calendar.HOUR_OF_DAY, 0);
        lastWeek.set(Calendar.MINUTE, 0);
        lastWeek.add(Calendar.DATE, -7);
        dataInizio = lastWeek.getTime();

    } else if (DistintaSearchForm.DATA_PERIODO_ULTIMO_MESE.equals(periodo)) {
        Calendar lastMonth = (Calendar) today.clone();

        // prendo la data corrente
        dataFine = Calendar.getInstance().getTime();

        // la data inizio rimane uguale sia per giornaliero che per orario
        lastMonth.set(Calendar.HOUR_OF_DAY, 0);
        lastMonth.set(Calendar.MINUTE, 0);
        lastMonth.add(Calendar.DATE, -30);
        dataInizio = lastMonth.getTime();

    } else if (DistintaSearchForm.DATA_PERIODO_ULTIMI_TRE_MESI.equals(periodo)) {
        Calendar lastyear = (Calendar) today.clone();

        dataFine = Calendar.getInstance().getTime();

        lastyear.set(Calendar.HOUR_OF_DAY, 0);
        lastyear.set(Calendar.MINUTE, 0);
        lastyear.add(Calendar.DATE, -90);
        dataInizio = lastyear.getTime();

    } else {
        // personalizzato
        dataInizio = data.getValue();
        dataFine = data.getValue2();
    }

    if (dataInizio != null) {
        log.debug("Data inizio[" + dataInizio + "]");
        filtro.setDataInizio(dataInizio);
    }

    if (dataFine != null) {
        log.debug("Data Fine[" + dataFine + "]");
        filtro.setDataFine(dataFine);
    }
}

From source file:cn.mljia.common.notify.utils.DateUtils.java

/**
 * ???//from ww  w. j a  v a  2 s.  c  o  m
 * 
 * @param date
 *            1
 * @param otherDate
 *            2
 * @param withUnit
 *            ??Calendar field?
 * @return 0, 0 ??0
 */
public static int compareTime(Date date, Date otherDate, int withUnit) {
    Calendar dateCal = Calendar.getInstance();
    dateCal.setTime(date);
    Calendar otherDateCal = Calendar.getInstance();
    otherDateCal.setTime(otherDate);

    dateCal.clear(Calendar.YEAR);
    dateCal.clear(Calendar.MONTH);
    dateCal.set(Calendar.DATE, 1);
    otherDateCal.clear(Calendar.YEAR);
    otherDateCal.clear(Calendar.MONTH);
    otherDateCal.set(Calendar.DATE, 1);
    switch (withUnit) {
    case Calendar.HOUR:
        dateCal.clear(Calendar.MINUTE);
        otherDateCal.clear(Calendar.MINUTE);
    case Calendar.MINUTE:
        dateCal.clear(Calendar.SECOND);
        otherDateCal.clear(Calendar.SECOND);
    case Calendar.SECOND:
        dateCal.clear(Calendar.MILLISECOND);
        otherDateCal.clear(Calendar.MILLISECOND);
    case Calendar.MILLISECOND:
        break;
    default:
        throw new IllegalArgumentException("withUnit ?? " + withUnit + " ????");
    }
    return dateCal.compareTo(otherDateCal);
}