Example usage for org.joda.time Years yearsBetween

List of usage examples for org.joda.time Years yearsBetween

Introduction

In this page you can find the example usage for org.joda.time Years yearsBetween.

Prototype

public static Years yearsBetween(ReadablePartial start, ReadablePartial end) 

Source Link

Document

Creates a Years representing the number of whole years between the two specified partial datetimes.

Usage

From source file:org.egov.infra.utils.DateUtils.java

License:Open Source License

public static int noOfYearsBetween(Date startDate, Date endDate) {
    return Years.yearsBetween(new LocalDate(startDate), new LocalDate(endDate)).getYears();
}

From source file:org.encuestame.utils.DateUtil.java

License:Apache License

/**
 * Get Years Between Dates./*www .jav a2s  .  co  m*/
 * @param startDate
 * @return
 */
public static Integer getYearsBetweenDates(final Date startDate) {
    final DateTime currentDate = new DateTime();
    final DateTime storedDate = new DateTime(startDate);
    final Years yearsBetween = Years.yearsBetween(storedDate, currentDate);
    return yearsBetween.getYears();
}

From source file:org.fam.ejb.model.FamPlayer.java

@PostLoad
@PostPersist/*w ww  .  jav  a 2  s .co m*/
@PostUpdate
void afterLoad() {
    StringBuilder builder = new StringBuilder();
    if (firstName != null) {
        builder.append(firstName);
    }
    if (lastName != null) {
        builder.append(" ").append(lastName);
    }
    displayName = builder.toString();

    if (this.getDtBirth() != null) {
        DateMidnight date = new DateMidnight(this.getDtBirth());
        DateTime now = new DateTime();
        Years y = Years.yearsBetween(date, now);
        age = y.getYears();
    }
    if (this.getDtArrival() != null) {
        DateMidnight date = new DateMidnight(this.getDtArrival());
        DateTime now = new DateTime();
        Years y = Years.yearsBetween(date, now);
        seniority = y.getYears();
    }
}

From source file:org.gnucash.android.model.Recurrence.java

License:Apache License

/**
 * Returns the number of periods from the start date of this recurrence until the end of the
 * interval multiplier specified in the {@link PeriodType}
 * //fixme: Improve the documentation//  w  ww  . jav  a2s .  co m
 * @return Number of periods in this recurrence
 */
public int getNumberOfPeriods(int numberOfPeriods) {
    LocalDate startDate = new LocalDate(mPeriodStart.getTime());
    LocalDate endDate;
    int interval = mPeriodType.getMultiplier();
    //// TODO: 15.08.2016 Why do we add the number of periods. maybe rename method or param
    switch (mPeriodType) {

    case DAY:
        return 1;
    case WEEK:
        endDate = startDate.dayOfWeek().withMaximumValue().plusWeeks(numberOfPeriods);
        return Weeks.weeksBetween(startDate, endDate).getWeeks() / interval;
    case MONTH:
        endDate = startDate.dayOfMonth().withMaximumValue().plusMonths(numberOfPeriods);
        return Months.monthsBetween(startDate, endDate).getMonths() / interval;
    case YEAR:
        endDate = startDate.dayOfYear().withMaximumValue().plusYears(numberOfPeriods);
        return Years.yearsBetween(startDate, endDate).getYears() / interval;
    }

    return 0;
}

From source file:org.gnucash.android.ui.report.BarChartFragment.java

License:Apache License

/**
 * Calculates difference between two date values accordingly to {@code mGroupInterval}
 * @param start start date/*from   w w w  .ja v  a 2s  .co  m*/
 * @param end end date
 * @return difference between two dates or {@code -1}
 */
private int getDateDiff(LocalDateTime start, LocalDateTime end) {
    switch (mGroupInterval) {
    case QUARTER:
        int y = Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
        return (getQuarter(end) - getQuarter(start) + y * 4);
    case MONTH:
        return Months.monthsBetween(start.withDayOfMonth(1).withMillisOfDay(0),
                end.withDayOfMonth(1).withMillisOfDay(0)).getMonths();
    case YEAR:
        return Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
    default:
        return -1;
    }
}

From source file:org.gnucash.android.ui.report.BaseReportFragment.java

License:Apache License

/**
 * Calculates difference between two date values accordingly to {@code mGroupInterval}
 * @param start start date//ww w .j av a 2s .c om
 * @param end end date
 * @return difference between two dates or {@code -1}
 */
protected int getDateDiff(LocalDateTime start, LocalDateTime end) {
    switch (mGroupInterval) {
    case QUARTER:
        int y = Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
        return getQuarter(end) - getQuarter(start) + y * 4;
    case MONTH:
        return Months.monthsBetween(start.withDayOfMonth(1).withMillisOfDay(0),
                end.withDayOfMonth(1).withMillisOfDay(0)).getMonths();
    case YEAR:
        return Years.yearsBetween(start.withDayOfYear(1).withMillisOfDay(0),
                end.withDayOfYear(1).withMillisOfDay(0)).getYears();
    default:
        return -1;
    }
}

From source file:org.helianto.core.domain.Identity.java

License:Apache License

/**
 * <<Transient>> Safe age getter.
 * //  w w  w .  j a  v a 2  s.c om
 * @param date
 */
protected int getAge(Date date) {
    if (getPersonalData() != null && getPersonalData().getBirthDate() != null) {
        DateTime birthdate = new DateTime(getPersonalData().getBirthDate()).withTimeAtStartOfDay();
        return Years.yearsBetween(birthdate, new DateTime(date)).getYears();
    }
    return -1;
}

From source file:org.itechkenya.leavemanager.domain.Contract.java

License:Open Source License

public int calculateContractYear(Date asOf) {
    return Years.yearsBetween(new DateTime(this.getStartDate()), new DateTime(asOf)).getYears() + 1;
}

From source file:org.jahia.taglibs.utility.DateDiffTag.java

License:Open Source License

/**
 * Default processing of the end tag returning EVAL_PAGE.
 *
 * @return EVAL_PAGE//from w ww . j  a  v  a 2 s  .c om
 * @throws javax.servlet.jsp.JspException if an error occurred while processing this tag
 * @see javax.servlet.jsp.tagext.Tag#doEndTag
 */
@Override
public int doEndTag() throws JspException {
    try {
        if ("years".equals(format)) {
            int years = Years.yearsBetween(new DateTime(startDate), new DateTime(endDate)).getYears();
            if (getVar() != null) {
                pageContext.setAttribute(getVar(), new Integer(years), scope);
            } else {
                pageContext.getOut().print(years);
            }
        } else if ("months".equals(format)) {
            int months = Months.monthsBetween(new DateTime(startDate), new DateTime(endDate)).getMonths();
            if (getVar() != null) {
                pageContext.setAttribute(getVar(), new Integer(months), scope);
            } else {
                pageContext.getOut().print(months);
            }
        } else if ("days".equals(format)) {
            int days = Days.daysBetween(new DateTime(startDate), new DateTime(endDate)).getDays();
            if (getVar() != null) {
                pageContext.setAttribute(getVar(), new Integer(days), scope);
            } else {
                pageContext.getOut().print(days);
            }
        }
    } catch (IOException e) {
        throw new JspException(e);
    }
    return SKIP_BODY;
}

From source file:org.kuali.rice.kim.api.identity.personal.EntityBioDemographics.java

License:Educational Community License

/**
 * Helper to parse the birth date for age calculation
 * @param birthDate the birth date in EntityBioDemographicsContract BIRTH_DATE_FORMAT format
 * @param deceasedDate the deceased date in EntityBioDemographicsContract DECEASED_DATE_FORMAT format
 * @param suppressPersonal whether personal information is being suppressed
 * @return the age in years or null if unavailable, suppressed, or an error occurs during calculation
 *///from  w w  w .  j a  v a2 s .  c o  m
private static Integer calculateAge(String birthDate, String deceasedDate, boolean suppressPersonal) {
    if (birthDate != null && !suppressPersonal) {
        Date parsedBirthDate;
        try {
            parsedBirthDate = new SimpleDateFormat(BIRTH_DATE_FORMAT).parse(birthDate);
        } catch (ParseException pe) {
            LOG.error("Error parsing EntityBioDemographics birth date: '" + birthDate + "'", pe);
            return null;
        }
        DateTime endDate;
        if (deceasedDate != null) {
            try {
                endDate = new DateTime(new SimpleDateFormat(BIRTH_DATE_FORMAT).parse(deceasedDate));
            } catch (ParseException pe) {
                LOG.error("Error parsing EntityBioDemographics deceased date: '" + deceasedDate + "'", pe);
                return null;
            }
        } else {
            endDate = new DateTime();
        }
        return Years.yearsBetween(new DateTime(parsedBirthDate), endDate).getYears();
    }
    return null;
}