org.joda.time.base.BaseSingleFieldPeriod Java Examples

The following examples show how to use org.joda.time.base.BaseSingleFieldPeriod. 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: BaseSingleFieldPeriodRelay.java    From jfixture with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("EqualsBetweenInconvertibleTypes") // SpecimenType knows how to do equals(Class<?>)
public Object create(Object request, SpecimenContext context) {

    if (!(request instanceof SpecimenType)) {
        return new NoSpecimen();
    }

    SpecimenType type = (SpecimenType) request;
    if (!BaseSingleFieldPeriod.class.isAssignableFrom(type.getRawType())) {
        return new NoSpecimen();
    }

    Duration duration = (Duration) context.resolve(Duration.class);
    if (type.equals(Seconds.class)) return Seconds.seconds(Math.max(1, (int) duration.getStandardSeconds()));
    if (type.equals(Minutes.class)) return Minutes.minutes(Math.max(1, (int) duration.getStandardMinutes()));
    if (type.equals(Hours.class)) return Hours.hours(Math.max(1, (int) duration.getStandardHours()));

    if (type.equals(Days.class)) return Days.days(Math.max(1, (int) duration.getStandardDays()));
    if (type.equals(Weeks.class)) return Weeks.weeks(Math.max(1, (int) duration.getStandardDays() / 7));
    if (type.equals(Months.class)) return Months.months(Math.max(1, (int) duration.getStandardDays() / 30));
    if (type.equals(Years.class)) return Years.years(Math.max(1, (int) duration.getStandardDays() / 365));

    return new NoSpecimen();
}
 
Example #2
Source File: AcademicPeriod.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int compareTo(BaseSingleFieldPeriod other) {
    if (!(other instanceof AcademicPeriod)) {
        throw new ClassCastException(getClass() + " cannot be compared to " + other.getClass());
    }

    AcademicPeriod otherAcademicPeriod = (AcademicPeriod) other;
    if (getWeight() > otherAcademicPeriod.getWeight()) {
        return -1;
    } else if (getWeight() < otherAcademicPeriod.getWeight()) {
        return 1;
    }

    return 0;
}
 
Example #3
Source File: JodaTimeSingleFieldPeriodConverter.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return targetTypeToken.isSubtypeOf(BaseSingleFieldPeriod.class)
            && ((source instanceof Number) || (stringToNumberConverter.canHandle(source, TypeToken.of(Long.class))));
}
 
Example #4
Source File: TestBaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
    return BaseSingleFieldPeriod.standardPeriodIn(period, millisPerUnit);
}
 
Example #5
Source File: TestBaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) {
    return BaseSingleFieldPeriod.between(start, end, zeroInstance);
}
 
Example #6
Source File: TestBaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) {
    return BaseSingleFieldPeriod.between(start, end, field);
}
 
Example #7
Source File: TestBaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) {
    return BaseSingleFieldPeriod.between(start, end, field);
}
 
Example #8
Source File: TestBaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) {
    return BaseSingleFieldPeriod.between(start, end, zeroInstance);
}
 
Example #9
Source File: TestBaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
    return BaseSingleFieldPeriod.standardPeriodIn(period, millisPerUnit);
}
 
Example #10
Source File: Years.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Years</code> representing the number of whole years
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in years
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Years yearsBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalDate && end instanceof LocalDate)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int years = chrono.years().getDifference(
                ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis());
        return Years.years(years);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Years.years(amount);
}
 
Example #11
Source File: Minutes.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Minutes</code> representing the number of whole minutes
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalTime</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in minutes
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Minutes minutesBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalTime && end instanceof LocalTime)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int minutes = chrono.minutes().getDifference(
                ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis());
        return Minutes.minutes(minutes);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Minutes.minutes(amount);
}
 
Example #12
Source File: Years.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Years</code> representing the number of whole years
 * in the specified interval. This method corectly handles any daylight
 * savings time changes that may occur during the interval.
 *
 * @param interval  the interval to extract years from, null returns zero
 * @return the period in years
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Years yearsIn(ReadableInterval interval) {
    if (interval == null)   {
        return Years.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.years());
    return Years.years(amount);
}
 
Example #13
Source File: Minutes.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Minutes</code> representing the number of whole minutes
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalTime</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in minutes
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Minutes minutesBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalTime && end instanceof LocalTime)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int minutes = chrono.minutes().getDifference(
                ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis());
        return Minutes.minutes(minutes);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Minutes.minutes(amount);
}
 
Example #14
Source File: Minutes.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Minutes</code> representing the number of whole minutes
 * in the specified interval.
 *
 * @param interval  the interval to extract minutes from, null returns zero
 * @return the period in minutes
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Minutes minutesIn(ReadableInterval interval) {
    if (interval == null)   {
        return Minutes.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.minutes());
    return Minutes.minutes(amount);
}
 
Example #15
Source File: Weeks.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Weeks</code> representing the number of whole weeks
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in weeks
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Weeks weeksBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalDate && end instanceof LocalDate)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int weeks = chrono.weeks().getDifference(
                ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis());
        return Weeks.weeks(weeks);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Weeks.weeks(amount);
}
 
Example #16
Source File: Weeks.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Weeks</code> representing the number of whole weeks
 * in the specified interval.
 *
 * @param interval  the interval to extract weeks from, null returns zero
 * @return the period in weeks
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Weeks weeksIn(ReadableInterval interval) {
    if (interval == null)   {
        return Weeks.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.weeks());
    return Weeks.weeks(amount);
}
 
Example #17
Source File: Seconds.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Seconds</code> representing the number of whole seconds
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalTime</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in seconds
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Seconds secondsBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalTime && end instanceof LocalTime)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int seconds = chrono.seconds().getDifference(
                ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis());
        return Seconds.seconds(seconds);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Seconds.seconds(amount);
}
 
Example #18
Source File: Seconds.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Seconds</code> representing the number of whole seconds
 * in the specified interval.
 *
 * @param interval  the interval to extract seconds from, null returns zero
 * @return the period in seconds
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Seconds secondsIn(ReadableInterval interval) {
    if (interval == null)   {
        return Seconds.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.seconds());
    return Seconds.seconds(amount);
}
 
Example #19
Source File: Days.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Days</code> representing the number of whole days
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in days
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Days daysBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalDate && end instanceof LocalDate)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int days = chrono.days().getDifference(
                ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis());
        return Days.days(days);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Days.days(amount);
}
 
Example #20
Source File: Days.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Days</code> representing the number of whole days
 * in the specified interval. This method corectly handles any daylight
 * savings time changes that may occur during the interval.
 *
 * @param interval  the interval to extract days from, null returns zero
 * @return the period in days
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Days daysIn(ReadableInterval interval) {
    if (interval == null)   {
        return Days.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.days());
    return Days.days(amount);
}
 
Example #21
Source File: Months.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Months</code> representing the number of whole months
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in months
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Months monthsBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalDate && end instanceof LocalDate)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int months = chrono.months().getDifference(
                ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis());
        return Months.months(months);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Months.months(amount);
}
 
Example #22
Source File: Months.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Months</code> representing the number of whole months
 * in the specified interval. This method corectly handles any daylight
 * savings time changes that may occur during the interval.
 *
 * @param interval  the interval to extract months from, null returns zero
 * @return the period in months
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Months monthsIn(ReadableInterval interval) {
    if (interval == null)   {
        return Months.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.months());
    return Months.months(amount);
}
 
Example #23
Source File: Hours.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Hours</code> representing the number of whole hours
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalTime</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in hours
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Hours hoursBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalTime && end instanceof LocalTime)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int hours = chrono.hours().getDifference(
                ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis());
        return Hours.hours(hours);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Hours.hours(amount);
}
 
Example #24
Source File: Hours.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Hours</code> representing the number of whole hours
 * in the specified interval.
 *
 * @param interval  the interval to extract hours from, null returns zero
 * @return the period in hours
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Hours hoursIn(ReadableInterval interval) {
    if (interval == null)   {
        return Hours.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.hours());
    return Hours.hours(amount);
}
 
Example #25
Source File: Years.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Years</code> representing the number of whole years
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalDate</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in years
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Years yearsBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalDate && end instanceof LocalDate)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int years = chrono.years().getDifference(
                ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis());
        return Years.years(years);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Years.years(amount);
}
 
Example #26
Source File: Years.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Years</code> representing the number of whole years
 * in the specified interval. This method corectly handles any daylight
 * savings time changes that may occur during the interval.
 *
 * @param interval  the interval to extract years from, null returns zero
 * @return the period in years
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Years yearsIn(ReadableInterval interval) {
    if (interval == null)   {
        return Years.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.years());
    return Years.years(amount);
}
 
Example #27
Source File: Hours.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Hours</code> representing the number of whole hours
 * in the specified interval.
 *
 * @param interval  the interval to extract hours from, null returns zero
 * @return the period in hours
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Hours hoursIn(ReadableInterval interval) {
    if (interval == null)   {
        return Hours.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.hours());
    return Hours.hours(amount);
}
 
Example #28
Source File: Hours.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Hours</code> representing the number of whole hours
 * between the two specified partial datetimes.
 * <p>
 * The two partials must contain the same fields, for example you can specify
 * two <code>LocalTime</code> objects.
 *
 * @param start  the start partial date, must not be null
 * @param end  the end partial date, must not be null
 * @return the period in hours
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Hours hoursBetween(ReadablePartial start, ReadablePartial end) {
    if (start instanceof LocalTime && end instanceof LocalTime)   {
        Chronology chrono = DateTimeUtils.getChronology(start.getChronology());
        int hours = chrono.hours().getDifference(
                ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis());
        return Hours.hours(hours);
    }
    int amount = BaseSingleFieldPeriod.between(start, end, ZERO);
    return Hours.hours(amount);
}
 
Example #29
Source File: Minutes.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Minutes</code> representing the number of whole minutes
 * in the specified interval.
 *
 * @param interval  the interval to extract minutes from, null returns zero
 * @return the period in minutes
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Minutes minutesIn(ReadableInterval interval) {
    if (interval == null)   {
        return Minutes.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.minutes());
    return Minutes.minutes(amount);
}
 
Example #30
Source File: Seconds.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a <code>Seconds</code> representing the number of whole seconds
 * in the specified interval.
 *
 * @param interval  the interval to extract seconds from, null returns zero
 * @return the period in seconds
 * @throws IllegalArgumentException if the partials are null or invalid
 */
public static Seconds secondsIn(ReadableInterval interval) {
    if (interval == null)   {
        return Seconds.ZERO;
    }
    int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.seconds());
    return Seconds.seconds(amount);
}