List of usage examples for org.joda.time LocalDate minusMonths
public LocalDate minusMonths(int months)
From source file:com.google.api.ads.adwords.awalerting.util.DateRange.java
License:Open Source License
/** * Parse DateRange in ReportDefinitionDateRangeType enum format. *//*from w ww. j ava 2 s. co m*/ private static DateRange parseEnumFormat(String dateRange) { ReportDefinitionDateRangeType dateRangeType; try { dateRangeType = ReportDefinitionDateRangeType.valueOf(dateRange); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unknown DateRange type: " + dateRange); } LocalDate today = LocalDate.now(); LocalDate startDate; LocalDate endDate; switch (dateRangeType) { case TODAY: startDate = endDate = today; break; case YESTERDAY: startDate = endDate = today.minusDays(1); break; case LAST_7_DAYS: startDate = today.minusDays(7); endDate = today.minusDays(1); break; case LAST_WEEK: LocalDate.Property lastWeekProp = today.minusWeeks(1).dayOfWeek(); startDate = lastWeekProp.withMinimumValue(); endDate = lastWeekProp.withMaximumValue(); break; case THIS_MONTH: LocalDate.Property thisMonthProp = today.dayOfMonth(); startDate = thisMonthProp.withMinimumValue(); endDate = thisMonthProp.withMaximumValue(); break; case LAST_MONTH: LocalDate.Property lastMonthProp = today.minusMonths(1).dayOfMonth(); startDate = lastMonthProp.withMinimumValue(); endDate = lastMonthProp.withMaximumValue(); break; case LAST_14_DAYS: startDate = today.minusDays(14); endDate = today.minusDays(1); break; case LAST_30_DAYS: startDate = today.minusDays(30); endDate = today.minusDays(1); break; case THIS_WEEK_SUN_TODAY: // Joda-Time uses the ISO standard Monday to Sunday week. startDate = today.minusWeeks(1).dayOfWeek().withMaximumValue(); endDate = today; break; case THIS_WEEK_MON_TODAY: startDate = today.dayOfWeek().withMinimumValue(); endDate = today; break; case LAST_WEEK_SUN_SAT: startDate = today.minusWeeks(2).dayOfWeek().withMaximumValue(); endDate = today.minusWeeks(1).dayOfWeek().withMaximumValue().minusDays(1); break; // Don't support the following enums case LAST_BUSINESS_WEEK: case ALL_TIME: case CUSTOM_DATE: default: throw new IllegalArgumentException("Unsupported DateRange type: " + dateRange); } return new DateRange(startDate, endDate); }
From source file:com.google.api.ads.adwords.awreporting.model.entities.DateRangeAndType.java
License:Open Source License
/** * Parse DateRange in ReportDefinitionDateRangeType enum format. *///www . j av a2s . c o m private static DateRangeAndType parseEnumFormat(ReportDefinitionDateRangeType type) { LocalDate today = LocalDate.now(); LocalDate startDate; LocalDate endDate; switch (type) { case TODAY: startDate = endDate = today; break; case YESTERDAY: startDate = endDate = today.minusDays(1); break; case LAST_7_DAYS: startDate = today.minusDays(7); endDate = today.minusDays(1); break; case LAST_WEEK: LocalDate.Property lastWeekProp = today.minusWeeks(1).dayOfWeek(); startDate = lastWeekProp.withMinimumValue(); endDate = lastWeekProp.withMaximumValue(); break; case THIS_MONTH: LocalDate.Property thisMonthProp = today.dayOfMonth(); startDate = thisMonthProp.withMinimumValue(); endDate = thisMonthProp.withMaximumValue(); break; case LAST_MONTH: LocalDate.Property lastMonthProp = today.minusMonths(1).dayOfMonth(); startDate = lastMonthProp.withMinimumValue(); endDate = lastMonthProp.withMaximumValue(); break; case LAST_14_DAYS: startDate = today.minusDays(14); endDate = today.minusDays(1); break; case LAST_30_DAYS: startDate = today.minusDays(30); endDate = today.minusDays(1); break; case THIS_WEEK_SUN_TODAY: // Joda-Time uses the ISO standard Monday to Sunday week. startDate = today.minusWeeks(1).dayOfWeek().withMaximumValue(); endDate = today; break; case THIS_WEEK_MON_TODAY: startDate = today.dayOfWeek().withMinimumValue(); endDate = today; break; case LAST_WEEK_SUN_SAT: startDate = today.minusWeeks(2).dayOfWeek().withMaximumValue(); endDate = today.minusWeeks(1).dayOfWeek().withMaximumValue().minusDays(1); break; // Don't support the following enums case LAST_BUSINESS_WEEK: case ALL_TIME: case CUSTOM_DATE: default: throw new IllegalArgumentException("Unsupported DateRange type: " + type.value()); } return new DateRangeAndType(startDate, endDate, type); }
From source file:com.gst.portfolio.loanaccount.loanschedule.domain.DefaultScheduledDateGenerator.java
License:Apache License
@Override public LocalDate idealDisbursementDateBasedOnFirstRepaymentDate( final PeriodFrequencyType repaymentPeriodFrequencyType, final int repaidEvery, final LocalDate firstRepaymentDate, final Calendar loanCalendar, final HolidayDetailDTO holidayDetailDTO, final LoanApplicationTerms loanApplicationTerms) { LocalDate idealDisbursementDate = null; switch (repaymentPeriodFrequencyType) { case DAYS:/*from www. ja va 2s . c o m*/ idealDisbursementDate = firstRepaymentDate.minusDays(repaidEvery); break; case WEEKS: idealDisbursementDate = firstRepaymentDate.minusWeeks(repaidEvery); break; case MONTHS: if (loanCalendar == null) { idealDisbursementDate = firstRepaymentDate.minusMonths(repaidEvery); } else { idealDisbursementDate = CalendarUtils.getNewRepaymentMeetingDate(loanCalendar.getRecurrence(), firstRepaymentDate.minusMonths(repaidEvery), firstRepaymentDate.minusMonths(repaidEvery), repaidEvery, CalendarUtils.getMeetingFrequencyFromPeriodFrequencyType(repaymentPeriodFrequencyType), holidayDetailDTO.getWorkingDays(), loanApplicationTerms.isSkipRepaymentOnFirstDayofMonth(), loanApplicationTerms.getNumberOfdays()); } break; case YEARS: idealDisbursementDate = firstRepaymentDate.minusYears(repaidEvery); break; case INVALID: break; } return idealDisbursementDate; }
From source file:com.gst.portfolio.loanaccount.loanschedule.domain.LoanApplicationTerms.java
License:Apache License
public BigDecimal calculatePeriodsBetweenDates(final LocalDate startDate, final LocalDate endDate) { BigDecimal numberOfPeriods = BigDecimal.ZERO; switch (this.repaymentPeriodFrequencyType) { case DAYS:/* w w w . j a v a 2s . c om*/ int numOfDays = Days.daysBetween(startDate, endDate).getDays(); numberOfPeriods = BigDecimal.valueOf((double) numOfDays); break; case WEEKS: int numberOfWeeks = Weeks.weeksBetween(startDate, endDate).getWeeks(); int daysLeftAfterWeeks = Days.daysBetween(startDate.plusWeeks(numberOfWeeks), endDate).getDays(); numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfWeeks)) .add(BigDecimal.valueOf((double) daysLeftAfterWeeks / 7)); break; case MONTHS: int numberOfMonths = Months.monthsBetween(startDate, endDate).getMonths(); LocalDate startDateAfterConsideringMonths = null; LocalDate endDateAfterConsideringMonths = null; int diffDays = 0; if (this.loanCalendar == null) { startDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths); startDateAfterConsideringMonths = CalendarUtils.adjustDate(startDateAfterConsideringMonths, getSeedDate(), this.repaymentPeriodFrequencyType); endDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths + 1); endDateAfterConsideringMonths = CalendarUtils.adjustDate(endDateAfterConsideringMonths, getSeedDate(), this.repaymentPeriodFrequencyType); } else { LocalDate expectedStartDate = startDate; if (!CalendarUtils.isValidRedurringDate(loanCalendar.getRecurrence(), loanCalendar.getStartDateLocalDate().minusMonths(getRepaymentEvery()), startDate)) { expectedStartDate = CalendarUtils.getNewRepaymentMeetingDate(loanCalendar.getRecurrence(), startDate.minusMonths(getRepaymentEvery()), startDate.minusMonths(getRepaymentEvery()), getRepaymentEvery(), CalendarUtils .getMeetingFrequencyFromPeriodFrequencyType(getLoanTermPeriodFrequencyType()), this.holidayDetailDTO.getWorkingDays(), isSkipRepaymentOnFirstDayOfMonth, numberOfDays); } if (!expectedStartDate.isEqual(startDate)) { diffDays = Days.daysBetween(startDate, expectedStartDate).getDays(); } if (numberOfMonths == 0) { startDateAfterConsideringMonths = expectedStartDate; } else { startDateAfterConsideringMonths = CalendarUtils.getNewRepaymentMeetingDate( loanCalendar.getRecurrence(), expectedStartDate, expectedStartDate.plusMonths(numberOfMonths), getRepaymentEvery(), CalendarUtils .getMeetingFrequencyFromPeriodFrequencyType(getLoanTermPeriodFrequencyType()), this.holidayDetailDTO.getWorkingDays(), isSkipRepaymentOnFirstDayOfMonth, numberOfDays); } endDateAfterConsideringMonths = CalendarUtils.getNewRepaymentMeetingDate( loanCalendar.getRecurrence(), startDateAfterConsideringMonths, startDateAfterConsideringMonths.plusDays(1), getRepaymentEvery(), CalendarUtils.getMeetingFrequencyFromPeriodFrequencyType(getLoanTermPeriodFrequencyType()), this.holidayDetailDTO.getWorkingDays(), isSkipRepaymentOnFirstDayOfMonth, numberOfDays); } int daysLeftAfterMonths = Days.daysBetween(startDateAfterConsideringMonths, endDate).getDays() + diffDays; int daysInPeriodAfterMonths = Days .daysBetween(startDateAfterConsideringMonths, endDateAfterConsideringMonths).getDays(); numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfMonths)) .add(BigDecimal.valueOf((double) daysLeftAfterMonths / daysInPeriodAfterMonths)); break; case YEARS: int numberOfYears = Years.yearsBetween(startDate, endDate).getYears(); LocalDate startDateAfterConsideringYears = startDate.plusYears(numberOfYears); LocalDate endDateAfterConsideringYears = startDate.plusYears(numberOfYears + 1); int daysLeftAfterYears = Days.daysBetween(startDateAfterConsideringYears, endDate).getDays(); int daysInPeriodAfterYears = Days .daysBetween(startDateAfterConsideringYears, endDateAfterConsideringYears).getDays(); numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfYears)) .add(BigDecimal.valueOf((double) daysLeftAfterYears / daysInPeriodAfterYears)); break; default: break; } return numberOfPeriods; }
From source file:com.gst.portfolio.savings.domain.SavingsAccountCharge.java
License:Apache License
public void updateToPreviousDueDate() { if (isAnnualFee() || isMonthlyFee() || isWeeklyFee()) { LocalDate nextDueLocalDate = new LocalDate(dueDate); if (isAnnualFee()) { nextDueLocalDate = nextDueLocalDate.withMonthOfYear(this.feeOnMonth).minusYears(1); nextDueLocalDate = setDayOfMonth(nextDueLocalDate); } else if (isMonthlyFee()) { nextDueLocalDate = nextDueLocalDate.minusMonths(this.feeInterval); nextDueLocalDate = setDayOfMonth(nextDueLocalDate); } else if (isWeeklyFee()) { nextDueLocalDate = nextDueLocalDate.minusDays(7 * this.feeInterval); nextDueLocalDate = setDayOfWeek(nextDueLocalDate); }//from w w w . j a va 2 s . c o m this.dueDate = nextDueLocalDate.toDate(); } }
From source file:com.mars.test.jodatime.Mars_App.java
public static void main(String[] args) { // LocalDate , TimeZone LocalDate dt = new LocalDate(); // 1.?1~31// w w w. ja va2s . co m log.info("1.?" + dt.withDayOfMonth(1).toString(pattern) + " ~ " + dt.dayOfMonth().withMaximumValue().toString(pattern)); // 2.?26~25(????) log.info("2.?" + dt.minusMonths(1).withDayOfMonth(26).toString(pattern) + " ~ " + dt.withDayOfMonth(25).toString(pattern)); // 3.??? LocalDate date2 = dt.withDayOfMonth(5); if (date2.getDayOfWeek() == DateTimeConstants.SATURDAY || date2.getDayOfWeek() == DateTimeConstants.SUNDAY) { date2 = date2.plusWeeks(1).withDayOfWeek(1); } log.info("3." + date2.toString(pattern)); LocalDate date3 = dt.plusMonths(1).withDayOfMonth(5); if (date3.getDayOfWeek() >= 6) { date3 = date3.plusWeeks(1).withDayOfWeek(1); } log.info("4.2014/7" + date3.toString(pattern)); }
From source file:com.squid.kraken.v4.api.core.EngineUtils.java
License:Open Source License
/** * Convert the facet value into a date. * If the value start with '=', it is expected to be a Expression, in which case we'll try to resolve it to a Date constant. * @param ctx/*w w w . j a v a2s . c o m*/ * @param index * @param lower * @param value * @param compareFromInterval * @return * @throws ParseException * @throws ScopeException * @throws ComputingException */ public Date convertToDate(Universe universe, DimensionIndex index, Bound bound, String value, IntervalleObject compareFromInterval) throws ParseException, ScopeException, ComputingException { if (value.equals("")) { return null; } else if (value.startsWith("__")) { // // support hard-coded shortcuts if (value.toUpperCase().startsWith("__COMPARE_TO_")) { // for compareTo if (compareFromInterval == null) { // invalid compare_to selection... return null; } if (value.equalsIgnoreCase("__COMPARE_TO_PREVIOUS_PERIOD")) { LocalDate localLower = new LocalDate(((Date) compareFromInterval.getLowerBound()).getTime()); if (bound == Bound.UPPER) { LocalDate date = localLower.minusDays(1); return date.toDate(); } else { LocalDate localUpper = new LocalDate( ((Date) compareFromInterval.getUpperBound()).getTime()); Days days = Days.daysBetween(localLower, localUpper); LocalDate date = localLower.minusDays(1 + days.getDays()); return date.toDate(); } } if (value.equalsIgnoreCase("__COMPARE_TO_PREVIOUS_MONTH")) { LocalDate localLower = new LocalDate(((Date) compareFromInterval.getLowerBound()).getTime()); LocalDate compareLower = localLower.minusMonths(1); if (bound == Bound.LOWER) { return compareLower.toDate(); } else { LocalDate localUpper = new LocalDate( ((Date) compareFromInterval.getUpperBound()).getTime()); Days days = Days.daysBetween(localLower, localUpper); LocalDate compareUpper = compareLower.plusDays(days.getDays()); return compareUpper.toDate(); } } if (value.equalsIgnoreCase("__COMPARE_TO_PREVIOUS_YEAR")) { LocalDate localLower = new LocalDate(((Date) compareFromInterval.getLowerBound()).getTime()); LocalDate compareLower = localLower.minusYears(1); if (bound == Bound.LOWER) { return compareLower.toDate(); } else { LocalDate localUpper = new LocalDate( ((Date) compareFromInterval.getUpperBound()).getTime()); Days days = Days.daysBetween(localLower, localUpper); LocalDate compareUpper = compareLower.plusDays(days.getDays()); return compareUpper.toDate(); } } } else { // for regular // get MIN, MAX first Intervalle range = null; if (index.getDimension().getType() == Type.CONTINUOUS) { if (index.getStatus() == Status.DONE) { List<DimensionMember> members = index.getMembers(); if (!members.isEmpty()) { DimensionMember member = members.get(0); Object object = member.getID(); if (object instanceof Intervalle) { range = (Intervalle) object; } } } else { try { DomainHierarchy hierarchy = universe .getDomainHierarchy(index.getAxis().getParent().getDomain()); hierarchy.isDone(index, null); } catch (ComputingException | InterruptedException | ExecutionException | TimeoutException e) { throw new ComputingException("failed to retrieve period interval"); } } } if (range == null) { range = IntervalleObject.createInterval(new Date(), new Date()); } if (value.equalsIgnoreCase("__ALL")) { if (index.getDimension().getType() != Type.CONTINUOUS) { return null; } if (bound == Bound.UPPER) { return (Date) range.getUpperBound(); } else { return (Date) range.getLowerBound(); } } if (value.equalsIgnoreCase("__LAST_DAY")) { if (bound == Bound.UPPER) { return (Date) range.getUpperBound(); } else { return (Date) range.getUpperBound(); } } if (value.equalsIgnoreCase("__LAST_7_DAYS")) { if (bound == Bound.UPPER) { return (Date) range.getUpperBound(); } else { LocalDate localUpper = new LocalDate(((Date) range.getUpperBound()).getTime()); LocalDate date = localUpper.minusDays(6);// 6+1 return date.toDate(); } } if (value.equalsIgnoreCase("__CURRENT_MONTH")) { if (bound == Bound.UPPER) { return (Date) range.getUpperBound(); } else { LocalDate localUpper = new LocalDate(((Date) range.getUpperBound()).getTime()); LocalDate date = localUpper.withDayOfMonth(1); return date.toDate(); } } if (value.equalsIgnoreCase("__CURRENT_YEAR")) { if (bound == Bound.UPPER) { return (Date) range.getUpperBound(); } else { LocalDate localUpper = new LocalDate(((Date) range.getUpperBound()).getTime()); LocalDate date = localUpper.withMonthOfYear(1).withDayOfMonth(1); return date.toDate(); } } if (value.equalsIgnoreCase("__PREVIOUS_MONTH")) {// the previous complete month if (bound == Bound.UPPER) { LocalDate localUpper = new LocalDate(((Date) range.getUpperBound()).getTime()); LocalDate date = localUpper.withDayOfMonth(1).minusDays(1); return date.toDate(); } else { LocalDate localUpper = new LocalDate(((Date) range.getUpperBound()).getTime()); LocalDate date = localUpper.withDayOfMonth(1).minusMonths(1); return date.toDate(); } } if (value.equalsIgnoreCase("__PREVIOUS_YEAR")) {// the previous complete month if (bound == Bound.UPPER) { LocalDate localUpper = new LocalDate(((Date) range.getUpperBound()).getTime()); LocalDate date = localUpper.withMonthOfYear(1).withDayOfMonth(1).minusDays(1); return date.toDate(); } else { LocalDate localUpper = new LocalDate(((Date) range.getUpperBound()).getTime()); LocalDate date = localUpper.withMonthOfYear(1).withDayOfMonth(1).minusYears(1); return date.toDate(); } } } throw new ScopeException("undefined facet expression alias: " + value); } else if (value.startsWith("=")) { // if the value starts by equal token, this is a formula that can be // evaluated try { String expr = value.substring(1); // check if the index content is available or wait for it DomainHierarchy hierarchy = universe.getDomainHierarchy(index.getAxis().getParent().getDomain(), true); hierarchy.isDone(index, null); // evaluate the expression Object defaultValue = evaluateExpression(universe, index, expr, compareFromInterval); // check we can use it if (defaultValue == null) { //throw new ScopeException("unable to parse the facet expression as a constant: " + expr); // T1769: it's ok to return null return null; } if (!(defaultValue instanceof Date)) { throw new ScopeException("unable to parse the facet expression as a date: " + expr); } // ok, it's a date return (Date) defaultValue; } catch (ComputingException | InterruptedException | ExecutionException | TimeoutException e) { throw new ComputingException("failed to retrieve period interval"); } } else { Date date = ServiceUtils.getInstance().toDate(value); if (bound == Bound.UPPER && !index.getAxis().getDefinitionSafe().getImageDomain().isInstanceOf(IDomain.TIME)) { // clear the timestamp return new LocalDate(date.getTime()).toDate(); } else { return date; } } }
From source file:de.appsolve.padelcampus.admin.controller.reports.AdminReportsUtilizationController.java
@RequestMapping() public ModelAndView getIndex() throws JsonProcessingException { LocalDate endDate = new LocalDate(); LocalDate startDate = endDate.minusMonths(3); DateRange dateRange = new DateRange(); dateRange.setStartDate(startDate);//from w ww .j ava 2 s.c o m dateRange.setEndDate(endDate); return getIndexView(dateRange); }
From source file:de.appsolve.padelcampus.tasks.VoucherCleanupTask.java
@Scheduled(cron = "0 0 2 * * *") //second minute hour day month year, * = any, */5 = every 5 public void deleteOldVouchers() { try {//from w w w . java 2s . c o m LocalDate now = new LocalDate(); LocalDate oneMonthAgo = now.minusMonths(1); List<Voucher> expiredVouchers = voucherBaseDAO.findExpiredBefore(oneMonthAgo); LOG.info("Deleting " + expiredVouchers.size() + " Vouchers expired before " + oneMonthAgo); for (Voucher voucher : expiredVouchers) { voucherBaseDAO.deleteById(voucher.getId()); } } catch (Throwable t) { errorReporter.notify(t); } }
From source file:it.cineca.pst.huborcid.service.UserService.java
License:Open Source License
/** * Persistent Token are used for providing automatic authentication, they should be automatically deleted after * 30 days.// w w w.ja va2 s.c o m * <p/> * <p> * This is scheduled to get fired everyday, at midnight. * </p> */ @Scheduled(cron = "0 0 0 * * ?") public void removeOldPersistentTokens() { LocalDate now = new LocalDate(); List<PersistentToken> tokens = persistentTokenRepository.findByTokenDateBefore(now.minusMonths(1)); for (PersistentToken token : tokens) { log.debug("Deleting token {}", token.getSeries()); User user = token.getUser(); user.getPersistentTokens().remove(token); persistentTokenRepository.delete(token); } }