Example usage for org.joda.time.field FieldUtils safeAdd

List of usage examples for org.joda.time.field FieldUtils safeAdd

Introduction

In this page you can find the example usage for org.joda.time.field FieldUtils safeAdd.

Prototype

public static long safeAdd(long val1, long val2) 

Source Link

Document

Add two values throwing an exception if overflow occurs.

Usage

From source file:influent.server.utilities.DateTimeParser.java

License:MIT License

/**
 * @see http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html#normalizedStandard()
 *//*w w w  .j  av a  2s .c o  m*/
public static Period normalize(Period period) {
    long millis = period.getMillis();
    millis += period.getSeconds() * DateTimeConstants.MILLIS_PER_SECOND;
    millis += period.getMinutes() * DateTimeConstants.MILLIS_PER_MINUTE;
    millis += period.getHours() * DateTimeConstants.MILLIS_PER_HOUR;
    millis += period.getDays() * DateTimeConstants.MILLIS_PER_DAY;
    millis += period.getWeeks() * DateTimeConstants.MILLIS_PER_WEEK;

    Period result = new Period(millis, DateTimeUtils.getPeriodType(PeriodType.standard()),
            ISOChronology.getInstanceUTC());
    int years = period.getYears();
    int months = period.getMonths();

    if (years != 0 || months != 0) {
        years = FieldUtils.safeAdd(years, months / 12);
        months = months % 12;
        if (years != 0) {
            result = result.withYears(years);
        }
        if (months != 0) {
            result = result.withMonths(months);
        }
    }

    return result;
}