Example usage for org.joda.time Period getYears

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

Introduction

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

Prototype

public int getYears() 

Source Link

Document

Gets the years field part of the period.

Usage

From source file:com.gst.portfolio.loanaccount.loanschedule.domain.LoanApplicationTerms.java

License:Apache License

private Integer getPeriodsBetween(LocalDate fromDate, LocalDate toDate) {
    Integer numberOfPeriods = 0;/*from  w  ww  . j  av  a2  s .  c o m*/
    PeriodType periodType = PeriodType.yearMonthDay();
    Period difference = new Period(fromDate, toDate, periodType);
    switch (this.repaymentPeriodFrequencyType) {
    case DAYS:
        numberOfPeriods = difference.getDays();
        break;
    case WEEKS:
        periodType = PeriodType.weeks();
        difference = new Period(fromDate, toDate, periodType);
        numberOfPeriods = difference.getWeeks();
        break;
    case MONTHS:
        numberOfPeriods = difference.getMonths();
        break;
    case YEARS:
        numberOfPeriods = difference.getYears();
        break;
    default:
        break;
    }
    return numberOfPeriods;
}

From source file:com.hendi.cekusia.Joda.java

public static void main(String[] args) {
    LocalDate birthdate = new LocalDate(1986, 9, 19); //Birth date
    LocalDate now = new LocalDate(); //Today's date
    Period period = new Period(birthdate, now, PeriodType.yearMonthDay());
    //Now access the values as below
    System.out.println("Using Joda Time");
    System.out.println("Usia : " + period.getYears() + " Tahun " + period.getMonths() + " Bulan "
            + period.getDays() + " Hari ");
    System.out.println();//w w w.  j a  v  a  2s .  c om

}

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  w w. 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/*  w w w  .  j a va2s  .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.
 *///from w  w  w .j a v a2  s.  co 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 www  . j  av  a 2  s  .c om
 * @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 w  w w.j  av  a 2s  .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.peertopark.java.dates.BirthdayUtils.java

public static boolean isAdult(Date actualDate, Date birthday, int yearsForAdult) {
    Period period = new Period(birthday.getTime(), actualDate.getTime());
    return period.getYears() >= yearsForAdult;
}

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   w w  w. j  a  va 2  s. c o  m
 */
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());
    }/*  w  ww. ja  va2  s . co m*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}