Search in sources :

Example 36 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project symja_android_library by axkr.

the class PackedLocalTime method truncatedTo.

public static int truncatedTo(TemporalUnit unit, int packedTime) {
    if (unit == ChronoUnit.NANOS || unit == ChronoUnit.MILLIS) {
        return packedTime;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    int hour = PackedLocalTime.getHour(packedTime);
    int minute = PackedLocalTime.getMinute(packedTime);
    int second = PackedLocalTime.getSecond(packedTime);
    int milli = 0;
    if (unit == ChronoUnit.DAYS) {
        hour = 0;
        minute = 0;
        second = 0;
    } else if (unit == ChronoUnit.HALF_DAYS) {
        if (hour >= 12) {
            hour = 12;
        } else {
            hour = 0;
        }
        minute = 0;
        second = 0;
    } else if (unit == ChronoUnit.HOURS) {
        minute = 0;
        second = 0;
    } else if (unit == ChronoUnit.MINUTES) {
        second = 0;
    }
    return PackedLocalTime.create(hour, minute, second, milli);
}
Also used : Duration(java.time.Duration) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 37 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project neo4j by neo4j.

the class TemporalValue method until.

@Override
public final long until(Temporal endExclusive, TemporalUnit unit) {
    if (!(endExclusive instanceof TemporalValue)) {
        throw new InvalidArgumentException("Can only compute durations between TemporalValues.");
    }
    TemporalValue from = this;
    TemporalValue to = (TemporalValue) endExclusive;
    if (unit.isTimeBased()) {
        from = attachTime(from);
        to = attachTime(to);
    } else if (from.isSupported(ChronoField.SECOND_OF_DAY) && !to.isSupported(ChronoField.SECOND_OF_DAY)) {
        to = attachTime(to);
    } else if (to.isSupported(ChronoField.SECOND_OF_DAY) && !from.isSupported(ChronoField.SECOND_OF_DAY)) {
        from = attachTime(from);
    }
    if (from.isSupported(ChronoField.MONTH_OF_YEAR) && !to.isSupported(ChronoField.MONTH_OF_YEAR)) {
        to = attachDate(to, from.getDatePart());
    } else if (to.isSupported(ChronoField.MONTH_OF_YEAR) && !from.isSupported(ChronoField.MONTH_OF_YEAR)) {
        from = attachDate(from, to.getDatePart());
    }
    if (from.supportsTimeZone() && !to.supportsTimeZone()) {
        to = attachTimeZone(to, from.getZoneId(from::getZoneOffset));
    } else if (to.supportsTimeZone() && !from.supportsTimeZone()) {
        from = attachTimeZone(from, to.getZoneId(to::getZoneOffset));
    }
    long until;
    try {
        until = from.temporal().until(to, unit);
    } catch (UnsupportedTemporalTypeException e) {
        throw new UnsupportedTemporalUnitException(e.getMessage(), e);
    }
    return until;
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) UnsupportedTemporalUnitException(org.neo4j.exceptions.UnsupportedTemporalUnitException) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Aggregations

UnsupportedTemporalTypeException (java.time.temporal.UnsupportedTemporalTypeException)37 ChronoField (java.time.temporal.ChronoField)22 Test (org.junit.Test)6 TemporalAccessor (java.time.temporal.TemporalAccessor)4 TimeColumn (tech.tablesaw.api.TimeColumn)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)2 TemporalField (java.time.temporal.TemporalField)2 Calendar (java.util.Calendar)2 LocalGregorianCalendar (sun.util.calendar.LocalGregorianCalendar)2 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 YearMonth (java.time.YearMonth)1 ZoneId (java.time.ZoneId)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeParseException (java.time.format.DateTimeParseException)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Date (java.util.Date)1 Map (java.util.Map)1