Java Code Examples for java.time.Period#isNegative()

The following examples show how to use java.time.Period#isNegative() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDurationLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
private javax.xml.datatype.Duration toYearsMonthDuration(DatatypeFactory datatypeFactory, LocalDate date1, LocalDate date2) {
    Period between = Period.between(date2, date1);
    int years = between.getYears();
    int months = between.getMonths();
    if (between.isNegative()) {
        years = - years;
        months = - months;
    }
    return datatypeFactory.newDurationYearMonth(!between.isNegative(), years, months);
}
 
Example 2
Source File: DefaultDurationLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
private Duration toYearsMonthDuration(DatatypeFactory datatypeFactory, LocalDate date1, LocalDate date2) {
    Period between = Period.between(date2, date1);
    int years = between.getYears();
    int months = between.getMonths();
    if (between.isNegative()) {
        years = - years;
        months = - months;
    }
    return datatypeFactory.newDurationYearMonth(!between.isNegative(), years, months);
}
 
Example 3
Source File: CypherDuration.java    From vertexium with Apache License 2.0 5 votes vote down vote up
public CypherDuration(Period period, Duration duration) {
    this.negative = (period.isNegative() || duration.isNegative());

    if (period.isNegative()) {
        period = period.negated();
    }
    if (duration.isNegative()) {
        duration = duration.negated();
    }

    this.nanos = ((duration.getSeconds() * NANOS_IN_SECOND) + duration.getNano()) % NANOS_IN_DAY;
    this.period = period.minusDays(this.nanos > 0 ? 1 : 0);
}
 
Example 4
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Supports the unary plus operator; returns a {@link java.time.Period} with all unit values positive.
 * For example, a period of "2 years, -3 months, and -4 days" would result in a period of
 * "2 years, 3 months, and 4 days." No normalization is performed.
 *
 * @param self a Period
 * @return a positive Period
 * @since 2.5.0
 */
public static Period positive(final Period self) {
    return !self.isNegative() ? self : self.withDays(Math.abs(self.getDays()))
            .withMonths(Math.abs(self.getMonths()))
            .withYears(Math.abs(self.getYears()));
}