Example usage for org.joda.time Period getMonths

List of usage examples for org.joda.time Period getMonths

Introduction

In this page you can find the example usage for org.joda.time Period getMonths.

Prototype

public int getMonths() 

Source Link

Document

Gets the months field part of the period.

Usage

From source file:com.jajja.jorm.mixins.Postgres.java

License:Open Source License

public static PGInterval toInterval(Period period) {
    if (period == null) {
        return null;
    }/*from w  ww . j  a  v a  2  s .c  o m*/
    return new PGInterval(period.getYears(), period.getMonths(), period.getDays(), period.getHours(),
            period.getMinutes(), period.getSeconds() + (double) period.getMillis() / 1000);
}

From source file:com.jbirdvegas.mgerrit.search.AgeSearch.java

License:Apache License

/**
 * Adds adjustment to the shortest set time range in period. E.g.
 *  period("5 days 3 hours", 1) -> "5 days 4 hours". This will fall
 *  back to adjusting years if no field in the period is set.
 * @param period The period to be adjusted
 * @param adjustment The adjustment. Note that positive values will result
 *                   in larger periods and an earlier time
 * @return The adjusted period//from w  w w. j a v  a  2 s . c o m
 */
private Period adjust(final Period period, int adjustment) {
    if (adjustment == 0)
        return period;

    // Order is VERY important here
    LinkedHashMap<Integer, DurationFieldType> map = new LinkedHashMap<>();
    map.put(period.getSeconds(), DurationFieldType.seconds());
    map.put(period.getMinutes(), DurationFieldType.minutes());
    map.put(period.getHours(), DurationFieldType.hours());
    map.put(period.getDays(), DurationFieldType.days());
    map.put(period.getWeeks(), DurationFieldType.weeks());
    map.put(period.getMonths(), DurationFieldType.months());
    map.put(period.getYears(), DurationFieldType.years());

    for (Map.Entry<Integer, DurationFieldType> entry : map.entrySet()) {
        if (entry.getKey() > 0) {
            return period.withFieldAdded(entry.getValue(), adjustment);
        }
    }
    // Fall back to modifying years
    return period.withFieldAdded(DurationFieldType.years(), adjustment);
}

From source file:com.jbirdvegas.mgerrit.search.AgeSearch.java

License:Apache License

/**
 * Calculates the number of days spanned in a period assuming 365 days per year, 30 days per
 * month, 7 days per week, 24 hours per day, 60 minutes per hour and 60 seconds per minute.
 * @param period A period to retrieve the number of standard days for
 * @return The number of days spanned by the period.
 */// ww  w . j av a2s. c o  m
protected static int getDaysInPeriod(final Period period) {
    int totalDays = 0;
    Period temp = new Period(period);
    if (period.getYears() > 0) {
        int years = period.getYears();
        totalDays += 365 * years;
        temp = temp.minusYears(years);
    }
    if (period.getMonths() > 0) {
        int months = period.getMonths();
        totalDays += 30 * period.getMonths();
        temp = temp.minusMonths(months);
    }
    return totalDays + temp.toStandardDays().getDays();
}

From source file:com.mobileman.kuravis.core.util.DateTimeUtils.java

License:Apache License

/**
 * @param date/*from  ww  w.  j a  va  2s.co  m*/
 * @return formatted elapsed time from now
 */
public static String fmtElapsedTime(Date date) {
    if (date == null) {
        return "";
    }
    Period period = new Period(date.getTime(), Calendar.getInstance().getTimeInMillis());
    PeriodFormatterBuilder pf = new PeriodFormatterBuilder();
    pf.appendPrefix(" vor ");
    if (period.getYears() > 0) {
        pf.appendYears().appendSuffix(" Jahr", " Jahren");
    } else if (period.getMonths() > 0) {
        pf.appendMonths().appendSuffix(" Monat", " Monaten");
    } else if (period.getWeeks() > 0) {
        pf.appendWeeks().appendSuffix(" Woche ", " Wochen");
    } else if (period.getDays() > 0) {
        pf.appendDays().appendSuffix(" Tag ", " Tagen");
    } else if (period.getHours() > 0) {
        pf.appendHours().appendSuffix(" Stunde ", " Stunden");
    } else if (period.getMinutes() > 0) {
        pf.appendMinutes().appendSuffix(" Minute ", " Minuten");
    } else if (period.getSeconds() > 0) {
        pf.appendSeconds().appendSuffix(" Sekunde ", " Sekunden");
    }
    return pf.toFormatter().print(period);
}

From source file:com.okmich.hackerday.client.tool.dashboard.UserByPeriodHandler.java

private void increasePoints(long mints, long maxts) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(mints);/*from   ww  w  .j av a2 s.c o  m*/

    Calendar cal2 = Calendar.getInstance();
    cal.setTimeInMillis(maxts);

    Period period = new Period(LocalDate.fromCalendarFields(cal), LocalDate.fromCalendarFields(cal2));

    if (period.getDays() <= 7) {
        increasePointValue(ITEM1);
    } else if (period.getMonths() <= 1) {
        increasePointValue(ITEM2);
    } else if (period.getYears() <= 1) {
        increasePointValue(ITEM3);
    } else {
        increasePointValue(ITEM4);
    }
}

From source file:com.reclabs.recomendar.common.helpers.types.DateHelper.java

License:Open Source License

/**
 * Dada una fecha de referencia y una fecha a comparar obtenemos la diferencia de la primera con la segunda, el
 * formato depender del rango de la diferencia, es decir, si hay menos de
 * un mes de diferencia obtendremos la diferencia en das.
 * @param referenceDate The date of reference
 * @param compareDate The date to compare
 * @return Diferencia entre las fechas//from  ww w  . j  a v a  2  s  .  c om
 */
public static String getDifToCompareDateInUnitTimeLowRound(final Date referenceDate, final Date compareDate) {
    Period period = new Period(compareDate.getTime(), referenceDate.getTime(),
            PeriodType.yearMonthDayTime().withSecondsRemoved().withMillisRemoved());
    if ((period.getYears() > 0) || (period.getMonths() > 0) || (period.getDays() > 0)) {
        return REDUCED_DATE_FORMAT.format(compareDate);
    } else if (period.getHours() > 0) {
        return period.getHours() + " Hora" + (period.getHours() > 1 ? "s" : "");
    } else if (period.getMinutes() > 0) {
        return period.getMinutes() + " Minuto" + (period.getMinutes() > 1 ? "s" : "");
    } else {
        return "Ahora";
    }
}

From source file:com.sap.dirigible.runtime.metrics.TimeUtils.java

License:Open Source License

private static DateTime dateTimeCeiling(DateTime dt, Period p) {
    if (p.getYears() != 0) {
        return dt.yearOfEra().roundCeilingCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundCeilingCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundCeilingCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundCeilingCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundCeilingCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundCeilingCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundCeilingCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }/*  www  . ja v a2s  . c o m*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

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();
        ////from  w  w w .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;
}

From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java

License:Open Source License

private Period computeOffset(IntervalleObject presentInterval, IntervalleObject pastInterval,
        AxisValues joinAxis) throws ScopeException {
    // it is better to compare on the lower bound because alignment on the
    // end of month is not accurate
    Object present = presentInterval.getLowerBound();
    Object past = pastInterval.getLowerBound();
    ///*  www. j  a va2s.  co  m*/

    IDomain image = joinAxis.getAxis().getDefinition().getImageDomain();
    PeriodType type = computePeriodType(image);
    //
    if (present instanceof Date && past instanceof Date) {

        Period presentPeriod = new Period(new LocalDate(((Date) presentInterval.getLowerBound()).getTime()),
                new LocalDate(((Date) presentInterval.getUpperBound()).getTime()).plusDays(1), type);

        Period pastPeriod = new Period(new LocalDate(((Date) pastInterval.getLowerBound()).getTime()),
                new LocalDate(((Date) pastInterval.getUpperBound()).getTime()).plusDays(1), type);

        Date pastDate = (Date) past;
        DateTime dt = new DateTime(pastDate);
        if (image.isInstanceOf(IDomain.YEARLY)) {
            if (presentPeriod.getYears() > pastPeriod.getYears()) {
                // e.g. presentPeriod of 365 days ->
                // presentPeriod.getYears=1 && past year of 366 days ->
                // pastPeriod.getYears=0
                DateTime newDT = new DateTime(dt.getYear(), 1, 1, 0, 0);
                pastDate = newDT.toDate();
            } else {
                if (dt.getDayOfYear() != 1) {
                    // e.g present period of 366 days -> past date at dec 31
                    DateTime newDT = new DateTime(dt.getYear() + 1, 1, 1, 0, 0);
                    pastDate = newDT.toDate();
                }
            }
        } else if (image.isInstanceOf(IDomain.QUATERLY) || image.isInstanceOf(IDomain.MONTHLY)) {

            if (presentPeriod.getMonths() > pastPeriod.getMonths()) {
                // e.g present period of 28 days(February) ->
                // pastPeriod.getMonths() = 0 (January has 31 days)
                DateTime newDT = new DateTime(dt.getYear(), dt.getMonthOfYear(), 1, 0, 0);
                pastDate = newDT.toDate();
            } else {
                if (dt.getDayOfMonth() != 1) {
                    // e.g. present period of 31 day(March) pastDate = Feb 6
                    if (dt.getMonthOfYear() == 12) {
                        DateTime newDT = new DateTime(dt.getYear() + 1, 1, 1, 0, 0);
                        pastDate = newDT.toDate();
                    } else {
                        DateTime newDT = new DateTime(dt.getYear(), dt.getMonthOfYear() + 1, 1, 0, 0);
                        pastDate = newDT.toDate();
                    }
                }
            }
        } else {
            // daily, keep Date as it is
        }
        return new Period(new LocalDate((pastDate).getTime()), new LocalDate(((Date) present).getTime()), type);

    } else {
        return null;
    }
}

From source file:control.EmprestimoController.java

private static TableModel UpdateTablemodel(TableModel tb) {
    LocalDateTime hoje = new LocalDateTime(System.currentTimeMillis());
    for (int i = 0; i < tb.getRowCount(); i++) {
        String in = tb.getValueAt(i, 3).toString();
        String out = tb.getValueAt(i, 4).toString();
        LocalDateTime inicio = new LocalDateTime(in);
        LocalDateTime fim = new LocalDateTime(out);
        tb.setValueAt("" + inicio.getDayOfMonth() + "/" + inicio.getMonthOfYear() + "/" + inicio.getYear() + "",
                i, 3);//from   w  ww.j  ava  2 s  . com
        tb.setValueAt("" + fim.getDayOfMonth() + "/" + fim.getMonthOfYear() + "/" + fim.getYear() + "", i, 4);

        Period p = new Period(hoje, fim);
        int dias = p.getDays();
        int horas = p.getHours();
        int month = p.getMonths();
        //System.out.println(dias+":"+horas+" :: ENTRE AS DATAS "+fim.toString()+" :: "+hoje.toString());
        if (dias < 1) {
            if (dias == 0) {
                tb.setValueAt("Atraso " + horas * -1 + "h", i, 5);
                if (horas > 0)
                    tb.setValueAt("Restam " + horas + "h", i, 5);
            } else
                tb.setValueAt("Atraso " + dias * -1 + "d:" + horas * -1 + "h", i, 5);

        }
        /*else 
        if (month >= 0)
            tb.setValueAt("Restam "+dias+"d:"+horas+"h", i, 5); 
        else
            tb.setValueAt("Restam "+month+" meses", i, 5); */
    }
    return tb;
}