Example usage for java.util Calendar clone

List of usage examples for java.util Calendar clone

Introduction

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

Prototype

@Override
public Object clone() 

Source Link

Document

Creates and returns a copy of this object.

Usage

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ?//from w  w  w  .j a v a 2s .  c  o  m
 *
 * @return
 */
public static String getPreviousMonthEnd() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    // f.set(Calendar.MILLISECOND, -1);
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    // f.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // (?)
    cal.set(Calendar.DATE, 1);// ?1?
    cal.add(Calendar.MONTH, 0);//
    cal.add(Calendar.DATE, -1);// ???
    return DateFormatUtils.format(cal, DATE_FORMAT);
}

From source file:nl.strohalm.cyclos.utils.DateHelper.java

/**
 * compares two Calendars on a certain precision level. <br>
 * For example, if you had the datetime of 12 Mar 2011 14:31:07.847, and a second datetime of 12 Mar 2011 14:31:11.734, they would evaluate as
 * equal on the Calendar.MINUTE level and above. They would evaluate as not equal on levels Calendar.SECOND and Calendar.MILLISECOND.<br>
 * Fields are rounded, so 12 Mar 2011 14:31:07.847 and 12 Mar 2011 14:31:08.123 would evaluate as equal on the Calendar.SECOND level.
 * @param cal1 if null, returns false/*from w w w.ja  va2  s  .c o  m*/
 * @param cal2 if null, returns false
 * @param level, for example Calendar.MINUTE
 * @return true if equal on this level, false if not.
 */
public static boolean equals(final Calendar cal1, final Calendar cal2, final int level) {
    if (cal1 == null || cal2 == null) {
        return false;
    }
    final Calendar temp1 = DateUtils.round((Calendar) cal1.clone(), level);
    final Calendar temp2 = DateUtils.round((Calendar) cal2.clone(), level);
    return (temp1.equals(temp2));
}

From source file:osh.busdriver.MieleGatewayBusDriver.java

static private StartTimeDetails createStartTimeDetails(final UUID devUUID, long timestamp,
        MieleDeviceHomeBusData dev) {//  w  ww  .j  a v a2 s  .  co  m

    StartTimeDetails startDetails = new StartTimeDetails(devUUID, timestamp);
    startDetails.setStartTime(-1);

    if (dev.getDeviceDetails() != null) {
        MieleDuration mieleStartTime = dev.getDeviceDetails().getStartTime();

        if (mieleStartTime != null) {
            int starttime = mieleStartTime.duration();

            if (starttime >= 0) {
                Calendar calNow = Calendar.getInstance();
                Calendar calStartTime = (Calendar) calNow.clone();

                calStartTime.set(Calendar.MINUTE, starttime % 60);
                calStartTime.set(Calendar.HOUR_OF_DAY, starttime / 60);

                if (calStartTime.before(calNow)) {
                    calStartTime.add(Calendar.DAY_OF_YEAR, 1);
                }

                startDetails.setStartTime(calStartTime.getTimeInMillis() / 1000L);
            }
        }
    }

    return startDetails;
}

From source file:com.maydesk.base.util.PDUtil.java

public static Calendar getDateOnlyCalendar(Calendar cal) {
    Calendar ret;/* w ww .  jav  a 2s  . co  m*/

    if (cal == null) {
        ret = Calendar.getInstance();
    } else {
        ret = (Calendar) cal.clone();
    }

    ret.set(Calendar.HOUR_OF_DAY, 0);
    ret.set(Calendar.MINUTE, 0);
    ret.set(Calendar.SECOND, 0);
    ret.set(Calendar.MILLISECOND, 0);

    return ret;
}

From source file:net.qvex.dommel.data.DommelDataService.java

public static int daysBetween(Calendar startCal, Calendar endCal) {

    Calendar date = (Calendar) startCal.clone();
    int daysBetween = 0;
    while (date.before(endCal)) {
        date.add(Calendar.DAY_OF_MONTH, 1);
        daysBetween++;/*from   www.ja  v a2  s .  c o  m*/
    }
    return daysBetween;
}

From source file:CalendarUtils.java

/**
 * Round this date, leaving the field specified as the most significant
 * field.  For example, if you had the datetime of 28 Mar 2002
 * 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
 * 2002 14:00:00.000.  If this was passed with MONTH, it would return
 * 1 April 2002 0:00:00.000.//from  w  w w.j av a  2s  . c o m
 */
public static Calendar round(Calendar val, int field) {
    Calendar rounded = (Calendar) val.clone();
    modify(rounded, field, true);
    return rounded;
}

From source file:CalendarUtils.java

/**
 * Truncate this date, leaving the field specified as the most significant
 * field.  For example, if you had the datetime of 28 Mar 2002
 * 13:45:01.231, if you passed with HOUR, it would return 28 Mar
 * 2002 13:00:00.000.  If this was passed with MONTH, it would return
 * 1 Mar 2002 0:00:00.000./*from ww  w  .  j a  va  2s . co  m*/
 */
public static Calendar trunc(Calendar val, int field) {
    Calendar truncated = (Calendar) val.clone();
    modify(truncated, field, false);
    return truncated;
}

From source file:org.getobjects.foundation.UDate.java

/**
 * Creates a new Calendar by cloning the Calendar which is passed in and then
 * calling Calendar.add() for each non-zero argument.
 *
 * @param _cal    - the Calendar on which the operation is based
 * @param _years  - number of years to add, or 0
 * @param _months - number of months to add, or 0
 * @param _days   - number of days to add, or 0
 * @param _hours  - number of hours to add, or 0
 * @param _mins   - number of minutes to add, or 0
 * @param _secs   - number of seconds to add, or 0
 * @return an adjusted Calendar/*from   www. j av  a 2  s .  c  om*/
 */
public static Calendar calendarByAdding(Calendar _cal, int _years, int _months, int _days, int _hours,
        int _mins, int _secs) {
    if (_cal == null)
        return null;

    Calendar cal = (Calendar) _cal.clone();

    if (_years != 0)
        cal.add(Calendar.YEAR, _years);
    if (_months != 0)
        cal.add(Calendar.MONTH, _months);
    if (_days != 0)
        cal.add(Calendar.DAY_OF_MONTH, _days);
    if (_hours != 0)
        cal.add(Calendar.HOUR, _hours);
    if (_mins != 0)
        cal.add(Calendar.MINUTE, _mins);
    if (_secs != 0)
        cal.add(Calendar.SECOND, _secs);

    return cal;
}

From source file:org.getobjects.foundation.UDate.java

/**
 * This returns a Calendar object which represents the beginning of the week
 * in the Calendar's locale and timezone.
 * <p>/*from  ww w  .  j  a  v  a2s  .  c om*/
 * Eg in a German locale this could be
 *   'Mon, 2007-06-04 00:00:00'
 * if the Calendar being passed in is
 *   'Wed, 2007-06-06 12:34:37'.
 * In US locales the week would start on Sunday etc.
 *
 * @param _calendar - a Calendar in the week
 * @return a Calendar representing the start of the week
 */
public static Calendar beginningOfWeekAsCalendar(Calendar _calendar) {
    if (_calendar == null) {
        /* not recommended as the timezone is most likely incorrect */
        _calendar = Calendar.getInstance();
    } else
        _calendar = (Calendar) _calendar.clone();

    _calendar.set(Calendar.DAY_OF_WEEK, _calendar.getFirstDayOfWeek());
    _calendar.set(Calendar.HOUR_OF_DAY, 0);
    _calendar.set(Calendar.MINUTE, 0);
    _calendar.set(Calendar.SECOND, 0);
    _calendar.set(Calendar.MILLISECOND, 0);
    return _calendar;
}

From source file:com.glaf.core.util.DateUtils.java

/**
 * /*from  w w w  .  j a v  a2s.c  om*/
 * 
 * @param date
 * @return
 */
public static Calendar getNextMonday(Calendar date) {
    Calendar result = date;
    do {
        result = (Calendar) result.clone();
        result.add(Calendar.DATE, 1);
    } while (result.get(Calendar.DAY_OF_WEEK) != 2);
    return result;
}