List of usage examples for org.joda.time DateTime minusMonths
public DateTime minusMonths(int months)
From source file:com.netflix.ice.processor.BillingFileProcessor.java
License:Apache License
private void archiveSummary(Map<Product, ReadWriteData> dataMap, String prefix) throws Exception { DateTime monthDateTime = new DateTime(startMilli, DateTimeZone.UTC); for (Product product : dataMap.keySet()) { String prodName = product == null ? "all" : product.name; ReadWriteData data = dataMap.get(product); Collection<TagGroup> tagGroups = data.getTagGroups(); // init daily, weekly and monthly List<Map<TagGroup, Double>> daily = Lists.newArrayList(); List<Map<TagGroup, Double>> weekly = Lists.newArrayList(); List<Map<TagGroup, Double>> monthly = Lists.newArrayList(); // get last month data ReadWriteData lastMonthData = new DataWriter(prefix + "hourly_" + prodName + "_" + AwsUtils.monthDateFormat.print(monthDateTime.minusMonths(1)), true).getData(); // aggregate to daily, weekly and monthly int dayOfWeek = monthDateTime.getDayOfWeek(); int daysFromLastMonth = dayOfWeek - 1; int lastMonthNumHours = monthDateTime.minusMonths(1).dayOfMonth().getMaximumValue() * 24; for (int hour = 0 - daysFromLastMonth * 24; hour < data.getNum(); hour++) { if (hour < 0) { // handle data from last month, add to weekly Map<TagGroup, Double> prevData = lastMonthData.getData(lastMonthNumHours + hour); for (TagGroup tagGroup : tagGroups) { Double v = prevData.get(tagGroup); if (v != null && v != 0) { addValue(weekly, 0, tagGroup, v); }/*from w w w . j a v a2s. c o m*/ } } else { // this month, add to weekly, monthly and daily Map<TagGroup, Double> map = data.getData(hour); for (TagGroup tagGroup : tagGroups) { Double v = map.get(tagGroup); if (v != null && v != 0) { addValue(monthly, 0, tagGroup, v); addValue(daily, hour / 24, tagGroup, v); addValue(weekly, (hour + daysFromLastMonth * 24) / 24 / 7, tagGroup, v); } } } } // archive daily int year = monthDateTime.getYear(); DataWriter writer = new DataWriter(prefix + "daily_" + prodName + "_" + year, true); ReadWriteData dailyData = writer.getData(); dailyData.setData(daily, monthDateTime.getDayOfYear() - 1, false); writer.archive(); // archive monthly writer = new DataWriter(prefix + "monthly_" + prodName, true); ReadWriteData monthlyData = writer.getData(); monthlyData.setData(monthly, Months.monthsBetween(config.startDate, monthDateTime).getMonths(), false); writer.archive(); // archive weekly writer = new DataWriter(prefix + "weekly_" + prodName, true); ReadWriteData weeklyData = writer.getData(); DateTime weekStart = monthDateTime.withDayOfWeek(1); int index; if (!weekStart.isAfter(config.startDate)) index = 0; else index = Weeks.weeksBetween(config.startDate, weekStart).getWeeks() + (config.startDate.dayOfWeek() == weekStart.dayOfWeek() ? 0 : 1); weeklyData.setData(weekly, index, true); writer.archive(); } }
From source file:com.netflix.raigad.indexmanagement.IndexUtils.java
License:Apache License
public static int getPastRetentionCutoffDate(IndexMetadata indexMetadata) throws UnsupportedAutoIndexException { DateTime dt = new DateTime(); int currentDate; switch (indexMetadata.getRetentionType()) { case DAILY:/*from ww w.j av a 2s .c o m*/ dt = dt.minusDays(indexMetadata.getRetentionPeriod()); currentDate = Integer .parseInt(String.format("%d%02d%02d", dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth())); break; case MONTHLY: dt = dt.minusMonths(indexMetadata.getRetentionPeriod()); currentDate = Integer.parseInt(String.format("%d%02d", dt.getYear(), dt.getMonthOfYear())); break; case YEARLY: dt = dt.minusYears(indexMetadata.getRetentionPeriod()); currentDate = Integer.parseInt(String.format("%d", dt.getYear())); break; default: throw new UnsupportedAutoIndexException( "Given index is not (DAILY or MONTHLY or YEARLY), please check your configuration."); } return currentDate; }
From source file:com.nfsdb.journal.utils.Dates.java
License:Apache License
private static Interval lastMonths(DateTime endDateTime, int duration) { if (duration < 1) { throw new JournalRuntimeException("Duration should be >= 1: %d", duration); }/*from w ww . j av a 2 s .c o m*/ DateTime start = endDateTime.minusMonths(duration); return new Interval(start, endDateTime); }
From source file:com.ning.billing.util.clock.DefaultClock.java
License:Apache License
public static DateTime addOrRemoveDuration(final DateTime input, final List<Duration> durations, final boolean add) { DateTime result = input; for (final Duration cur : durations) { switch (cur.getUnit()) { case DAYS: result = add ? result.plusDays(cur.getNumber()) : result.minusDays(cur.getNumber()); break; case MONTHS: result = add ? result.plusMonths(cur.getNumber()) : result.minusMonths(cur.getNumber()); break; case YEARS: result = add ? result.plusYears(cur.getNumber()) : result.minusYears(cur.getNumber()); break; case UNLIMITED: default:// w ww . ja v a2 s. c o m throw new RuntimeException("Trying to move to unlimited time period"); } } return result; }
From source file:com.reclabs.recomendar.common.helpers.types.DateHelper.java
License:Open Source License
/** * Subtract to the current date the amount of that precisionType represent. * @param date The date to subtract/* w w w. ja v a2s . c o m*/ * @param amount The amount to subtract * @param precisionType The field to subtract * @return The new date */ public static Date subtract(Date date, int amount, DatePrecisionType precisionType) { DateTime jodaDate1 = new DateTime(date.getTime()); DateTime result; switch (precisionType) { case YEAR: result = jodaDate1.minusYears(amount); break; case MONTH: result = jodaDate1.minusMonths(amount); break; case DAY: result = jodaDate1.minusDays(amount); break; case HOUR: result = jodaDate1.minusHours(amount); break; case MINUTE: result = jodaDate1.minusMinutes(amount); break; case SECOND: result = jodaDate1.minusSeconds(amount); break; case MILLISECOND: result = jodaDate1.minusMillis(amount); break; default: LOGGER.warn("[Error subtract, precision value is invalid: {}]", precisionType); throw new RecIllegalArgumentException("The precision value is invalid " + precisionType); } return result.toDate(); }
From source file:com.salesmanBuddy.dao.JDBCSalesmanBuddyDAO.java
License:Open Source License
private List<SBEmail> generateIndividualSalesmanSummaryEmailsForDealershipIdReportType(Integer dealershipId, Integer reportType) {/*from ww w . j a va 2s . c o m*/ DateTime to = new DateTime(DateTimeZone.UTC); DateTime from = null; switch (reportType) { case DAILY_SALESMAN_SUMMARY_EMAIL_TYPE: from = to.minusDays(1).minusMinutes(10); break; case WEEKLY_SALESMAN_SUMMARY_EMAIL_TYPE: from = to.minusWeeks(1).minusMinutes(10); break; case MONTHLY_SALESMAN_SUMMARY_EMAIL_TYPE: from = to.minusMonths(1).minusMinutes(10); break; default: throw new RuntimeException( "report type invalid for generateIndividualSalesmanSummaryEmailsForDealershipIdReportType"); } List<UserTree> userTrees = this.getUserTreeForDealershipIdType(dealershipId, reportType); List<SBEmail> emails = new ArrayList<>(); for (UserTree ut : userTrees) { try { String subject = "Report about " + this.getUsersName(ut.getUserId()).getName() + " from Salesman Buddy"; String body = this.individualSalesmanSummaryReport(this.getUserByGoogleId(ut.getUserId()).getId(), from, to); SBEmail email = SBEmail.newPlainTextEmail(REPORTS_EMAIL, null, subject, body, true); email.replaceTo(this.getEmailForGoogleId(ut.getSupervisorId())); emails.add(email); } catch (UserNameException e) { e.printStackTrace(); JDBCSalesmanBuddyDAO.sendErrorToMe(new StringBuilder().append( "Error in generateIndividualSalesmanSummaryEmailsForDealershipIdReportType for getting user's name for userTree: ") .append(ut.toString()).append(", error: ").append(e.getLocalizedMessage()).toString()); } } return emails; }
From source file:com.salesmanBuddy.dao.JDBCSalesmanBuddyDAO.java
License:Open Source License
private String generateEmailContentForDealershipIdReportType(Integer dealershipId, Integer type) { DateTime now = new DateTime(DateTimeZone.forID("America/Denver")); final Integer BACK_MINUTES = 10; DateTime dayPrevious = now.minusDays(1).minusMinutes(BACK_MINUTES); DateTime weekPrevious = now.minusWeeks(1).minusMinutes(BACK_MINUTES); DateTime monthPrevious = now.minusMonths(1).minusMinutes(BACK_MINUTES); switch (type) { case DAILY_TEST_DRIVE_SUMMARY_EMAIL_TYPE: return this.testDriveSummaryReport(dealershipId, dayPrevious, now); case DAILY_ALL_SALESMAN_SUMMARY_EMAIL_TYPE: return this.allSalesmanSummaryReport(dealershipId, dayPrevious, now); case DAILY_DEALERSHIP_SUMMARY_EMAIL_TYPE: return this.dealershipSummaryReport(dealershipId, dayPrevious, now); case DAILY_STOCK_NUMBERS_EMAIL_TYPE: return this.stockNumberSummaryReport(dealershipId, dayPrevious, now); case WEEKLY_TEST_DRIVE_SUMMARY_EMAIL_TYPE: return this.testDriveSummaryReport(dealershipId, weekPrevious, now); case WEEKLY_ALL_SALESMAN_SUMMARY_EMAIL_TYPE: return this.allSalesmanSummaryReport(dealershipId, weekPrevious, now); case WEEKLY_DEALERSHIP_SUMMARY_EMAIL_TYPE: return this.dealershipSummaryReport(dealershipId, weekPrevious, now); case WEEKLY_STOCK_NUMBERS_EMAIL_TYPE: return this.stockNumberSummaryReport(dealershipId, weekPrevious, now); case MONTHLY_TEST_DRIVE_SUMMARY_EMAIL_TYPE: return this.testDriveSummaryReport(dealershipId, monthPrevious, now); case MONTHLY_ALL_SALESMAN_SUMMARY_EMAIL_TYPE: return this.allSalesmanSummaryReport(dealershipId, monthPrevious, now); case MONTHLY_DEALERSHIP_SUMMARY_EMAIL_TYPE: return this.dealershipSummaryReport(dealershipId, monthPrevious, now); case MONTHLY_STOCK_NUMBERS_EMAIL_TYPE: return this.stockNumberSummaryReport(dealershipId, monthPrevious, now); // case DAILY_SALESMAN_SUMMARY_EMAIL_TYPE: // // w w w .ja v a2 s . c o m // break; // // case WEEKLY_SALESMAN_SUMMARY_EMAIL_TYPE: // // break; // // case MONTHLY_SALESMAN_SUMMARY_EMAIL_TYPE: // // break; default: return "Error, couldn't find correct report type"; } }
From source file:com.sos.scheduler.model.objects.JodaTools.java
License:Apache License
public static DateTime getDayInMonth(DateTime date, int day) { DateTime baseDate = (day > 1) ? date.minusMonths(1) : date; DateTime result = getEndOfMonth(baseDate).plusDays(day); return result.minusMillis(result.getMillisOfDay()); }
From source file:com.splicemachine.derby.utils.SpliceDateFunctions.java
License:Apache License
/** * Implements the trunc_date function/* w w w .ja v a 2s.c o m*/ */ public static Timestamp TRUNC_DATE(Timestamp source, String field) throws SQLException { if (source == null || field == null) return null; DateTime dt = new DateTime(source); field = field.toLowerCase(); String lowerCaseField = field.toLowerCase(); if ("microseconds".equals(lowerCaseField)) { int nanos = source.getNanos(); nanos = nanos - nanos % 1000; source.setNanos(nanos); return source; } else if ("milliseconds".equals(lowerCaseField)) { int nanos = source.getNanos(); nanos = nanos - nanos % 1000000; source.setNanos(nanos); return source; } else if ("second".equals(lowerCaseField)) { source.setNanos(0); return source; } else if ("minute".equals(lowerCaseField)) { DateTime modified = dt.minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("hour".equals(lowerCaseField)) { DateTime modified = dt.minusMinutes(dt.getMinuteOfHour()).minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("day".equals(lowerCaseField)) { DateTime modified = dt.minusHours(dt.getHourOfDay()).minusMinutes(dt.getMinuteOfHour()) .minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("week".equals(lowerCaseField)) { DateTime modified = dt.minusDays(dt.getDayOfWeek()).minusHours(dt.getHourOfDay()) .minusMinutes(dt.getMinuteOfHour()).minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("month".equals(lowerCaseField)) { DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1) .minusHours(dt.getHourOfDay()).minusMinutes(dt.getMinuteOfHour()) .minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("quarter".equals(lowerCaseField)) { int month = dt.getMonthOfYear(); DateTime modified = dt; if ((month + 1) % 3 == 1) { modified = dt.minusMonths(2); } else if ((month + 1) % 3 == 0) { modified = dt.minusMonths(1); } DateTime fin = modified.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1) .minusHours(dt.getHourOfDay()).minusMinutes(dt.getMinuteOfHour()) .minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(fin.getMillis()); ret.setNanos(0); return ret; } else if ("year".equals(lowerCaseField)) { DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1) .minusHours(dt.getHourOfDay()).minusMonths(dt.getMonthOfYear() - 1) .minusMinutes(dt.getMinuteOfHour()).minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("decade".equals(lowerCaseField)) { DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1) .minusYears(dt.getYear() % 10).minusHours(dt.getHourOfDay()) .minusMonths(dt.getMonthOfYear() - 1).minusMinutes(dt.getMinuteOfHour()) .minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("century".equals(lowerCaseField)) { DateTime modified = dt.minusDays(dt.get(DateTimeFieldType.dayOfMonth()) - 1) .minusHours(dt.getHourOfDay()).minusYears(dt.getYear() % 100) .minusMonths(dt.getMonthOfYear() - 1).minusMinutes(dt.getMinuteOfHour()) .minusSeconds(dt.getSecondOfMinute()); Timestamp ret = new Timestamp(modified.getMillis()); ret.setNanos(0); return ret; } else if ("millennium".equals(lowerCaseField)) { int newYear = dt.getYear() - dt.getYear() % 1000; //noinspection deprecation (converstion from joda to java.sql.Timestamp did not work for millennium < 2000) return new Timestamp(newYear - 1900, Calendar.JANUARY, 1, 0, 0, 0, 0); } else { throw new SQLException(String.format("invalid time unit '%s'", field)); } }
From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java
License:Open Source License
private IntervalleObject alignPastInterval(IntervalleObject presentInterval, IntervalleObject pastInterval, Axis joinAxis) throws ScopeException { if (joinAxis != null && presentInterval != null && pastInterval != null) { Object lowerPresent = presentInterval.getLowerBound(); Object lowerPast = pastInterval.getLowerBound(); Object upperPresent = presentInterval.getUpperBound(); Object upperPast = pastInterval.getUpperBound(); ///*w ww. ja v a 2 s .c o m*/ IDomain image = joinAxis.getDefinition().getImageDomain(); if (lowerPresent instanceof Date && lowerPast instanceof Date) { DateTime lowerPastDT = new DateTime((Date) lowerPast); DateTime lowerPresentDT = new DateTime((Date) lowerPresent); DateTime upperPresentDT = new DateTime((Date) upperPresent); DateTime upperPastDT = new DateTime((Date) upperPast); // realign if (image.isInstanceOf(IDomain.YEARLY)) { // check if present is an exact number of years if (lowerPresentDT.getDayOfYear() == 1 && upperPresentDT.getDayOfYear() == upperPresentDT.dayOfYear().getMaximumValue()) { // check of both periods have the same number of days Period presentPeriod = new Period(new LocalDate(lowerPresent), (new LocalDate(upperPresent)), PeriodType.days()); Period pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)), PeriodType.days()); if (presentPeriod.getDays() == pastPeriod.getDays()) { presentPeriod = new Period(new LocalDate(lowerPresent), (new LocalDate(upperPresent)).plusDays(1), PeriodType.years()); pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)).plusDays(1), PeriodType.years()); // realign if (presentPeriod.getYears() > pastPeriod.getYears()) { // some days are missing to align the periods if (lowerPastDT.getDayOfYear() != 1) { // previous period Date newLowerPast = new DateTime(upperPastDT.getYear(), 1, 1, 0, 0).toDate(); return new IntervalleObject(newLowerPast, upperPast); } if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) { // year over year Date newUpperPast = new DateTime(upperPastDT.getYear(), 12, 31, 23, 59) .toDate(); return new IntervalleObject(lowerPast, newUpperPast); } } else { // either already aligned, or some days should // be removed if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) { // year over Year Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59) .toDate(); return new IntervalleObject(lowerPast, newUpperPast); } if (lowerPastDT.getDayOfYear() != 1) { // previous period Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0) .toDate(); return new IntervalleObject(newLowerPast, upperPast); } } } } } else if (image.isInstanceOf(IDomain.QUATERLY) || image.isInstanceOf(IDomain.MONTHLY)) { // check if present is an exact number of month if (lowerPresentDT.getDayOfMonth() == 1 && upperPresentDT.getDayOfMonth() == upperPresentDT.dayOfMonth().getMaximumValue()) { // check of both periods have the same number of days Period presentPeriod = new Period(new LocalDate(lowerPresent), new LocalDate(upperPresent), PeriodType.days()); Period pastPeriod = new Period(new LocalDate(lowerPast), new LocalDate(upperPast), PeriodType.days()); if (presentPeriod.getDays() == pastPeriod.getDays()) { // realign presentPeriod = new Period(new LocalDate(lowerPresent), (new LocalDate(upperPresent)).plusDays(1), PeriodType.months()); pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)).plusDays(1), PeriodType.months()); if (presentPeriod.getMonths() > pastPeriod.getMonths()) { // some days are missing if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) { // month over month Date newUpperPast = new DateTime(upperPastDT.getYear(), upperPastDT.getMonthOfYear(), upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate(); return new IntervalleObject(lowerPast, newUpperPast); } if (lowerPastDT.getDayOfMonth() != 1) { // previous period Date newLowerPast = new DateTime(lowerPastDT.getYear(), lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate(); return new IntervalleObject(newLowerPast, upperPast); } } else { // either already aligned, of some days should // be removed if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) { /// month over month if (upperPastDT.getMonthOfYear() == 1) { Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59) .toDate(); return new IntervalleObject(lowerPast, newUpperPast); } else { upperPastDT = upperPastDT.minusMonths(1); Date newUpperPast = new DateTime(upperPastDT.getYear(), upperPastDT.getMonthOfYear(), upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate(); return new IntervalleObject(lowerPast, newUpperPast); } } if (lowerPastDT.getDayOfMonth() != 1) { // previous period if (lowerPastDT.getMonthOfYear() == 12) { Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0) .toDate(); return new IntervalleObject(newLowerPast, upperPast); } else { lowerPastDT = lowerPastDT.plusMonths(1); Date newLowerPast = new DateTime(lowerPastDT.getYear(), lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate(); return new IntervalleObject(newLowerPast, upperPast); } } } } } } } } return pastInterval; }