Search in sources :

Example 51 with Chronology

use of org.joda.time.Chronology in project joda-time by JodaOrg.

the class BasePeriod method toDurationFrom.

//-----------------------------------------------------------------------
/**
     * Gets the total millisecond duration of this period relative to a start instant.
     * <p>
     * This method adds the period to the specified instant in order to
     * calculate the duration.
     * <p>
     * An instant must be supplied as the duration of a period varies.
     * For example, a period of 1 month could vary between the equivalent of
     * 28 and 31 days in milliseconds due to different length months.
     * Similarly, a day can vary at Daylight Savings cutover, typically between
     * 23 and 25 hours.
     *
     * @param startInstant  the instant to add the period to, thus obtaining the duration
     * @return the total length of the period as a duration relative to the start instant
     * @throws ArithmeticException if the millis exceeds the capacity of the duration
     */
public Duration toDurationFrom(ReadableInstant startInstant) {
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    long endMillis = chrono.add(this, startMillis, 1);
    return new Duration(startMillis, endMillis);
}
Also used : ReadableDuration(org.joda.time.ReadableDuration) Duration(org.joda.time.Duration) Chronology(org.joda.time.Chronology) ISOChronology(org.joda.time.chrono.ISOChronology)

Example 52 with Chronology

use of org.joda.time.Chronology in project joda-time by JodaOrg.

the class BaseSingleFieldPeriod method between.

//-----------------------------------------------------------------------
/**
     * Calculates the number of whole units between the two specified datetimes.
     *
     * @param start  the start instant, validated to not be null
     * @param end  the end instant, validated to not be null
     * @param field  the field type to use, must not be null
     * @return the period
     * @throws IllegalArgumentException if the instants are null or invalid
     */
protected static int between(ReadableInstant start, ReadableInstant end, DurationFieldType field) {
    if (start == null || end == null) {
        throw new IllegalArgumentException("ReadableInstant objects must not be null");
    }
    Chronology chrono = DateTimeUtils.getInstantChronology(start);
    int amount = field.getField(chrono).getDifference(end.getMillis(), start.getMillis());
    return amount;
}
Also used : Chronology(org.joda.time.Chronology) ISOChronology(org.joda.time.chrono.ISOChronology)

Example 53 with Chronology

use of org.joda.time.Chronology in project joda-time by JodaOrg.

the class BaseSingleFieldPeriod method standardPeriodIn.

/**
     * Creates a new instance representing the number of complete standard length units
     * in the specified period.
     * <p>
     * This factory method converts all fields from the period to hours using standardised
     * durations for each field. Only those fields which have a precise duration in
     * the ISO UTC chronology can be converted.
     * <ul>
     * <li>One week consists of 7 days.
     * <li>One day consists of 24 hours.
     * <li>One hour consists of 60 minutes.
     * <li>One minute consists of 60 seconds.
     * <li>One second consists of 1000 milliseconds.
     * </ul>
     * Months and Years are imprecise and periods containing these values cannot be converted.
     *
     * @param period  the period to get the number of hours from, must not be null
     * @param millisPerUnit  the number of milliseconds in one standard unit of this period
     * @throws IllegalArgumentException if the period contains imprecise duration values
     */
protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
    if (period == null) {
        return 0;
    }
    Chronology iso = ISOChronology.getInstanceUTC();
    long duration = 0L;
    for (int i = 0; i < period.size(); i++) {
        int value = period.getValue(i);
        if (value != 0) {
            DurationField field = period.getFieldType(i).getField(iso);
            if (field.isPrecise() == false) {
                throw new IllegalArgumentException("Cannot convert period to duration as " + field.getName() + " is not precise in the period " + period);
            }
            duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value));
        }
    }
    return FieldUtils.safeToInt(duration / millisPerUnit);
}
Also used : Chronology(org.joda.time.Chronology) ISOChronology(org.joda.time.chrono.ISOChronology) DurationField(org.joda.time.DurationField)

Example 54 with Chronology

use of org.joda.time.Chronology in project joda-time by JodaOrg.

the class StringConverter method setInto.

//-----------------------------------------------------------------------
/**
     * Sets the value of the mutable interval from the string.
     * 
     * @param writableInterval  the interval to set
     * @param object  the String to convert, must not be null
     * @param chrono  the chronology to use, may be null
     */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;
    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }
    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
Also used : PeriodFormatter(org.joda.time.format.PeriodFormatter) Period(org.joda.time.Period) ReadWritablePeriod(org.joda.time.ReadWritablePeriod) Chronology(org.joda.time.Chronology) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 55 with Chronology

use of org.joda.time.Chronology in project joda-time by JodaOrg.

the class BasicChronology method getDateTimeMillis.

@Override
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) throws IllegalArgumentException {
    Chronology base;
    if ((base = getBase()) != null) {
        return base.getDateTimeMillis(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
    }
    FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), hourOfDay, 0, 23);
    FieldUtils.verifyValueBounds(DateTimeFieldType.minuteOfHour(), minuteOfHour, 0, 59);
    FieldUtils.verifyValueBounds(DateTimeFieldType.secondOfMinute(), secondOfMinute, 0, 59);
    FieldUtils.verifyValueBounds(DateTimeFieldType.millisOfSecond(), millisOfSecond, 0, 999);
    long millisOfDay = hourOfDay * DateTimeConstants.MILLIS_PER_HOUR + minuteOfHour * DateTimeConstants.MILLIS_PER_MINUTE + secondOfMinute * DateTimeConstants.MILLIS_PER_SECOND + millisOfSecond;
    return getDateTimeMillis0(year, monthOfYear, dayOfMonth, (int) millisOfDay);
}
Also used : Chronology(org.joda.time.Chronology)

Aggregations

Chronology (org.joda.time.Chronology)69 DateTime (org.joda.time.DateTime)32 ISOChronology (org.joda.time.chrono.ISOChronology)30 GJChronology (org.joda.time.chrono.GJChronology)18 BuddhistChronology (org.joda.time.chrono.BuddhistChronology)17 LocalDate (org.joda.time.LocalDate)14 DateTimeZone (org.joda.time.DateTimeZone)13 MutableDateTime (org.joda.time.MutableDateTime)7 LocalDateTime (org.joda.time.LocalDateTime)4 ReadableDateTime (org.joda.time.ReadableDateTime)4 JulianChronology (org.joda.time.chrono.JulianChronology)3 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)3 Serializable (java.io.Serializable)2 Date (java.util.Date)2 Duration (org.joda.time.Duration)2 Period (org.joda.time.Period)2 ReadableDuration (org.joda.time.ReadableDuration)2 ReadableInstant (org.joda.time.ReadableInstant)2 ReadablePartial (org.joda.time.ReadablePartial)2 Calendar (java.util.Calendar)1