List of usage examples for java.util Calendar clone
@Override
public Object clone()
From source file:org.jahia.utils.DateUtils.java
/** * Returns the end of the day (23:59:59:999) for the specified date. * //from w ww .ja va2 s . c o m * @param date the date to be processed * @return the end of the day (23:59:59:999) for the specified date */ public static Calendar dayEnd(Calendar date) { Calendar c = (Calendar) date.clone(); c.set(Calendar.HOUR_OF_DAY, c.getMaximum(Calendar.HOUR_OF_DAY)); c.set(Calendar.MINUTE, c.getMaximum(Calendar.MINUTE)); c.set(Calendar.SECOND, c.getMaximum(Calendar.SECOND)); c.set(Calendar.MILLISECOND, c.getMaximum(Calendar.MILLISECOND)); return c; }
From source file:org.jahia.utils.DateUtils.java
/** * Returns the start of the day (00:00:00:000) for the specified date. * /*w w w .j av a 2 s.c o m*/ * @param date the date to be processed * @return the start of the day (00:00:00:000) for the specified date */ public static Calendar dayStart(Calendar date) { Calendar c = (Calendar) date.clone(); c.set(Calendar.HOUR_OF_DAY, c.getMinimum(Calendar.HOUR_OF_DAY)); c.set(Calendar.MINUTE, c.getMinimum(Calendar.MINUTE)); c.set(Calendar.SECOND, c.getMinimum(Calendar.SECOND)); c.set(Calendar.MILLISECOND, c.getMinimum(Calendar.MILLISECOND)); return c; }
From source file:Main.java
/** * Add the given number of days to the calendar and convert to Date. * * @param calendar/*from w w w . ja v a2s. c o m*/ * The calendar to add to. * @param days * The number of days to add. * @return The date object given by the modified calendar. */ private static Date rollGetDate(Calendar calendar, int days) { Calendar easterSunday = (Calendar) calendar.clone(); easterSunday.add(Calendar.DATE, days); return easterSunday.getTime(); }
From source file:Main.java
static int daysBetween(Calendar day1, Calendar day2) { /**/*from w w w . j a v a 2 s .c o m*/ * Saved some effort using the solution described here, * http://stackoverflow.com/a/28865648/1587370 */ Calendar dayOne = (Calendar) day1.clone(), dayTwo = (Calendar) day2.clone(); if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) { return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR)); } else { if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) { //swap them Calendar temp = dayOne; dayOne = dayTwo; dayTwo = temp; } int extraDays = 0; int dayOneOriginalYearDays = dayOne.get(Calendar.DAY_OF_YEAR); while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) { dayOne.add(Calendar.YEAR, -1); // getActualMaximum() important for leap years extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR); } return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays; } }
From source file:CalendarUtilsTest.java
/** * This checks that this is a 7 element iterator of Calendar objects * that are dates (no time), and exactly 1 day spaced after each other. *//*from w ww. j a v a 2 s. c o m*/ private static void assertWeekIterator(Iterator it, Calendar start) { Calendar end = (Calendar) start.clone(); end.add(Calendar.DATE, 6); assertWeekIterator(it, start, end); }
From source file:org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils.java
/** * Returns the end of day from the calendar object. *//*from w w w. j a v a 2 s. c om*/ public static Calendar endOfDayDate(Calendar calendar) { Calendar cal = (Calendar) calendar.clone(); cal = DateUtils.ceiling(cal, Calendar.DATE); cal.add(Calendar.MILLISECOND, -1); return cal; }
From source file:nl.strohalm.cyclos.utils.NamedPeriod.java
/** * Makes a defaultperiod for the form. It calculates the last quarter which was finished via the getLastQuarter() method. Then it calculates the * starting and ending day for this period. However, if the needed quarter is the last of a year, then it returns a whole year, in stead of a * whole quarter. In this case, the enddate is the same as it would have been with the quarter. The name of the Period is generated via the * produceName method/*w w w . j a v a 2 s. co m*/ * @param aDate returns the last completed quarter which falls before this date. So if this date is january 1st, the method returns quarter 4 of * the previous year (octobre 1st to decembre 31st) * @return a quarterly period * @see #getLastQuarter() * @see #produceName(int, int) */ public static NamedPeriod getQuarterPeriod(final Calendar aDate) { int year = aDate.get(Calendar.YEAR); final int quarter = getLastQuarter(aDate); final int endMonth = (3 * quarter) - 3; final Calendar endDay = new GregorianCalendar(year, endMonth, 1, 0, 0, 0); final Calendar startDay = (Calendar) endDay.clone(); if (quarter == 1) { startDay.add(Calendar.YEAR, -1); year--; } else { startDay.add(Calendar.MONTH, -3); } endDay.add(Calendar.MILLISECOND, -1); // do not include the endday, by distracting one millisecond from the first of next month final NamedPeriod period = new NamedPeriod(startDay, endDay, produceName(year, quarter)); return period; }
From source file:org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils.java
/** * Converts the time (hour.minute) to a calendar object. *///from w ww . j a va 2 s . c o m public static Calendar timeToCalendar(Calendar calendar, double time) { if (time < 0.0) { return null; } Calendar cal = (Calendar) calendar.clone(); int hour = 0; int minute = 0; if (time == 24.0) { cal.add(Calendar.DAY_OF_MONTH, 1); } else { hour = (int) time; minute = (int) ((time * 100) - (hour * 100)); } cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); return DateUtils.truncate(cal, Calendar.MINUTE); }
From source file:com.espertech.esper.epl.datetime.calop.CalendarOpPlusFastAddHelper.java
public static CalendarOpPlusFastAddResult computeNextDue(long currentTime, TimePeriod timePeriod, Calendar reference) { if (reference.getTimeInMillis() > currentTime) { return new CalendarOpPlusFastAddResult(0, reference); }/*from ww w . ja va2 s .c o m*/ // add one time period Calendar work = (Calendar) reference.clone(); if (DEBUG && log.isDebugEnabled()) { log.debug("Work date is " + DateTime.print(work)); } CalendarOpPlusMinus.actionSafeOverflow(work, 1, timePeriod); long inMillis = work.getTimeInMillis(); if (inMillis > currentTime) { return new CalendarOpPlusFastAddResult(1, work); } if (DEBUG && log.isDebugEnabled()) { log.debug("Work date is " + DateTime.print(work)); } long factor = 1; // determine multiplier long deltaCurrentToStart = currentTime - reference.getTimeInMillis(); long deltaAddedOne = work.getTimeInMillis() - reference.getTimeInMillis(); double multiplierDbl = (deltaCurrentToStart / deltaAddedOne) - 1; long multiplierRoundedLong = (long) multiplierDbl; // handle integer max while (multiplierRoundedLong > Integer.MAX_VALUE) { CalendarOpPlusMinus.actionSafeOverflow(work, Integer.MAX_VALUE, timePeriod); factor += Integer.MAX_VALUE; multiplierRoundedLong -= Integer.MAX_VALUE; if (DEBUG && log.isDebugEnabled()) { log.debug("Work date is " + DateTime.print(work) + " factor " + factor); } } // add int multiplierRoundedInt = (int) multiplierRoundedLong; CalendarOpPlusMinus.actionSafeOverflow(work, multiplierRoundedInt, timePeriod); factor += multiplierRoundedInt; // if below, add more if (work.getTimeInMillis() <= currentTime) { while (work.getTimeInMillis() <= currentTime) { CalendarOpPlusMinus.actionSafeOverflow(work, 1, timePeriod); factor += 1; if (DEBUG && log.isDebugEnabled()) { log.debug("Work date is " + DateTime.print(work) + " factor " + factor); } } return new CalendarOpPlusFastAddResult(factor, work); } // we are over while (work.getTimeInMillis() > currentTime) { CalendarOpPlusMinus.actionSafeOverflow(work, -1, timePeriod); factor -= 1; if (DEBUG && log.isDebugEnabled()) { log.debug("Work date is " + DateTime.print(work) + " factor " + factor); } } CalendarOpPlusMinus.actionSafeOverflow(work, 1, timePeriod); if (DEBUG && log.isDebugEnabled()) { log.debug("Work date is " + DateTime.print(work) + " factor " + factor); } return new CalendarOpPlusFastAddResult(factor + 1, work); }
From source file:Main.java
/** * <p>Truncate this date, leaving the field specified as the most * significant field.</p>//from w ww.ja v a2 s. c om * * <p>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.</p> * * @param date the date to work with * @param field the field from <code>Calendar</code> * or <code>SEMI_MONTH</code> * @return the rounded date (a different object) * @throws IllegalArgumentException if the date is <code>null</code> * @throws ArithmeticException if the year is over 280 million */ public static Calendar truncate(Calendar date, int field) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar truncated = (Calendar) date.clone(); modify(truncated, field, false); return truncated; }