Example usage for org.joda.time DateTime minusMonths

List of usage examples for org.joda.time DateTime minusMonths

Introduction

In this page you can find the example usage for org.joda.time DateTime minusMonths.

Prototype

public DateTime minusMonths(int months) 

Source Link

Document

Returns a copy of this datetime minus the specified number of months.

Usage

From source file:org.opendaylight.streamhandler.impl.KafkaStreamProducer.java

License:Open Source License

/**
 * @param timeStamp// w w w . jav  a 2  s. c om
 *            timestamp.
 * @return long
 *           return rfc3164TimeStamp.
 */
protected long rfc3164TimeStamp(String timeStamp) {
    DateTime currentDateTime = DateTime.now();
    int yearCurrent = currentDateTime.getYear();
    timeStamp = SPACES_TWO_CHECK.matcher(timeStamp).replaceFirst(" ");
    DateTime dateReturned;
    try {
        dateReturned = RFC3164_FORMAT_DATETIME.parseDateTime(timeStamp);
    } catch (IllegalArgumentException e) {
        log.error("rfc3164 date parse failed on (" + timeStamp + "): invalid format", e);
        return 0;
    }
    if (dateReturned != null) {
        DateTime fixedDate = dateReturned.withYear(yearCurrent);
        if (fixedDate.isAfter(currentDateTime) && fixedDate.minusMonths(1).isAfter(currentDateTime)) {
            fixedDate = dateReturned.withYear(yearCurrent - 1);
        } else if (fixedDate.isBefore(currentDateTime) && fixedDate.plusMonths(1).isBefore(currentDateTime)) {
            fixedDate = dateReturned.withYear(yearCurrent + 1);
        }
        dateReturned = fixedDate;
    }
    if (dateReturned == null) {
        return 0;
    }
    return dateReturned.getMillis();
}

From source file:org.opendaylight.streamhandler.impl.LogCollector.java

License:Open Source License

protected long rfc3164TimeStamp(String timeStamp) {
    DateTime currentDateTime = DateTime.now();
    int yearCurrent = currentDateTime.getYear();
    timeStamp = SPACES_TWO_CHECK.matcher(timeStamp).replaceFirst(" ");
    DateTime dateReturned;/* w ww  .  j  av a  2s. c  om*/
    try {
        dateReturned = RFC3164_FORMAT_DATETIME.parseDateTime(timeStamp);
    } catch (IllegalArgumentException e) {
        LOG.error("rfc3164 date parse failed on (" + timeStamp + "): invalid format", e);
        return 0;
    }
    if (dateReturned != null) {
        DateTime fixedDate = dateReturned.withYear(yearCurrent);
        if (fixedDate.isAfter(currentDateTime) && fixedDate.minusMonths(1).isAfter(currentDateTime)) {
            fixedDate = dateReturned.withYear(yearCurrent - 1);
        } else if (fixedDate.isBefore(currentDateTime) && fixedDate.plusMonths(1).isBefore(currentDateTime)) {
            fixedDate = dateReturned.withYear(yearCurrent + 1);
        }
        dateReturned = fixedDate;
    }
    if (dateReturned == null) {
        return 0;
    }
    return dateReturned.getMillis();
}

From source file:org.opendaylight.streamhandler.impl.LogCollectorTLS.java

License:Open Source License

protected long rfc3164TimeStamp(String timeStamp) {
    DateTime currentDateTime = DateTime.now();
    int yearCurrent = currentDateTime.getYear();
    timeStamp = SPACES_TWO_CHECK.matcher(timeStamp).replaceFirst(" ");
    DateTime dateReturned;/*from   ww w.  j av  a 2  s . c o m*/
    try {
        dateReturned = rfc3164_FORMAT_DATETIME.parseDateTime(timeStamp);
    } catch (IllegalArgumentException e) {
        LOG.error("rfc3164 date parse failed on (" + timeStamp + "): invalid format", e);
        return 0;
    }
    if (dateReturned != null) {
        DateTime fixedDate = dateReturned.withYear(yearCurrent);
        if (fixedDate.isAfter(currentDateTime) && fixedDate.minusMonths(1).isAfter(currentDateTime)) {
            fixedDate = dateReturned.withYear(yearCurrent - 1);
        } else if (fixedDate.isBefore(currentDateTime) && fixedDate.plusMonths(1).isBefore(currentDateTime)) {
            fixedDate = dateReturned.withYear(yearCurrent + 1);
        }
        dateReturned = fixedDate;
    }
    if (dateReturned == null) {
        return 0;
    }
    return dateReturned.getMillis();
}

From source file:org.openlmis.core.presenter.RnRFormListPresenter.java

License:Open Source License

protected void addPreviousPeriodMissedViewModels(List<RnRFormViewModel> viewModels) throws LMISException {
    int offsetMonth = periodService.getMissedPeriodOffsetMonth(this.programCode);

    DateTime inventoryBeginDate = periodService.getCurrentMonthInventoryBeginDate();
    for (int i = 0; i < offsetMonth; i++) {
        viewModels.add(RnRFormViewModel.buildMissedPeriod(inventoryBeginDate.toDate(),
                inventoryBeginDate.plusMonths(1).toDate()));
        inventoryBeginDate = inventoryBeginDate.minusMonths(1);
    }/* w  ww .j  ava  2 s  . c o m*/
    generateFirstMissedRnrFormViewModel(viewModels, offsetMonth, inventoryBeginDate);
}

From source file:org.openlmis.report.service.lookup.ReportLookupService.java

License:Open Source License

public DateTime periodStartDate(Long range) {

    DateTime periodEndDate = periodEndDate();

    if (range == 1)
        return periodEndDate.withDayOfMonth(1);
    else if (range == 2)
        return periodEndDate.minusMonths(2).withDayOfMonth(1);
    else if (range == 3)
        return periodEndDate.minusMonths(5).withDayOfMonth(1);
    else if (range == 4)
        return periodEndDate.minusYears(1).withDayOfMonth(1);

    return null;/* w  ww . ja v  a2  s .  com*/

}

From source file:org.projectbuendia.client.ui.patientcreation.PatientCreationController.java

License:Apache License

private DateTime getBirthdateFromAge(int ageInt, int ageUnits) {
    DateTime now = DateTime.now();
    switch (ageUnits) {
    case AGE_YEARS:
        return now.minusYears(ageInt);
    case AGE_MONTHS:
        return now.minusMonths(ageInt);
    default:/* w w  w.  j av a  2  s.c om*/
        return null;
    }
}

From source file:org.springframework.analytics.rest.controller.AggregateCounterController.java

License:Apache License

/**
 * Return a default value for the interval start if none has been provided.
 *//*w w  w  .j a  v  a 2 s .  c o  m*/
private DateTime providedOrDefaultFromValue(DateTime from, DateTime to, AggregateCounterResolution resolution) {
    if (from != null) {
        return from;
    }
    switch (resolution) {
    case minute:
        return to.minusMinutes(59);
    case hour:
        return to.minusHours(23);
    case day:
        return to.minusDays(6);
    case month:
        return to.minusMonths(11);
    case year:
        return to.minusYears(4);
    default:
        throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution);
    }
}

From source file:org.springframework.xd.dirt.rest.metrics.AggregateCountersController.java

License:Apache License

/**
 * Return a default value for the interval start if none has been provided.
 *///from w  ww. j  a v  a  2 s.  c  o m
private DateTime providedOrDefaultFromValue(DateTime from, DateTime to, AggregateCountResolution resolution) {
    if (from != null) {
        return from;
    }
    switch (resolution) {
    case minute:
        return to.minusMinutes(59);
    case hour:
        return to.minusHours(23);
    case day:
        return to.minusDays(6);
    case month:
        return to.minusMonths(11);
    case year:
        return to.minusYears(4);
    default:
        throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution);
    }
}

From source file:org.wso2.das4esb.integration.common.clients.ConcurrentEventsPublisher.java

License:Open Source License

@Override
public void run() {
    Object[] metaData = { true, tenantId };
    int sentFaults = 0;
    int publishedRequests = 0;
    // Publish events
    try {//from   w ww  .j  av  a  2s.  com
        for (int j = 0; j < this.noOfRequests; j++) {
            boolean isFault = false;
            String messageId = "urn_uuid_" + UUID.randomUUID();
            String[] payloadData = new String[2];
            Event event;
            payloadData[0] = messageId;

            DateTime time = new DateTime();
            if (j < this.noOfRequests / 20) {
                time = time.minusMonths(3).minusDays(1).minusHours(2);
            } else if (j < this.noOfRequests / 20 * 2) {
                time = time.minusMonths(3).minusDays(1);
            } else if (j < this.noOfRequests / 20 * 3) {
                time = time.minusMonths(3);
            } else if (j < this.noOfRequests / 20 * 4) {
                time = time.minusMonths(2).minusDays(1).minusHours(2);
            } else if (j < this.noOfRequests / 20 * 5) {
                time = time.minusMonths(2).minusDays(1);
            } else if (j < this.noOfRequests / 20 * 6) {
                time = time.minusMonths(2);
            } else if (j < this.noOfRequests / 20 * 7) {
                time = time.minusMonths(1).minusDays(1).minusHours(2);
            } else if (j < this.noOfRequests / 20 * 8) {
                time = time.minusMonths(1).minusDays(1);
            } else if (j < this.noOfRequests / 20 * 9) {
                time = time.minusMonths(1);
            } else if (j < this.noOfRequests / 20 * 10) {
                time = time.minusHours(5);
            } else if (j < this.noOfRequests / 20 * 11) {
                time = time.minusHours(3);
            } else if (j < this.noOfRequests / 20 * 12) {
                time = time.minusHours(1);
            } else {
                isFault = sentFaults < this.noOfFaults;
            }

            long timeInMilliSec = time.getMillis();
            payloadData[1] = Utils.getESBCompressedEventString(messageId, this.entryPointName,
                    this.noOfMediators, this.payloadsEnabled, this.propertiesEnabled, isFault, timeInMilliSec);
            event = new Event(null, System.currentTimeMillis(), metaData, null, payloadData);
            this.dataPublisherClient.publish(TestConstants.ESB_FLOW_ENTRY_STREAM_NAME, "1.0.0", event);
            if (isFault) {
                sentFaults++;
            }
            // sleep to control the throughput
            Thread.sleep(this.sleepBetweenRequests);
            publishedRequests++;
        }
    } catch (DataEndpointException e) {
        throw new RuntimeException("Falied to publish event: " + e.getMessage(), e);
    } catch (InterruptedException ignored) {
    } finally {
        try {
            log.info("Published: " + publishedRequests + " events to: " + entryPointName + " for tenant: "
                    + tenantId);
            if (this.dataPublisherClient != null) {
                Thread.sleep(20000);
                this.dataPublisherClient.shutdown();
            }
        } catch (Exception ignored) {
        }
    }
}

From source file:ph.fingra.statisticsweb.common.util.DateTimeUtil.java

License:Apache License

public static String[] getDashboardFromToWithPrev(String numType) {

    int num = Integer.parseInt(numType.substring(0, numType.length() - 1));
    String type = numType.substring(numType.length() - 1).toLowerCase();
    final DateTime now = DateTime.now();
    final DateTime to = now.minusDays(1);

    final DateTime from = type.equals("w") ? to.minusDays(num * 7 - 1)
            : (type.equals("m") ? to.minusMonths(num) : to.withMonthOfYear(1).withDayOfMonth(1));
    final DateTime prevTo = type.equals("y") ? to.minusYears(1) : from.minusDays(1);
    final DateTime prevFrom = type.equals("w") ? prevTo.minusDays(num * 7 - 1)
            : (type.equals("m") ? prevTo.minusMonths(num) : prevTo.withMonthOfYear(1).withDayOfMonth(1));

    final DateTime yesterday = now.minusDays(1);
    final DateTime beforeYesterday = now.minusDays(2);
    String nowTime = "";
    String prevTime = "";

    // before or after 10 minutes
    if (now.getMinuteOfHour() < 10) { // before 10 minutes 
        String nowTemp = now.equals("0") ? "23" : String.valueOf(now.getHourOfDay() - 1);
        nowTime = (nowTemp.length() < 2) ? nowTemp = "0" + nowTemp : nowTemp;
        String prevTemp = nowTime.equals("00") ? "23" : String.valueOf(Integer.parseInt(nowTime) - 1);
        prevTime = (prevTemp.length() < 2) ? prevTemp = "0" + prevTemp : prevTemp;
        System.out.println("10    " + now.getMinuteOfHour() + "");
    } else { // after 10 minutes
        nowTime = (String.valueOf(now.getHourOfDay()).length() < 2) ? "0" + String.valueOf(now.getHourOfDay())
                : String.valueOf(now.getHourOfDay());
        String prevTemp = now.equals("0") ? "23" : String.valueOf(now.getHourOfDay() - 1);
        prevTime = (prevTemp.length() < 2) ? prevTemp = "0" + prevTemp : prevTemp;
        System.out.println("10    " + now.getMinuteOfHour() + "");
    }//from ww  w .  j  a  v  a 2s  .  c  o m

    return new String[] { from.toString("yyyy-MM-dd"), to.toString("yyyy-MM-dd"),
            prevFrom.toString("yyyy-MM-dd"), prevTo.toString("yyyy-MM-dd"), yesterday.toString("yyyy-MM-dd"),
            beforeYesterday.toString("yyyy-MM-dd"), now.toString("yyyy-MM-dd"), nowTime, prevTime };
}