DateTime myBirthDate = new DateTime(1978, 3, 26, 0, 0, 0, 0); DateTime now = new DateTime(); Period period = new Period(myBirthDate, now); int years = period.getYears(); System.out.println(years); // 32
/** * Check that there are no years or months in the period. * * @param destintionType the destination type, not null * @throws UnsupportedOperationException if the period contains years or months */ private void checkYearsAndMonths(String destintionType) { if (getMonths() != 0) { throw new UnsupportedOperationException("Cannot convert to " + destintionType + " as this period contains months and months vary in length"); } if (getYears() != 0) { throw new UnsupportedOperationException("Cannot convert to " + destintionType + " as this period contains years and years vary in length"); } }
/** * Check that there are no years or months in the period. * * @param destintionType the destination type, not null * @throws UnsupportedOperationException if the period contains years or months */ private void checkYearsAndMonths(String destintionType) { if (getMonths() != 0) { throw new UnsupportedOperationException("Cannot convert to " + destintionType + " as this period contains months and months vary in length"); } if (getYears() != 0) { throw new UnsupportedOperationException("Cannot convert to " + destintionType + " as this period contains years and years vary in length"); } }
Period period = new Period(d1, d2); System.out.print(period.getYears() + " years, "); System.out.print(period.getMonths() + " months, "); // ...
public static int periodToMonths(Period value) { return value.getYears() * yearsToMonths + value.getMonths(); }
public static int monthsFromPeriod(Period period){ return (period.getYears() * yearsToMonths) + period.getMonths(); }
LocalDate today = LocalDate.now(); LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1); Period p = Period.between(birthday, today); long p2 = ChronoUnit.DAYS.between(birthday, today); System.out.println("You are " + p.getYears() + " years, " + p.getMonths() + " months, and " + p.getDays() + " days old. (" + p2 + " days total)");
public static StringBuilder intervalYearStringBuilder(Period value) { return intervalYearStringBuilder( value.getYears() * 12 + value.getMonths()); }
/** * Creates a new <code>Years</code> by parsing a string in the ISO8601 format 'PnY'. * <p> * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the * years component may be non-zero. If any other component is non-zero, an exception * will be thrown. * * @param periodStr the period string, null returns zero * @return the period in years * @throws IllegalArgumentException if the string format is invalid */ @FromString public static Years parseYears(String periodStr) { if (periodStr == null) { return Years.ZERO; } Period p = PARSER.parsePeriod(periodStr); return Years.years(p.getYears()); }
/** * Creates a new <code>Years</code> by parsing a string in the ISO8601 format 'PnY'. * <p> * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the * years component may be non-zero. If any other component is non-zero, an exception * will be thrown. * * @param periodStr the period string, null returns zero * @return the period in years * @throws IllegalArgumentException if the string format is invalid */ @FromString public static Years parseYears(String periodStr) { if (periodStr == null) { return Years.ZERO; } Period p = PARSER.parsePeriod(periodStr); return Years.years(p.getYears()); }
Interval interval = new Interval(d1.getTime(), d2.getTime()); Period period = interval.toPeriod(); System.out.printf( "%d years, %d months, %d days, %d hours, %d minutes, %d seconds%n", period.getYears(), period.getMonths(), period.getDays(), period.getHours(), period.getMinutes(), period.getSeconds());
public static StringBuilder intervalStringBuilder(Period value) { return intervalStringBuilder( value.getYears() * 12 + value.getMonths(), value.getDays(), periodToMillis(value)); }
private static boolean periodIsDayMultiple(final Period period) { return period.getMillis() == 0 && period.getSeconds() == 0 && period.getMinutes() == 0 && period.getHours() == 0 && (period.getDays() > 0 || period.getWeeks() > 0 || period.getMonths() > 0 || period.getYears() > 0); }
curStdOffset == nextStdOffset && curNameKey.equals(nextNameKey) && p.getYears() == 0 && p.getMonths() > 4 && p.getMonths() < 8 && curNameKey.equals(zoneNameData[2]) && curNameKey.equals(zoneNameData[4])) {
curStdOffset == nextStdOffset && curNameKey.equals(nextNameKey) && p.getYears() == 0 && p.getMonths() > 4 && p.getMonths() < 8 && curNameKey.equals(zoneNameData[2]) && curNameKey.equals(zoneNameData[4])) {
public static LocalDate alignProposedBillCycleDate(final LocalDate proposedDate, final int billingCycleDay, final BillingPeriod billingPeriod) { // billingCycleDay alignment only makes sense for month based BillingPeriod (MONTHLY, QUARTERLY, BIANNUAL, ANNUAL) final boolean isMonthBased = (billingPeriod.getPeriod().getMonths() | billingPeriod.getPeriod().getYears()) > 0; if (!isMonthBased) { return proposedDate; } final int lastDayOfMonth = proposedDate.dayOfMonth().getMaximumValue(); int proposedBillCycleDate = proposedDate.getDayOfMonth(); if (proposedBillCycleDate < billingCycleDay) { if (billingCycleDay <= lastDayOfMonth) { proposedBillCycleDate = billingCycleDay; } else { proposedBillCycleDate = lastDayOfMonth; } } return new LocalDate(proposedDate.getYear(), proposedDate.getMonthOfYear(), proposedBillCycleDate, proposedDate.getChronology()); }
int years = normalized.getYears(); int months = normalized.getMonths(); int weeks = normalized.getWeeks();
millis += (((long) getWeeks()) * ((long) DateTimeConstants.MILLIS_PER_WEEK)); Period result = new Period(millis, type, ISOChronology.getInstanceUTC()); int years = getYears(); int months = getMonths(); if (years != 0 || months != 0) {
millis += (((long) getWeeks()) * ((long) DateTimeConstants.MILLIS_PER_WEEK)); Period result = new Period(millis, type, ISOChronology.getInstanceUTC()); int years = getYears(); int months = getMonths(); if (years != 0 || months != 0) {
unsupported = unsupported.withMonths(0); if (JodaTimeUtils.hasField(unsupported, DurationFieldType.years()) && unsupported.getYears() % 10 == 0 && JodaTimeUtils.hasField(p.base, JodaTimeUtils.DecadeOfCentury)) { Partial p2 = p.base.withFieldAddWrapped(JodaTimeUtils.Decades, unsupported.getYears() / 10); p = new PartialTime(p, p2); unsupported = unsupported.withYears(0); if (JodaTimeUtils.hasField(unsupported, DurationFieldType.years()) && unsupported.getYears() % 100 == 0 && JodaTimeUtils.hasField(p.base, DateTimeFieldType.centuryOfEra())) { Partial p2 = p.base.withField(DateTimeFieldType.centuryOfEra(), p.base.get(DateTimeFieldType.centuryOfEra()) + unsupported.getYears() / 100); p = new PartialTime(p, p2); unsupported = unsupported.withYears(0);