Java Code Examples for org.joda.time.DateTime#minusMonths()

The following examples show how to use org.joda.time.DateTime#minusMonths() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private ReferenceBag calculateReference(DateTime start, DateTime end, String referenceType,
                                        MetricContract metricContract) throws IOException {
    ReferenceBag referenceBag = new ReferenceBag();
    referenceBag.setReferenceType(referenceType);
    DateTime referenceStart;
    DateTime referenceEnd;
    if (referenceType.equalsIgnoreCase("DAY")) {
        referenceStart = start.minusDays(1);
        referenceEnd = end.minusDays(1);
        referenceBag.setDescription("昨天");
    } else if (referenceType.equalsIgnoreCase("WEEK")) {
        referenceStart = start.minusDays(7);
        referenceEnd = end.minusDays(7);
        referenceBag.setDescription("上周");
    } else if (referenceType.equalsIgnoreCase("MONTH")) {
        referenceStart = start.minusMonths(1);
        referenceEnd = end.minusMonths(1);
        referenceBag.setDescription("上月");
    } else {
        throw new IllegalArgumentException("unknown reference_type: " + referenceType);
    }
    ElasticsearchMetric elasticsearchMetric = this.elasticsearchDataQuery.queryElasticsearchMetricValue(referenceStart, referenceEnd, metricContract);
    referenceBag.setValue(toDouble(elasticsearchMetric.getMetricValue(), 0D));
    return new ReferenceBag();
}
 
Example 2
private DateInterval toMonthIsoInterval( DateTimeUnit dateTimeUnit, int offset, int length )
{
    DateTime from = dateTimeUnit.toJodaDateTime( chronology );

    if ( offset > 0 )
    {
        from = from.plusMonths( offset );
    }
    else if ( offset < 0 )
    {
        from = from.minusMonths( -offset );
    }

    DateTime to = new DateTime( from ).plusMonths( length ).minusDays( 1 );

    DateTimeUnit fromDateTimeUnit = DateTimeUnit.fromJodaDateTime( from );
    DateTimeUnit toDateTimeUnit = DateTimeUnit.fromJodaDateTime( to );

    fromDateTimeUnit.setDayOfWeek( isoWeekday( fromDateTimeUnit ) );
    toDateTimeUnit.setDayOfWeek( isoWeekday( toDateTimeUnit ) );

    return new DateInterval( toIso( fromDateTimeUnit ), toIso( toDateTimeUnit ), DateIntervalType.ISO8601_MONTH );
}
 
Example 3
/**
 * Create a list of DateTimePeriods that represent the last year of
 * YearMonth periods.  For example, if its currently January 2009, this
 * would return periods representing "January 2009, December 2008, ... February 2008"
 * @param zone
 * @return
 */
static public List<DateTimePeriod> createLastYearMonths(DateTimeZone zone) {
    ArrayList<DateTimePeriod> periods = new ArrayList<DateTimePeriod>();

    // get today's date
    DateTime now = new DateTime(zone);

    // start with today's current month and 11 others (last 12 months)
    for (int i = 0; i < 12; i++) {
        // create a new period
        DateTimePeriod period = createMonth(now.getYear(), now.getMonthOfYear(), zone);
        periods.add(period);
        // subtract 1 month
        now = now.minusMonths(1);
    }

    return periods;
}
 
Example 4
/**
 * Create a list of DateTimePeriods that represent the last 12 month periods
 * based on the current date.  The list will be arranged in ascending order
 * from earliest to latest date. For example, if its currently January 2009,
 * a list containing "February 2008, March 2008, ... , January 2009" would
 * be returned.  If you need this list in reverse order to show the most
 * recent month first, just call Collections.reverse() on the returned list.
 * @param zone The time zone used for calculations
 * @return A list of the last 12 months
 */
static public List<DateTimePeriod> last12Months(DateTimeZone zone) {
    ArrayList<DateTimePeriod> periods = new ArrayList<DateTimePeriod>();

    // get today's date
    DateTime now = new DateTime(zone);

    // start with today's current month and 11 others (last 12 months)
    for (int i = 0; i < 12; i++) {
        // create a new period
        DateTimePeriod period = DateTimePeriod.createMonth(now.getYear(), now.getMonthOfYear(), zone);
        periods.add(period);
        // subtract 1 month
        now = now.minusMonths(1);
    }

    Collections.reverse(periods);

    return periods;
}
 
Example 5
/**
 * Replace date formats with the DateTime values passed in.
 *
 * Operations like "YYYYMMdd-1D" are supported for doing simple subtraction
 * on the passed in DateTime.
 *
 * See DateMod for supported modification values.
 *
 * @param aQuery - a String that's modified by doing replacement according to the formats specified
 * @param replaceWith - the DateTime moment for which the replacements are based off of
 */
public static String replaceDateValues(String aQuery, DateTime replaceWith) {
  Matcher matcher = DATE_REPLACE_PATTERN.matcher(aQuery);
  while (matcher.find()) {
    final String format = matcher.group(1);
    DateTimeFormatter formatter = QueryReplaceUtil.makeDateTimeFormat(format);
    if (matcher.groupCount() > 3 &&
        matcher.group(3) != null && matcher.group(4) != null) {
      int count = Integer.valueOf(matcher.group(3));
      DateMod dm = DateMod.valueOf(matcher.group(4));
      DateTime toMod = new DateTime(replaceWith);

      if (dm.equals(DateMod.H)) {
        toMod = toMod.minusHours(count);
      } else if (dm.equals(DateMod.D)) {
        toMod = toMod.minusDays(count);
      } else if (dm.equals(DateMod.M)) {
        toMod = toMod.minusMonths(count);
      }

      aQuery = aQuery.replace(matcher.group(), formatter.print(toMod));
    } else { // no mod
      aQuery = aQuery.replace(matcher.group(), formatter.print(replaceWith));
    }
    matcher = DATE_REPLACE_PATTERN.matcher(aQuery);
  }
  return aQuery;
}
 
Example 6
private DateTime getTrainingStartTime(DateTime windowStart) {
  DateTime trainStart;
  if (isMultiDayGranularity()) {
    trainStart = windowStart.minusDays(timeGranularity.getSize() * LOOKBACK);
  } else if (this.monitoringGranularity.endsWith(TimeGranularity.MONTHS)) {
    trainStart = windowStart.minusMonths(LOOKBACK);
  } else if (this.monitoringGranularity.endsWith(TimeGranularity.WEEKS)) {
    trainStart = windowStart.minusWeeks(LOOKBACK);
  } else {
    trainStart = windowStart.minusDays(LOOKBACK);
  }
  return trainStart;
}
 
Example 7
Source Project: estatio   File: PaymentBatchMenu.java    License: Apache License 2.0 5 votes vote down vote up
@Action(semantics = SemanticsOf.SAFE)
@MemberOrder(sequence = "300.15")
public List<PaymentBatch> findRecentPaymentBatches() {
    DateTime now = clockService.nowAsDateTime();
    DateTime threeMonthsAgo = now.minusMonths(3);
    return paymentBatchRepository.findByCreatedOnBetween(threeMonthsAgo, now);
}
 
Example 8
@Test
public void lastMonth() throws Exception {
    DateTimePeriod period = DateTimePeriodSelector.lastMonth(DateTimeZone.UTC);
    DateTime now = new DateTime(DateTimeZone.UTC);
    now = now.minusMonths(1);
    DateTimePeriod expectedPeriod = DateTimePeriod.createMonth(now.getYear(), now.getMonthOfYear(), DateTimeZone.UTC);

    Assert.assertEquals(expectedPeriod, period);
}
 
Example 9
@Test
public void last12Months() throws Exception {
    List<DateTimePeriod> periods = DateTimePeriodSelector.last12Months(DateTimeZone.UTC);
    DateTime now = new DateTime(DateTimeZone.UTC);
    
    // create our list for comparison
    DateTime startingMonth = now.minusMonths(12);
    DateTimePeriod startingPeriod = DateTimePeriod.createMonth(startingMonth.getYear(), startingMonth.getMonthOfYear(), DateTimeZone.UTC);
    ArrayList<DateTimePeriod> expectedPeriods = new ArrayList<DateTimePeriod>();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);
    startingPeriod = startingPeriod.getNext();
    expectedPeriods.add(startingPeriod);

    Assert.assertArrayEquals(expectedPeriods.toArray(), periods.toArray());
}
 
Example 10
Source Project: liteflow   File: DateUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 获取上个月日期
 * @return
 */
public static Date getPreMonth(Date date) {
    DateTime dateTime = new DateTime(date);
    dateTime = dateTime.minusMonths(1);
    return dateTime.toDate();
}
 
Example 11
@Test
public void subtract_months_from_date_in_java_joda () {

	DateTime superBowlXLV = new DateTime(2011, 2, 6, 0, 0, 0, 0);
	DateTime championshipWeekend = superBowlXLV.minusMonths(1);

	DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss z");
	
	logger.info(superBowlXLV.toString(fmt));
	logger.info(championshipWeekend.toString(fmt));

	assertTrue(championshipWeekend.isBefore(superBowlXLV));
}