/** * 统计两个日期之间包含的天数。包含date1,但不包含date2 * * @param date1 * @param date2 * @return */ public static int getDayDiff(Date date1, Date date2) { if (date1 == null || date2 == null) { throw new InvalidParameterException("date1 and date2 cannot be null!"); } Date startDate = org.apache.commons.lang3.time.DateUtils.truncate(date1, Calendar.DATE); Date endDate = org.apache.commons.lang3.time.DateUtils.truncate(date2, Calendar.DATE); if (startDate.after(endDate)) { throw new InvalidParameterException("date1 cannot be after date2!"); } long millSecondsInOneDay = HOURS_IN_A_DAY * MINUTES_IN_AN_HOUR * SECONDS_IN_A_MINUTE * MILLIS_IN_A_SECOND; return (int) ((endDate.getTime() - startDate.getTime()) / millSecondsInOneDay); }
public String getStyleForDate(int year, int month, int date, TimeZone timeZone) { String cellStyle = null; if (timeZone == null) { timeZone = getTimeZone(); } GregorianCalendar dateCompareCalendar = new GregorianCalendar(timeZone); dateCompareCalendar.set(GregorianCalendar.YEAR, year); dateCompareCalendar.set(GregorianCalendar.MONTH, month); dateCompareCalendar.set(GregorianCalendar.DATE, date); dateCompareCalendar = (GregorianCalendar) DateUtils.truncate(dateCompareCalendar, GregorianCalendar.DATE); for (Entry<Long, String> entryCellStyle : getCellStyles().entrySet()) { GregorianCalendar cellStyleCalendar = new GregorianCalendar(timeZone); cellStyleCalendar.setTimeInMillis(entryCellStyle.getKey()); // If selected month and year equals timestamp of cell style if (DateUtils.isSameDay(dateCompareCalendar, cellStyleCalendar)) { cellStyle = entryCellStyle.getValue(); } } return cellStyle; }
/** * This method returns all events for a specified date (year, month, date)<br> * * @param year e.g. 2010 * @param month e.g. 11 for November * @param date e.g. 12 * @return Collection of all events, which are planned for the specified date. */ public Collection<CalendarEvent> getAllEventsForDate( int year, int month, int date, TimeZone timeZone) { // Get all events Collection<CalendarEvent> events = getCalendarEvents(); Collection<CalendarEvent> dateEvents = new ArrayList<CalendarEvent>(); GregorianCalendar dateCompareCalendar = new GregorianCalendar(timeZone); dateCompareCalendar.set(GregorianCalendar.YEAR, year); dateCompareCalendar.set(GregorianCalendar.MONTH, month); dateCompareCalendar.set(GregorianCalendar.DATE, date); dateCompareCalendar.set(GregorianCalendar.HOUR_OF_DAY, 00); dateCompareCalendar = (GregorianCalendar) DateUtils.truncate(dateCompareCalendar, GregorianCalendar.HOUR_OF_DAY); Iterator<CalendarEvent> eventsIter = events.iterator(); while (eventsIter.hasNext()) { CalendarEvent event = eventsIter.next(); GregorianCalendar eventCalendar = new GregorianCalendar(timeZone); eventCalendar.setTimeInMillis(event.getTimestampInMillis()); // If selected month and year equals event in collection if (DateUtils.isSameDay(dateCompareCalendar, eventCalendar)) { dateEvents.add(event); } } // TODO Sort per hours return dateEvents; }
@JsonProperty(value = "accountExpired") public boolean isAccountExpired() { if (!accountStatus.isPending()) { Date today = DateUtils.truncate(new Date(), Calendar.DATE); return ((getAccountExpirationDate() != null) && !today.before(getAccountExpirationDate())); } return false; }
@JsonProperty(value = "passwordExpired") public boolean isPasswordExpired() { Date passwordEpirationDate = getPasswordExpirationDate(); if (passwordEpirationDate != null) { Date today = DateUtils.truncate(new Date(), Calendar.DATE); return !today.before(passwordEpirationDate); } return false; }
@JsonProperty(value = "passwordExpirationDate") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss") public Date getPasswordExpirationDate() { if (!accountStatus.isPending() && (getPasswordLastModifiedDate() != null) && (getPasswordExpirationInterval() != null)) { return DateUtils.addDays( DateUtils.truncate(getPasswordLastModifiedDate(), Calendar.DATE), getPasswordExpirationInterval()); } return null; }
// TOOLS private DateTime getDateLimit(final boolean upper) { final Date date = DateUtils.truncate(new Date(), Calendar.DATE); final DateTime now = new DateTime(date); switch (type) { case YEAR: return upper ? now.plusYears(upperBoundary) : now.minusYears(lowerBoundary); case MONTH: return upper ? now.plusMonths(upperBoundary) : now.minusMonths(lowerBoundary); case DAY: return upper ? now.plusDays(upperBoundary) : now.minusDays(lowerBoundary); default: throw new IllegalArgumentException("Unsupported type of time unit: " + type); } }
// 各日付に記事があるか否かを判定 public boolean isArticle(int day) { for (Article article : this.result) { // 対象日 Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(this.year, this.month - 1, day, 0, 0, 0); Date selectedDay = cal.getTime(); // 記事の書かれた日 Date date = article.created_at; Date createdDay = DateUtils.truncate(date, Calendar.DAY_OF_MONTH); if (createdDay.equals(selectedDay)) { return true; } } return false; }
/** * 判断date1是否在date2之后,忽略时间部分 * * @param date1 * @param date2 * @return */ public static boolean isDateAfter(Date date1, Date date2) { Date theDate1 = org.apache.commons.lang3.time.DateUtils.truncate(date1, Calendar.DATE); Date theDate2 = org.apache.commons.lang3.time.DateUtils.truncate(date2, Calendar.DATE); return theDate1.after(theDate2); }
private boolean initializationNeeded(Date lastSystemRun) { Date dueDate = DateUtils.addHours(DateUtils.truncate(new Date(), Calendar.MONTH), -2); return (lastSystemRun == null || lastSystemRun.before(dueDate)); }
public static Date endOfDay(Date d) { return DateUtils.addSeconds(DateUtils.addDays(DateUtils.truncate(d, Calendar.DATE), 1), -1); }