org.joda.time.IllegalInstantException Java Examples

The following examples show how to use org.joda.time.IllegalInstantException. 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: DateUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Parses the given string into a Date object. In case the date parsed falls in a
 * daylight savings transition, the date is parsed via a local date and converted to the
 * first valid time after the DST gap. When the fallback is used, any timezone offset in the given
 * format would be ignored.
 *
 * @param dateString The string to parse
 * @param formatter The formatter to use for parsing
 * @return Parsed Date object. Null if the supplied dateString is empty.
 */
private static Date safeParseDateTime( final String dateString, final DateTimeFormatter formatter )
{
    if ( StringUtils.isEmpty( dateString ) )
    {
        return null;
    }

    try
    {
        return formatter.parseDateTime( dateString ).toDate();
    }
    catch( IllegalInstantException e )
    {
        return formatter.parseLocalDateTime( dateString ).toDate();
    }
}
 
Example #2
Source File: DateTimeUnit.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Converts dateUnit to Joda-Time DateTime with a specific chronology.
 *
 * @param chronology Chronology to use
 * @return Populated DateTime object
 */
public DateTime toJodaDateTime( Chronology chronology )
{
    try
    {
        return new DateTime( year, month, day, hour, minute, second, millis, chronology.withZone( DateTimeZone.forTimeZone( timeZone ) ) );
    }
    catch ( IllegalInstantException ex )
    {
        LocalDateTime localDateTime = new LocalDateTime( year, month, day, hour, minute, second, millis,
            chronology.withZone( DateTimeZone.forTimeZone( timeZone ) ) );

        return localDateTime.toLocalDate().toDateTimeAtStartOfDay();
    }
}
 
Example #3
Source File: ZonedChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param localInstant  the instant from 1970-01-01T00:00:00 local time
 * @return the instant from 1970-01-01T00:00:00Z
 */
private long localToUTC(long localInstant) {
    DateTimeZone zone = getZone();
    int offset = zone.getOffsetFromLocal(localInstant);
    localInstant -= offset;
    if (offset != zone.getOffset(localInstant)) {
        throw new IllegalInstantException(localInstant, zone.getID());
    }
    return localInstant;
}
 
Example #4
Source File: ZonedChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public long set(long instant, int value) {
    long localInstant = iZone.convertUTCToLocal(instant);
    localInstant = iField.set(localInstant, value);
    long result = iZone.convertLocalToUTC(localInstant, false, instant);
    if (get(result) != value) {
        IllegalInstantException cause = new IllegalInstantException(localInstant,  iZone.getID());
        IllegalFieldValueException ex = new IllegalFieldValueException(iField.getType(), Integer.valueOf(value), cause.getMessage());
        ex.initCause(cause);
        throw ex;
    }
    return result;
}
 
Example #5
Source File: ZonedChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param localInstant  the instant from 1970-01-01T00:00:00 local time
 * @return the instant from 1970-01-01T00:00:00Z
 */
private long localToUTC(long localInstant) {
    DateTimeZone zone = getZone();
    int offset = zone.getOffsetFromLocal(localInstant);
    localInstant -= offset;
    if (offset != zone.getOffset(localInstant)) {
        throw new IllegalInstantException(localInstant, zone.getID());
    }
    return localInstant;
}
 
Example #6
Source File: ZonedChronology.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public long set(long instant, int value) {
    long localInstant = iZone.convertUTCToLocal(instant);
    localInstant = iField.set(localInstant, value);
    long result = iZone.convertLocalToUTC(localInstant, false, instant);
    if (get(result) != value) {
        IllegalInstantException cause = new IllegalInstantException(localInstant,  iZone.getID());
        IllegalFieldValueException ex = new IllegalFieldValueException(iField.getType(), Integer.valueOf(value), cause.getMessage());
        ex.initCause(cause);
        throw ex;
    }
    return result;
}
 
Example #7
Source File: Nopol2017_0091_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Computes the parsed datetime by setting the saved fields.
 * This method is idempotent, but it is not thread-safe.
 *
 * @param resetFields false by default, but when true, unsaved field values are cleared
 * @param text optional text being parsed, to be included in any error message
 * @return milliseconds since 1970-01-01T00:00:00Z
 * @throws IllegalArgumentException if any field is out of range
 * @since 1.3
 */
public long computeMillis(boolean resetFields, String text) {
    SavedField[] savedFields = iSavedFields;
    int count = iSavedFieldsCount;
    if (iSavedFieldsShared) {
        iSavedFields = savedFields = (SavedField[])iSavedFields.clone();
        iSavedFieldsShared = false;
    }
    sort(savedFields, count);
    if (count > 0) {
        // alter base year for parsing if first field is month or day
        DurationField months = DurationFieldType.months().getField(iChrono);
        DurationField days = DurationFieldType.days().getField(iChrono);
        DurationField first = savedFields[0].iField.getDurationField();
        if (compareReverse(first, months) >= 0 && compareReverse(first, days) <= 0) {
            saveField(DateTimeFieldType.year(), iDefaultYear);
            if (resetFields) {
                return computeMillis(resetFields, text);
            }
        }
    }

    long millis = iMillis;
    try {
        for (int i = 0; i < count; i++) {
            millis = savedFields[i].set(millis, resetFields);
        }
        if (resetFields) {
            for (int i = 0; i < count; i++) {
                millis = savedFields[i].set(millis, i == (count - 1));
            }
        }
    } catch (IllegalFieldValueException e) {
        if (text != null) {
            e.prependMessage("Cannot parse \"" + text + '"');
        }
        throw e;
    }
    
    if (iOffset != null) {
        millis -= iOffset;
    } else if (iZone != null) {
        int offset = iZone.getOffsetFromLocal(millis);
        millis -= offset;
        if (offset != iZone.getOffset(millis)) {
            String message = "Illegal instant due to time zone offset transition (" + iZone + ')';
            if (text != null) {
                message = "Cannot parse \"" + text + "\": " + message;
            }
            throw new IllegalInstantException(message);
        }
    }
    
    return millis;
}
 
Example #8
Source File: Nopol2017_0091_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Computes the parsed datetime by setting the saved fields.
 * This method is idempotent, but it is not thread-safe.
 *
 * @param resetFields false by default, but when true, unsaved field values are cleared
 * @param text optional text being parsed, to be included in any error message
 * @return milliseconds since 1970-01-01T00:00:00Z
 * @throws IllegalArgumentException if any field is out of range
 * @since 1.3
 */
public long computeMillis(boolean resetFields, String text) {
    SavedField[] savedFields = iSavedFields;
    int count = iSavedFieldsCount;
    if (iSavedFieldsShared) {
        iSavedFields = savedFields = (SavedField[])iSavedFields.clone();
        iSavedFieldsShared = false;
    }
    sort(savedFields, count);
    if (count > 0) {
        // alter base year for parsing if first field is month or day
        DurationField months = DurationFieldType.months().getField(iChrono);
        DurationField days = DurationFieldType.days().getField(iChrono);
        DurationField first = savedFields[0].iField.getDurationField();
        if (compareReverse(first, months) >= 0 && compareReverse(first, days) <= 0) {
            saveField(DateTimeFieldType.year(), iDefaultYear);
            return computeMillis(resetFields, text);
        }
    }

    long millis = iMillis;
    try {
        for (int i = 0; i < count; i++) {
            millis = savedFields[i].set(millis, resetFields);
        }
        if (resetFields) {
            for (int i = 0; i < count; i++) {
                millis = savedFields[i].set(millis, i == (count - 1));
            }
        }
    } catch (IllegalFieldValueException e) {
        if (text != null) {
            e.prependMessage("Cannot parse \"" + text + '"');
        }
        throw e;
    }
    
    if (iOffset != null) {
        millis -= iOffset;
    } else if (iZone != null) {
        int offset = iZone.getOffsetFromLocal(millis);
        millis -= offset;
        if (offset != iZone.getOffset(millis)) {
            String message = "Illegal instant due to time zone offset transition (" + iZone + ')';
            if (text != null) {
                message = "Cannot parse \"" + text + "\": " + message;
            }
            throw new IllegalInstantException(message);
        }
    }
    
    return millis;
}
 
Example #9
Source File: DateTimeParserBucket.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Computes the parsed datetime by setting the saved fields.
 * This method is idempotent, but it is not thread-safe.
 *
 * @param resetFields false by default, but when true, unsaved field values are cleared
 * @param text optional text being parsed, to be included in any error message
 * @return milliseconds since 1970-01-01T00:00:00Z
 * @throws IllegalArgumentException if any field is out of range
 * @since 1.3
 */
public long computeMillis(boolean resetFields, String text) {
    SavedField[] savedFields = iSavedFields;
    int count = iSavedFieldsCount;
    if (iSavedFieldsShared) {
        iSavedFields = savedFields = (SavedField[])iSavedFields.clone();
        iSavedFieldsShared = false;
    }
    sort(savedFields, count);
    if (count > 0) {
        // alter base year for parsing if first field is month or day
        DurationField months = DurationFieldType.months().getField(iChrono);
        DurationField days = DurationFieldType.days().getField(iChrono);
        DurationField first = savedFields[0].iField.getDurationField();
        if (compareReverse(first, months) >= 0 && compareReverse(first, days) <= 0) {
            saveField(DateTimeFieldType.year(), iDefaultYear);
            return computeMillis(resetFields, text);
        }
    }

    long millis = iMillis;
    try {
        for (int i = 0; i < count; i++) {
            millis = savedFields[i].set(millis, resetFields);
        }
        if (resetFields) {
            for (int i = 0; i < count; i++) {
                millis = savedFields[i].set(millis, i == (count - 1));
            }
        }
    } catch (IllegalFieldValueException e) {
        if (text != null) {
            e.prependMessage("Cannot parse \"" + text + '"');
        }
        throw e;
    }
    
    if (iOffset != null) {
        millis -= iOffset;
    } else if (iZone != null) {
        int offset = iZone.getOffsetFromLocal(millis);
        millis -= offset;
        if (offset != iZone.getOffset(millis)) {
            String message = "Illegal instant due to time zone offset transition (" + iZone + ')';
            if (text != null) {
                message = "Cannot parse \"" + text + "\": " + message;
            }
            throw new IllegalInstantException(message);
        }
    }
    
    return millis;
}
 
Example #10
Source File: DateTimeParserBucket.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Computes the parsed datetime by setting the saved fields.
 * This method is idempotent, but it is not thread-safe.
 *
 * @param resetFields false by default, but when true, unsaved field values are cleared
 * @param text optional text being parsed, to be included in any error message
 * @return milliseconds since 1970-01-01T00:00:00Z
 * @throws IllegalArgumentException if any field is out of range
 * @since 1.3
 */
public long computeMillis(boolean resetFields, String text) {
    SavedField[] savedFields = iSavedFields;
    int count = iSavedFieldsCount;
    if (iSavedFieldsShared) {
        iSavedFields = savedFields = (SavedField[])iSavedFields.clone();
        iSavedFieldsShared = false;
    }
    sort(savedFields, count);
    if (count > 0) {
        // alter base year for parsing if first field is month or day
        DurationField months = DurationFieldType.months().getField(iChrono);
        DurationField days = DurationFieldType.days().getField(iChrono);
        DurationField first = savedFields[0].iField.getDurationField();
        if (compareReverse(first, months) >= 0 && compareReverse(first, days) <= 0) {
            saveField(DateTimeFieldType.year(), iDefaultYear);
            return computeMillis(resetFields, text);
        }
    }

    long millis = iMillis;
    try {
        for (int i = 0; i < count; i++) {
            millis = savedFields[i].set(millis, resetFields);
        }
        if (resetFields) {
            for (int i = 0; i < count; i++) {
                millis = savedFields[i].set(millis, i == (count - 1));
            }
        }
    } catch (IllegalFieldValueException e) {
        if (text != null) {
            e.prependMessage("Cannot parse \"" + text + '"');
        }
        throw e;
    }
    
    if (iOffset != null) {
        millis -= iOffset;
    } else if (iZone != null) {
        int offset = iZone.getOffsetFromLocal(millis);
        millis -= offset;
        if (offset != iZone.getOffset(millis)) {
            String message = "Illegal instant due to time zone offset transition (" + iZone + ')';
            if (text != null) {
                message = "Cannot parse \"" + text + "\": " + message;
            }
            throw new IllegalInstantException(message);
        }
    }
    
    return millis;
}