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:com.barchart.feed.ddf.message.enums.DDF_TradeDay.java

License:BSD License

/**
 * recover full trade date from DDF day code and todays date
 * /*from w  ww  .j a va  2s  . com*/
 * expressed in UTC zone
 * 
 * year, month, day : should be treated as local market trade date.
 *
 * @param tradeDay the trade day
 * @param todayDate the today date
 * @return the time value
 */
public static TimeValue tradeDateFrom(final DDF_TradeDay tradeDay, final DateTime todayDate) {

    // trading day of month reported by the feed
    final int tradingDayNum = tradeDay.day;

    // current day of month
    final int currentDayNum = todayDate.getDayOfMonth();

    // positive for same month if trading date is in the future
    // unless day enum is not a day in the month ???
    final int difference = tradingDayNum - currentDayNum;

    final boolean isSmall = Math.abs(difference) <= HOLIDAY_THESHOLD;
    final boolean isLarge = !isSmall;

    //

    final boolean isSameMonthSameDay = (difference == 0);

    final boolean isSameMonthPastDay = difference < 0 & isSmall;
    final boolean isSameMonthNextDay = difference > 0 & isSmall;

    final boolean isPastMonthPastDay = difference > 0 & isLarge;
    final boolean isNextMonthNextDay = difference < 0 & isLarge;

    //

    DateTime generated;

    try {
        if (isSameMonthSameDay) {
            generated = todayDate;
        } else if (isSameMonthPastDay) {
            generated = todayDate.withDayOfMonth(tradingDayNum);
        } else if (isSameMonthNextDay) {
            generated = todayDate.withDayOfMonth(tradingDayNum);
        } else if (isPastMonthPastDay) {
            generated = todayDate.minusMonths(1).withDayOfMonth(tradingDayNum);
        } else if (isNextMonthNextDay) {
            generated = todayDate.plusMonths(1).withDayOfMonth(tradingDayNum);
        } else {
            logger.error("should not happen");
            generated = todayDate;
        }
    } catch (final Exception e) {
        generated = todayDate;
    }

    final DateTime result = new DateTime(//
            generated.getYear(), //
            generated.getMonthOfYear(), //
            generated.getDayOfMonth(), //
            0, 0, 0, 0, ZONE_UTC);

    final long millisUTC = result.getMillis();

    return ValueBuilder.newTime(millisUTC);

}

From source file:com.boha.monitor.util.DataUtil.java

public ResponseDTO getServerErrors(long startDate, long endDate) throws DataException {
    ResponseDTO r = new ResponseDTO();
    if (startDate == 0) {
        DateTime ed = new DateTime();
        DateTime sd = ed.minusMonths(3);
        startDate = sd.getMillis();/*from  w  w  w  .j av a  2 s  . com*/
        endDate = ed.getMillis();
    }
    try {
        Query q = em.createNamedQuery("ErrorStore.findByPeriod", ErrorStore.class);
        q.setParameter("startDate", new Date(startDate));
        q.setParameter("endDate", new Date(endDate));
        List<ErrorStore> list = q.getResultList();
        List<ErrorStoreDTO> dList = new ArrayList();
        for (ErrorStore e : list) {
            dList.add(new ErrorStoreDTO(e));
        }
        r.setErrorStoreList(dList);
        log.log(Level.OFF, "Errors found {0}", r.getErrorStoreList().size());
    } catch (Exception e) {
        log.log(Level.SEVERE, "Failed to getServerErrors");
        throw new DataException("Failed to getServerErrors\n" + getErrorString(e));
    }
    return r;
}

From source file:com.boha.proximity.util.DataUtil.java

public ResponseDTO getServerEvents(long startDate, long endDate) throws DataException {
    ResponseDTO r = new ResponseDTO();
    if (startDate == 0) {
        DateTime ed = new DateTime();
        DateTime sd = ed.minusMonths(3);
        startDate = sd.getMillis();/* www.j  a v  a  2 s . c  om*/
        endDate = ed.getMillis();
    }
    try {
        Query q = em.createNamedQuery("ErrorStoreAndroid.findByPeriod", ErrorStoreAndroid.class);
        q.setParameter("from", new Date(startDate));
        q.setParameter("to", new Date(endDate));
        List<ErrorStoreAndroid> list = q.getResultList();
        List<ErrorStoreAndroidDTO> dList = new ArrayList();
        for (ErrorStoreAndroid e : list) {
            dList.add(new ErrorStoreAndroidDTO(e));
        }
        r.setErrorStoreAndroidList(dList);
        r.setErrorStoreList(getServerErrors(startDate, endDate).getErrorStoreList());

        String logx = LogfileUtil.getFileString();
        r.setLog(logx);
        log.log(Level.OFF, "Android Errors found {0}", r.getErrorStoreAndroidList().size());
    } catch (DataException | IOException e) {
        log.log(Level.SEVERE, "Failed to findClubsWithinRadius");
        throw new DataException("Failed to findClubsWithinRadius\n" + getErrorString(e));
    }
    return r;
}

From source file:com.cloudhopper.commons.util.time.DateTimePeriod.java

License:Apache License

/**
 * 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/* ww  w .j a  v a 2s.co  m*/
 * @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;
}

From source file:com.cloudhopper.commons.util.time.DateTimePeriodSelector.java

License:Apache License

/**
 * 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/*w ww.  j av a2s.co  m*/
 */
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;
}

From source file:com.datastax.sampledata.CreateSSTables.java

private static Date randomDateInLastNYears(int years) {

    DateTime dateTime = new DateTime();
    dateTime = dateTime.minusYears(new Double(Math.random() * years).intValue());
    dateTime = dateTime.minusMonths(new Double(Math.random() * 12).intValue());
    dateTime = dateTime.minusDays(new Double(Math.random() * 30).intValue());

    dateTime = dateTime.minusHours(new Double(Math.random() * 24).intValue());
    dateTime = dateTime.minusMinutes(new Double(Math.random() * 60).intValue());
    dateTime = dateTime.minusSeconds(new Double(Math.random() * 60).intValue());

    return dateTime.toDate();
}

From source file:com.epam.ta.reportportal.core.project.impl.GetProjectStatisticHandler.java

License:Open Source License

/**
 * Utility method for calculation of start interval date
 * /*from w  ww  .j  a  v a2s .co  m*/
 * @param input
 * @return
 */
private static Date getStartIntervalDate(InfoInterval input) {
    DateTime now = new DateTime().toDateTime(DateTimeZone.UTC);
    DateTime range = now.minusMonths(input.getCount());
    return range.toDate();
}

From source file:com.google.api.ads.adwords.awreporting.model.entities.dateranges.LastMonthDateRangeHandler.java

License:Open Source License

@Override
public DateTime retrieveDateStart(DateTime date) {
    DateTime minusMonth = date.minusMonths(1);
    return new DateTime(minusMonth.getYear(), minusMonth.getMonthOfYear(), 1, 12, 0);
}

From source file:com.google.api.ads.adwords.awreporting.model.entities.dateranges.LastMonthDateRangeHandler.java

License:Open Source License

@Override
public DateTime retrieveMonth(DateTime date) {
    return date.minusMonths(1);
}

From source file:com.marand.thinkmed.medications.dao.openehr.MedicationsOpenEhrDao.java

License:Open Source License

@EhrSessioned
public Map<Pair<MedicationOrderComposition, MedicationInstructionInstruction>, String> getActiveInstructionPairsWithPatientIds(
        final DateTime when) {
    final StringBuilder sb = new StringBuilder();
    sb.append("SELECT c, i/name/value, e/ehr_status/subject/external_ref/id/value FROM EHR e")
            .append(" CONTAINS Composition c[openEHR-EHR-COMPOSITION.encounter.v1]")
            .append(" CONTAINS Instruction i[openEHR-EHR-INSTRUCTION.medication.v1]")
            .append(" WHERE c/name/value = 'Medication order'").append(" AND c/context/start_time > ")
            .append(getAqlDateTimeQuoted(when.minusMonths(6)));

    appendMedicationTimingIntervalCriterion(sb, Intervals.infiniteFrom(when));

    final Map<Pair<MedicationOrderComposition, MedicationInstructionInstruction>, String> instructionPairWithPatientIds = new HashMap<>();

    try (EhrResult ehrResult = new QueryBuilder(ehrQueryService, ehrSessionManager.getSessionId())
            .poll(100L, 1000L * 30L).withAql(sb.toString()).execute()) {
        while (ehrResult.isActive()) {
            if (ehrResult.hasNext(100L)) {
                final EhrResultRow resultRow = ehrResult.next();
                final MedicationOrderComposition composition = RmoToTdoConverter
                        .convert(MedicationOrderComposition.class, (RmObject) resultRow.get(0));
                final MedicationInstructionInstruction instruction = MedicationsEhrUtils
                        .getMedicationInstructionByEhrName(composition, (String) resultRow.get(1));
                final String patientId = (String) resultRow.get(2);

                instructionPairWithPatientIds.put(Pair.of(composition, instruction), patientId);
            }/*from  w  ww . j av a 2s  .co  m*/
        }
    }

    return instructionPairWithPatientIds;
}