/** * Gets the number of days in between this calendar date and the specified * calendar date. If this calendar date represents a day after the * specified calendar date, the difference will be positive. If this * calendar date represents a day before the specified calendar date, the * difference will be negative. If the two calendar dates represent the * same day, the difference will be zero. * <p> * More formally, it is defined that given calendar dates <tt>c1</tt> and * <tt>c2</tt>, the following will return <tt>true</tt>: * <pre> * c1.add(c2.subtract(c1)).equals(c2); * </pre> * * @param calendarDate * The calendar date to subtract from this calendar date. * * @return * The number of days in between this calendar date and * <tt>calendarDate</tt>. */ public int subtract(CalendarDate calendarDate) { GregorianCalendar c1 = toCalendar(); GregorianCalendar c2 = calendarDate.toCalendar(); long t1 = c1.getTimeInMillis(); long t2 = c2.getTimeInMillis(); return (int)((t1 - t2) / (1000l * 60 * 60 * 24)); }
/** * Gets the number of days in between this calendar date and the specified * calendar date. If this calendar date represents a day after the * specified calendar date, the difference will be positive. If this * calendar date represents a day before the specified calendar date, the * difference will be negative. If the two calendar dates represent the * same day, the difference will be zero. * <p> * More formally, it is defined that given calendar dates <tt>c1</tt> and * <tt>c2</tt>, the following will return <tt>true</tt>: * <pre> * c1.add(c2.subtract(c1)).equals(c2); * </pre> * * @param calendarDate * The calendar date to subtract from this calendar date. * * @return * The number of days in between this calendar date and * <tt>calendarDate</tt>. */ public int subtract(CalendarDate calendarDate) { GregorianCalendar c1 = toCalendar(); GregorianCalendar c2 = calendarDate.toCalendar(); long t1 = c1.getTimeInMillis(); long t2 = c2.getTimeInMillis(); return (int)((t1 - t2) / (1000l * 60 * 60 * 24)); }
/** * Translates this calendar date to an instance of * <tt>GregorianCalendar</tt>, with the <tt>year</tt>, <tt>month</tt>, and * <tt>dayOfMonth</tt> fields set in the default time zone with the default * locale. * * @return * This calendar date as a <tt>GregorianCalendar</tt>. */ public GregorianCalendar toCalendar() { return toCalendar(new Time(0, 0, 0)); }
/** * Translates this calendar date to an instance of * <tt>GregorianCalendar</tt>, with the <tt>year</tt>, <tt>month</tt>, and * <tt>dayOfMonth</tt> fields set in the default time zone with the default * locale. * * @return * This calendar date as a <tt>GregorianCalendar</tt>. */ public GregorianCalendar toCalendar() { return toCalendar(new Time(0, 0, 0)); }
@Override public String toString(Object row, String columnName) { Object cellData = JSON.get(row, columnName); String string; if (cellData instanceof Date) { string = dateFormat.format((Date)cellData); } else if (cellData instanceof Long) { string = dateFormat.format(new Date((Long)cellData)); } else if (cellData instanceof Calendar) { string = dateFormat.format(((Calendar)cellData).getTime()); } else if (cellData instanceof CalendarDate) { string = dateFormat.format(((CalendarDate)cellData).toCalendar().getTime()); } else { string = (cellData == null) ? null : cellData.toString(); } return string; } }
@Override public String toString(Object row, String columnName) { Object cellData = JSON.get(row, columnName); String string; if (cellData instanceof Date) { string = dateFormat.format((Date)cellData); } else if (cellData instanceof Long) { string = dateFormat.format(new Date((Long)cellData)); } else if (cellData instanceof Calendar) { string = dateFormat.format(((Calendar)cellData).getTime()); } else if (cellData instanceof CalendarDate) { string = dateFormat.format(((CalendarDate)cellData).toCalendar().getTime()); } else { string = (cellData == null) ? null : cellData.toString(); } return string; } }
@Override public void render(Object data, Button button, boolean highlight) { CalendarButton calendarButton = (CalendarButton)button; Locale locale = calendarButton.getLocale(); if (data == null) { data = ""; } else { if (data instanceof CalendarDate) { CalendarDate date = (CalendarDate)data; DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); data = dateFormat.format(date.toCalendar().getTime()); } } super.render(data, button, highlight); } }
/** * Adds the specified number of days to this calendar date and returns the * resulting calendar date. The number of days may be negative, in which * case the result will be a date before this calendar date. * <p> * More formally, it is defined that given calendar dates <tt>c1</tt> and * <tt>c2</tt>, the following will return <tt>true</tt>: * <pre> * c1.add(c2.subtract(c1)).equals(c2); * </pre> * * @param days * The number of days to add to (or subtract from if negative) this * calendar date. * * @return * The resulting calendar date. */ public CalendarDate add(int days) { GregorianCalendar calendar = toCalendar(); calendar.add(Calendar.DAY_OF_YEAR, days); return new CalendarDate(calendar); }
/** * Adds the specified number of days to this calendar date and returns the * resulting calendar date. The number of days may be negative, in which * case the result will be a date before this calendar date. * <p> * More formally, it is defined that given calendar dates <tt>c1</tt> and * <tt>c2</tt>, the following will return <tt>true</tt>: * <pre> * c1.add(c2.subtract(c1)).equals(c2); * </pre> * * @param days * The number of days to add to (or subtract from if negative) this * calendar date. * * @return * The resulting calendar date. */ public CalendarDate add(int days) { GregorianCalendar calendar = toCalendar(); calendar.add(Calendar.DAY_OF_YEAR, days); return new CalendarDate(calendar); }
@Override public void render(final Object data, final Button button, boolean highlight) { Object dataMutable = data; CalendarButton calendarButton = (CalendarButton)button; Locale locale = calendarButton.getLocale(); if (dataMutable == null) { dataMutable = ""; } else { if (dataMutable instanceof CalendarDate) { CalendarDate date = (CalendarDate)dataMutable; DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); dataMutable = dateFormat.format(date.toCalendar().getTime()); } } super.render(dataMutable, button, highlight); } }