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

The following examples show how to use java.time.Period#isZero() . 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: StringColumnPeriodMapper.java    From jadira with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a string representation of the amount of time.
 * @param value Period to convert to a String
 * @return the amount of time in ISO8601 string format
 */
public String toString(Period value) {

    final String str;
    if (value.isZero()) {
        str = "PT0S";
    } else {
        StringBuilder buf = new StringBuilder();
        buf.append('P');
        if (value.getYears() != 0) {
            buf.append(value.getYears()).append('Y');
        }
        if (value.getMonths() != 0) {
            buf.append(value.getMonths()).append('M');
        }
        if (value.getDays() != 0) {
            buf.append(value.getDays()).append('D');
        }
        str = buf.toString();
    }
    return str;
}
 
Example 2
Source File: FpmlDocument.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an FpML 'RelativeDateOffset' to a {@code DaysAdjustment}.
 * 
 * @param baseEl  the FpML adjustable date element
 * @return the days adjustment
 * @throws RuntimeException if unable to parse
 */
public DaysAdjustment parseRelativeDateOffsetDays(XmlElement baseEl) {
  // FpML content: ('periodMultiplier', 'period', 'dayType?',
  //                'businessDayConvention', 'BusinessCentersOrReference.model?'
  //                'dateRelativeTo', 'adjustedDate')
  // The 'dateRelativeTo' element is not used here
  // The 'adjustedDate' element is ignored
  Period period = parsePeriod(baseEl);
  if (period.toTotalMonths() != 0) {
    throw new FpmlParseException("Expected days-based period but found " + period);
  }
  Optional<XmlElement> dayTypeEl = baseEl.findChild("dayType");
  boolean calendarDays = period.isZero() || (dayTypeEl.isPresent() && dayTypeEl.get().getContent().equals("Calendar"));
  BusinessDayConvention fixingBdc = convertBusinessDayConvention(
      baseEl.getChild("businessDayConvention").getContent());
  HolidayCalendarId calendar = parseBusinessCenters(baseEl);
  if (calendarDays) {
    return DaysAdjustment.ofCalendarDays(
        period.getDays(), BusinessDayAdjustment.of(fixingBdc, calendar));
  } else {
    return DaysAdjustment.ofBusinessDays(period.getDays(), calendar);
  }
}
 
Example 3
Source File: StringFormatUtils.java    From datakernel with Apache License 2.0 6 votes vote down vote up
public static String formatPeriod(Period value) {
	if (value.isZero()) {
		return "0 days";
	}
	String result = "";
	int years = value.getYears(), months = value.getMonths(),
			days = value.getDays();
	if (years != 0) {
		result += years + " years ";
	}
	if (months != 0) {
		result += months + " months ";
	}
	if (days != 0) {
		result += days + " days ";
	}
	return result.trim();
}
 
Example 4
Source File: Parsed.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 5
Source File: Parsed.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 6
Source File: Parsed.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 7
Source File: Parsed.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 8
Source File: Parsed.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 9
Source File: Parsed.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 10
Source File: Parsed.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 11
Source File: Parsed.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 12
Source File: Parsed.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 13
Source File: Parsed.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 14
Source File: Parsed.java    From desugar_jdk_libs with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 15
Source File: Parsed.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 16
Source File: FpmlDocument.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an FpML 'AdjustedRelativeDateOffset' to a resolved {@code LocalDate}.
 * 
 * @param baseEl  the FpML adjustable date element
 * @return the resolved date
 * @throws RuntimeException if unable to parse
 */
public AdjustableDate parseAdjustedRelativeDateOffset(XmlElement baseEl) {
  // FpML content: ('periodMultiplier', 'period', 'dayType?',
  //                'businessDayConvention', 'BusinessCentersOrReference.model?'
  //                'dateRelativeTo', 'adjustedDate', 'relativeDateAdjustments?')
  // The 'adjustedDate' element is ignored
  XmlElement relativeToEl = lookupReference(baseEl.getChild("dateRelativeTo"));
  LocalDate baseDate;
  if (relativeToEl.hasContent()) {
    baseDate = parseDate(relativeToEl);
  } else if (relativeToEl.getName().contains("relative")) {
    baseDate = parseAdjustedRelativeDateOffset(relativeToEl).getUnadjusted();
  } else {
    throw new FpmlParseException(
        "Unable to resolve 'dateRelativeTo' to a date: " + baseEl.getChild("dateRelativeTo").getAttribute(HREF));
  }
  Period period = parsePeriod(baseEl);
  Optional<XmlElement> dayTypeEl = baseEl.findChild("dayType");
  boolean calendarDays = period.isZero() || (dayTypeEl.isPresent() && dayTypeEl.get().getContent().equals("Calendar"));
  BusinessDayAdjustment bda1 = parseBusinessDayAdjustments(baseEl);
  BusinessDayAdjustment bda2 = baseEl.findChild("relativeDateAdjustments")
      .map(el -> parseBusinessDayAdjustments(el))
      .orElse(bda1);
  // interpret and resolve, simple calendar arithmetic or business days
  LocalDate resolvedDate;
  if (period.getYears() > 0 || period.getMonths() > 0 || calendarDays) {
    resolvedDate = bda1.adjust(baseDate.plus(period), refData);
  } else {
    LocalDate datePlusBusDays = bda1.getCalendar().resolve(refData).shift(baseDate, period.getDays());
    resolvedDate = bda1.adjust(datePlusBusDays, refData);
  }
  return AdjustableDate.of(resolvedDate, bda2);
}
 
Example 17
Source File: Parsed.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 18
Source File: Parsed.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {
    if (time != null) {
        if (time.equals(timeToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);
        }
        if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {
            throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);
        } else {
            excessDays = periodToSet;
        }
    } else {
        time = timeToSet;
        excessDays = periodToSet;
    }
}
 
Example 19
Source File: TimeParser.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
/** Format a temporal amount
 *
 *  @param amount {@link TemporalAmount}
 *  @return Text like "2 days" that {@link #parseTemporalAmount(String)} can parse
 */
public static String format(final TemporalAmount amount)
{
    final StringBuilder buf = new StringBuilder();
    if (amount instanceof Period)
    {
        final Period period = (Period) amount;
        if (period.isZero())
            return NOW;

        if (period.getYears() == 1)
            buf.append("1 year ");
        else if (period.getYears() > 1)
            buf.append(period.getYears()).append(" years ");

        if (period.getMonths() == 1)
            buf.append("1 month ");
        else if (period.getMonths() > 0)
            buf.append(period.getMonths()).append(" months ");

        if (period.getDays() == 1)
            buf.append("1 day");
        else if (period.getDays() > 0)
            buf.append(period.getDays()).append(" days");
    }
    else
    {
        long secs = ((Duration) amount).getSeconds();
        if (secs == 0)
            return NOW;

        int p = (int) (secs / (24*60*60));
        if (p > 0)
        {
            if (p == 1)
                buf.append("1 day ");
            else
                buf.append(p).append(" days ");
            secs -= p * (24*60*60);
        }

        p = (int) (secs / (60*60));
        if (p > 0)
        {
            if (p == 1)
                buf.append("1 hour ");
            else
                buf.append(p).append(" hours ");
            secs -= p * (60*60);
        }

        p = (int) (secs / (60));
        if (p > 0)
        {
            if (p == 1)
                buf.append("1 minute ");
            else
                buf.append(p).append(" minutes ");
            secs -= p * (60);
        }

        if (secs > 0)
            if (secs == 1)
                buf.append("1 second ");
            else
                buf.append(secs).append(" seconds ");

        final int ms = ((Duration)amount).getNano() / 1000000;
        if (ms > 0)
            buf.append(ms).append(" ms");
    }
    return buf.toString().trim();
}
 
Example 20
Source File: TimeWarp.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
/** @param amount Relative time span
 *  @return Legacy specification, e.g. "-3 days -3.124 seconds"
 */
public static String formatAsLegacy(final TemporalAmount amount)
{
    final StringBuilder buf = new StringBuilder();

    if (amount instanceof Period)
    {
        final Period period = (Period) amount;
        if (period.isZero())
            return "now";
        if (period.getYears() > 1)
            buf.append(-period.getYears()).append(" years ");
        if (period.getMonths() > 0)
            buf.append(-period.getMonths()).append(" months ");
        if (period.getDays() > 0)
            buf.append(-period.getDays()).append(" days");
    }
    else
    {
        long secs = ((Duration) amount).getSeconds();
        if (secs == 0)
            return "now";

        int p = (int) (secs / (24*60*60));
        if (p > 0)
        {
            buf.append(-p).append(" days ");
            secs -= p * (24*60*60);
        }

        p = (int) (secs / (60*60));
        if (p > 0)
        {
            buf.append(-p).append(" hours ");
            secs -= p * (60*60);
        }

        p = (int) (secs / (60));
        if (p > 0)
        {
            buf.append(-p).append(" minutes ");
            secs -= p * (60);
        }

        final long ms = ((Duration) amount).getNano() / 1000000;
        if (ms > 0)
            buf.append(-secs - ms / 1000.0).append(" seconds");
        else
            if (secs > 0)
                buf.append(-secs).append(" seconds");
    }
    return buf.toString().trim();
}