List of usage examples for org.joda.time Days daysBetween
public static Days daysBetween(ReadablePartial start, ReadablePartial end)
Days
representing the number of whole days between the two specified partial datetimes. From source file:org.jahia.taglibs.utility.DateDiffTag.java
License:Open Source License
/** * Default processing of the end tag returning EVAL_PAGE. * * @return EVAL_PAGE//w w w . j a v a 2 s .c o m * @throws javax.servlet.jsp.JspException if an error occurred while processing this tag * @see javax.servlet.jsp.tagext.Tag#doEndTag */ @Override public int doEndTag() throws JspException { try { if ("years".equals(format)) { int years = Years.yearsBetween(new DateTime(startDate), new DateTime(endDate)).getYears(); if (getVar() != null) { pageContext.setAttribute(getVar(), new Integer(years), scope); } else { pageContext.getOut().print(years); } } else if ("months".equals(format)) { int months = Months.monthsBetween(new DateTime(startDate), new DateTime(endDate)).getMonths(); if (getVar() != null) { pageContext.setAttribute(getVar(), new Integer(months), scope); } else { pageContext.getOut().print(months); } } else if ("days".equals(format)) { int days = Days.daysBetween(new DateTime(startDate), new DateTime(endDate)).getDays(); if (getVar() != null) { pageContext.setAttribute(getVar(), new Integer(days), scope); } else { pageContext.getOut().print(days); } } } catch (IOException e) { throw new JspException(e); } return SKIP_BODY; }
From source file:org.jasig.cas.adaptors.ldap.LdapPasswordPolicyEnforcer.java
License:Apache License
/** * Calculates the number of days left to the expiration date based on the * {@code expireDate} parameter.//from ww w . j a v a 2 s. c om * @param expireDate * @param userId * @return number of days left to the expiration date, or {@value #PASSWORD_STATUS_PASS} */ private long getDaysToExpirationDate(final String userId, final DateTime expireDate) throws LdapPasswordPolicyEnforcementException { log.debug("Calculating number of days left to the expiration date for user {}", userId); final DateTime currentTime = new DateTime(DEFAULT_TIME_ZONE); log.info("Current date is {}, expiration date is {}", currentTime.toString(), expireDate.toString()); final Days d = Days.daysBetween(currentTime, expireDate); int daysToExpirationDate = d.getDays(); if (expireDate.equals(currentTime) || expireDate.isBefore(currentTime)) { String msgToLog = "Authentication failed because account password has expired with " + daysToExpirationDate + " to expiration date. "; msgToLog += "Verify the value of the " + this.dateAttribute + " attribute and make sure it's not before the current date, which is " + currentTime.toString(); final LdapPasswordPolicyEnforcementException exc = new LdapPasswordPolicyEnforcementException(msgToLog); log.error(msgToLog, exc); throw exc; } /* * Warning period begins from X number of ways before the expiration date */ DateTime warnPeriod = new DateTime(DateTime.parse(expireDate.toString()), DEFAULT_TIME_ZONE); warnPeriod = warnPeriod.minusDays(this.warningDays); log.info("Warning period begins on {}", warnPeriod.toString()); if (this.warnAll) { log.info("Warning all. The password for {} will expire in {} days.", userId, daysToExpirationDate); } else if (currentTime.equals(warnPeriod) || currentTime.isAfter(warnPeriod)) { log.info("Password will expire in {} days.", daysToExpirationDate); } else { log.info("Password is not expiring. {} days left to the warning", daysToExpirationDate); daysToExpirationDate = PASSWORD_STATUS_PASS; } return daysToExpirationDate; }
From source file:org.jasig.cas.adaptors.ldap.lppe.LPPEAuthenticationHandler.java
License:Apache License
/** * Calculates the number of days left to the expiration date. * @return Number of days left to the expiration date or -1 if the no expiration warning is * calculated based on the defined policy. *//*from ww w . ja va 2 s .co m*/ protected int getDaysToExpirationDate(final DateTime expireDate, final PasswordPolicyResult result) throws LoginException { final DateTimeZone timezone = configuration.getDateConverter().getTimeZone(); final DateTime currentTime = new DateTime(timezone); logger.debug("Current date is {}. Expiration date is {}", currentTime, expireDate); final Days d = Days.daysBetween(currentTime, expireDate); int daysToExpirationDate = d.getDays(); logger.debug("[{}] days left to the expiration date.", daysToExpirationDate); if (expireDate.equals(currentTime) || expireDate.isBefore(currentTime)) { final String msgToLog = String.format("Password expiration date %s is on/before the current time %s.", daysToExpirationDate, currentTime); logger.debug(msgToLog); throw new CredentialExpiredException(msgToLog); } // Warning period begins from X number of days before the expiration date final DateTime warnPeriod = new DateTime(DateTime.parse(expireDate.toString()), timezone) .minusDays(result.getPasswordWarningNumberOfDays()); logger.debug("Warning period begins on [{}]", warnPeriod); if (configuration.isAlwaysDisplayPasswordExpirationWarning()) { logger.debug("Warning all. The password for [{}] will expire in [{}] day(s).", result.getDn(), daysToExpirationDate); } else if (currentTime.equals(warnPeriod) || currentTime.isAfter(warnPeriod)) { logger.debug("Password will expire in [{}] day(s)", daysToExpirationDate); } else { logger.debug("Password is not expiring. [{}] day(s) left to the warning.", daysToExpirationDate); daysToExpirationDate = -1; } return daysToExpirationDate; }
From source file:org.jasig.cas.authentication.support.DefaultAccountStateHandler.java
License:Apache License
/** * Handle an account state warning produced by ldaptive account state machinery. * <p>/* w w w . ja v a 2s.c o m*/ * Override this method to provide custom warning message handling. * * @param warning the account state warning messages. * @param response Ldaptive authentication response. * @param configuration Password policy configuration. * @param messages Container for messages produced by account state warning handling. */ protected void handleWarning(final AccountState.Warning warning, final AuthenticationResponse response, final LdapPasswordPolicyConfiguration configuration, final List<MessageDescriptor> messages) { logger.debug("Handling warning {}", warning); if (warning == null) { logger.debug("Account state warning not defined"); return; } final Calendar expDate = warning.getExpiration(); final Days ttl = Days.daysBetween(Instant.now(), new Instant(expDate)); logger.debug("Password expires in {} days. Expiration warning threshold is {} days.", ttl.getDays(), configuration.getPasswordWarningNumberOfDays()); if (configuration.isAlwaysDisplayPasswordExpirationWarning() || ttl.getDays() < configuration.getPasswordWarningNumberOfDays()) { messages.add(new PasswordExpiringWarningMessageDescriptor( "Password expires in {0} days. Please change your password at <href=\"{1}\">{1}</a>", ttl.getDays(), configuration.getPasswordPolicyUrl())); } if (warning.getLoginsRemaining() > 0) { messages.add(new DefaultMessageDescriptor("password.expiration.loginsRemaining", "You have {0} logins remaining before you MUST change your password.", warning.getLoginsRemaining())); } }
From source file:org.jbpm.console.ng.ht.backend.server.TaskCalendarServiceImpl.java
License:Apache License
private int getNumberOfDaysWithinDateRange(LocalDate dayFrom, LocalDate dayTo) { Days daysBetween = Days.daysBetween(dayFrom, dayTo); return daysBetween.getDays() + 1; }
From source file:org.jhub1.agent.statistics.SmartDateDisplay.java
License:Open Source License
public static String showHowLong(DateTime rDate) { StringBuilder sb = new StringBuilder(); DateTime dt = new DateTime(); Weeks w = Weeks.weeksBetween(rDate, dt); Days d = Days.daysBetween(rDate, dt); Hours h = Hours.hoursBetween(rDate, dt); Minutes m = Minutes.minutesBetween(rDate, dt); Seconds s = Seconds.secondsBetween(rDate, dt); if (s.getSeconds() != 0) { sb.append(s.getSeconds() % 60).append("s"); }/*from ww w .j a v a 2 s.co m*/ if (m.getMinutes() != 0) { sb.insert(0, (m.getMinutes() % 60) + "m "); } if (h.getHours() != 0) { sb.insert(0, (h.getHours() % 24) + "h "); } if (d.getDays() != 0) { sb.insert(0, (d.getDays() % 7) + "d "); } if (w.getWeeks() != 0) { sb.insert(0, w.getWeeks() + "w "); } return sb.toString(); }
From source file:org.jimcat.gui.histogram.image.DateTakenDimension.java
License:Open Source License
/** * this methode will reload statistical information from the library * //www.j ava 2 s . com * @param library */ private void reloadValues(ImageLibrary library) { // get images List<Image> images = new ArrayList<Image>(library.getAll()); // 1) find min / max date int i = 0; DateTime min = null; DateTime max = null; while (min == null && i < images.size()) { min = getDateTaken(images.get(i)); max = min; i++; } if (min == null || max == null) { // result is an empty vector values = new int[RESOLUTION_COUNT][0]; maxValues = new int[RESOLUTION_COUNT]; minDate = null; fireStructureChangedEvent(); return; } // iterate through images for (; i < images.size(); i++) { Image img = images.get(i); DateTime date = getDateTaken(img); if (date != null && min.isAfter(date)) { min = date; } if (date != null && max.isBefore(date)) { max = date; } } // save min dates // get references // must be a pure day DateTime dayRef = new DateTime(2000, 1, 1, 0, 0, 0, 0); // must be a monday DateTime weekRef = new DateTime(2000, 1, 3, 0, 0, 0, 0); // must be the first of any month DateTime monthRef = dayRef; minDate = new DateTime[RESOLUTION_COUNT]; minDate[DAYS] = dayRef.plusDays(Days.daysBetween(dayRef, min).getDays()); minDate[WEEKS] = weekRef.plusWeeks(Weeks.weeksBetween(weekRef, min).getWeeks()); minDate[MONTHS] = monthRef.plusMonths(Months.monthsBetween(monthRef, min).getMonths()); // 2) build arrays values = new int[RESOLUTION_COUNT][]; values[DAYS] = new int[Days.daysBetween(minDate[DAYS], max).getDays() + 1]; values[WEEKS] = new int[Weeks.weeksBetween(minDate[WEEKS], max).getWeeks() + 1]; values[MONTHS] = new int[Months.monthsBetween(minDate[MONTHS], max).getMonths() + 1]; // 3) fill in values for (Image img : images) { // extract date DateTime date = getDateTaken(img); if (date != null) { values[DAYS][Days.daysBetween(minDate[DAYS], date).getDays()]++; values[WEEKS][Weeks.weeksBetween(minDate[WEEKS], date).getWeeks()]++; values[MONTHS][Months.monthsBetween(minDate[MONTHS], date).getMonths()]++; } } // 4) get max values maxValues = new int[RESOLUTION_COUNT]; for (int j = 0; j < RESOLUTION_COUNT; j++) { maxValues[j] = values[j][0]; for (int k = 1; k < values[j].length; k++) { if (maxValues[j] < values[j][k]) { maxValues[j] = values[j][k]; } } } // 6) notify listners fireStructureChangedEvent(); }
From source file:org.jimcat.gui.histogram.image.DateTakenDimension.java
License:Open Source License
/** * converte a given resolution / date pair to a index * /* w ww. java2s . c om*/ * @param resolution * @param date * @return the converted index */ private int dateToIndex(int resolution, DateTime date) { if (date == null || minDate == null) { return 0; } // convert to new resolution switch (resolution) { case DAYS: return Days.daysBetween(minDate[DAYS], date).getDays(); case WEEKS: return Weeks.weeksBetween(minDate[WEEKS], date).getWeeks(); default: return Months.monthsBetween(minDate[MONTHS], date).getMonths(); } }
From source file:org.jimcat.model.filter.metadata.RelativeDateFilter.java
License:Open Source License
/** * /*from www.j a v a2 s . co m*/ * @param time * @return the value to compare depending on time unit set */ private int getCompareValue(DateTime time) { DateTime now = new DateTime(); switch (timeUnit) { case DAYS: return Days.daysBetween(time, now).getDays(); case WEEKS: return Weeks.weeksBetween(time, now).getWeeks(); case MONTHS: return Months.monthsBetween(time, now).getMonths(); } return -1; }
From source file:org.jobscheduler.dashboard.web.rest.SchedulerHistoryResource.java
License:Apache License
/** * Build one serie with number of jobs between startDate and endDate (job in * error or not)/*from w w w . j a v a 2 s.co m*/ * * @param startTime * @param endTime * @param model * @return */ @RequestMapping("/nbJobsBetween2Date") @ApiOperation(value = "Build one serie with number of jobs between startDate and endDate") public @ResponseBody List<SerieDTO> nbJobsBetween2Date( @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy/MM/dd") DateTime startChartDT, @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy/MM/dd") DateTime endChartDT, @RequestParam(value = "jobInError", required = false) Boolean jobInError, Model model) { if ((startChartDT == null) || (endChartDT == null)) return null; Days days = Days.daysBetween(startChartDT, endChartDT); if (days.getDays() < 0) return new ArrayList<SerieDTO>(0); // Create 1 serie with point per day List<SerieDTO> series = new ArrayList<SerieDTO>(1); SerieDTO serie = new SerieDTO(); List<PointDTO> points = new ArrayList<PointDTO>(days.getDays() + 1); for (int i = 0; i < days.getDays() + 1; i++) { PointDTO point = new PointDTO(); point.setX(startChartDT.plusDays(i).getMillis()); Long nbJobs; if (jobInError) { nbJobs = schedulerHistoryRepository.countByStartTimeBetweenAndError( new Timestamp(startChartDT.plusDays(i).getMillis()), new Timestamp(startChartDT.plusDays(i + 1).getMillis()), new BigDecimal(1)); } else { nbJobs = schedulerHistoryRepository.countByStartTimeBetween( new Timestamp(startChartDT.plusDays(i).getMillis()), new Timestamp(startChartDT.plusDays(i + 1).getMillis())); } point.setY(nbJobs); points.add(point); } String inError = ""; if (jobInError) inError = "in Error"; serie.setKey("Number of jobs " + inError + " between " + startChartDT.toString(fmt) + " and " + endChartDT.toString(fmt)); serie.setValues(points); series.add(serie); return series; }