Search in sources :

Example 31 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project j2objc by google.

the class Year method with.

/**
 * Returns a copy of this year with the specified field set to a new value.
 * <p>
 * This returns a {@code Year}, based on this one, with the value
 * for the specified field changed.
 * If it is not possible to set the value, because the field is not supported or for
 * some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the adjustment is implemented here.
 * The supported fields behave as follows:
 * <ul>
 * <li>{@code YEAR_OF_ERA} -
 *  Returns a {@code Year} with the specified year-of-era
 *  The era will be unchanged.
 * <li>{@code YEAR} -
 *  Returns a {@code Year} with the specified year.
 *  This completely replaces the date and is equivalent to {@link #of(int)}.
 * <li>{@code ERA} -
 *  Returns a {@code Year} with the specified era.
 *  The year-of-era will be unchanged.
 * </ul>
 * <p>
 * In all cases, if the new value is outside the valid range of values for the field
 * then a {@code DateTimeException} will be thrown.
 * <p>
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
 * passing {@code this} as the argument. In this case, the field determines
 * whether and how to adjust the instant.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param field  the field to set in the result, not null
 * @param newValue  the new value of the field in the result
 * @return a {@code Year} based on {@code this} with the specified field set, not null
 * @throws DateTimeException if the field cannot be set
 * @throws UnsupportedTemporalTypeException if the field is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Year with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch(f) {
            case YEAR_OF_ERA:
                return Year.of((int) (year < 1 ? 1 - newValue : newValue));
            case YEAR:
                return Year.of((int) newValue);
            case ERA:
                return (getLong(ERA) == newValue ? this : Year.of(1 - year));
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 32 with UnsupportedTemporalTypeException

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

the class DateMapFunctions method timeWindow.

/**
 * Returns a column containing integers representing the nth group (0-based) that a date falls
 * into.
 *
 * <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
 * after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
 * to the second ("1") group.
 *
 * @param unit A ChronoUnit greater than or equal to a day
 * @param n The number of units in each group.
 * @param start The starting point of the first group; group boundaries are offsets from this
 *     point
 */
default IntColumn timeWindow(ChronoUnit unit, int n, LocalDate start) {
    String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
    int packedStartDate = PackedLocalDate.pack(start);
    IntColumn numberColumn = IntColumn.create(newColumnName, size());
    for (int i = 0; i < size(); i++) {
        int packedDate = getIntInternal(i);
        int result;
        switch(unit) {
            case DAYS:
                result = PackedLocalDate.daysUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case WEEKS:
                result = PackedLocalDate.weeksUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case MONTHS:
                result = PackedLocalDate.monthsUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case YEARS:
                result = PackedLocalDate.yearsUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            default:
                throw new UnsupportedTemporalTypeException("The ChronoUnit " + unit + " is not supported for timeWindows on dates");
        }
    }
    numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
    return numberColumn;
}
Also used : UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) IntColumn(tech.tablesaw.api.IntColumn)

Example 33 with UnsupportedTemporalTypeException

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

the class DateTimeMapFunctions method timeWindow.

/**
 * Returns a column containing integers representing the nth group (0-based) that a date falls
 * into.
 *
 * <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
 * after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
 * to the second ("1") group.
 *
 * @param unit A ChronoUnit greater than or equal to a minute
 * @param n The number of units in each group.
 * @param start The starting point of the first group; group boundaries are offsets from this
 *     point
 */
default LongColumn timeWindow(ChronoUnit unit, int n, LocalDateTime start) {
    String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
    long packedStartDate = pack(start);
    LongColumn numberColumn = LongColumn.create(newColumnName, size());
    for (int i = 0; i < size(); i++) {
        long packedDate = getLongInternal(i);
        long result;
        switch(unit) {
            case MINUTES:
                result = minutesUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case HOURS:
                result = hoursUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case DAYS:
                result = daysUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case WEEKS:
                result = weeksUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case MONTHS:
                result = monthsUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case YEARS:
                result = yearsUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            default:
                throw new UnsupportedTemporalTypeException("The ChronoUnit " + unit + " is not supported for timeWindows on dates");
        }
    }
    numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
    return numberColumn;
}
Also used : LongColumn(tech.tablesaw.api.LongColumn) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 34 with UnsupportedTemporalTypeException

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

the class TimeMapFunctions method plus.

default TimeColumn plus(int time, ChronoUnit unit) {
    TimeColumn newColumn = TimeColumn.create("");
    String timeUnitString = "";
    for (int r = 0; r < size(); r++) {
        int c1 = this.getIntInternal(r);
        if (TimeColumn.valueIsMissing(c1)) {
            newColumn.appendInternal(TimeColumnType.missingValueIndicator());
        } else {
            switch(unit) {
                case HOURS:
                    newColumn.appendInternal(PackedLocalTime.plusHours(time, c1));
                    timeUnitString = "hours";
                    break;
                case MINUTES:
                    newColumn.appendInternal(PackedLocalTime.plusMinutes(time, c1));
                    timeUnitString = "minutes";
                    break;
                case SECONDS:
                    newColumn.appendInternal(PackedLocalTime.plusSeconds(time, c1));
                    timeUnitString = "seconds";
                    break;
                case MILLIS:
                    newColumn.appendInternal(PackedLocalTime.plusMilliseconds(time, c1));
                    timeUnitString = "ms";
                    break;
                default:
                    throw new UnsupportedTemporalTypeException("Type " + unit + " is not currently supported");
            }
        }
    }
    newColumn.setName(name() + " + " + time + " " + timeUnitString + "(s)");
    return newColumn;
}
Also used : TimeColumn(tech.tablesaw.api.TimeColumn) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 35 with UnsupportedTemporalTypeException

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

the class TimeMapFunctions method timeWindow.

/**
 * Returns a column containing integers representing the nth group (0-based) that a date falls
 * into.
 *
 * <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
 * after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
 * to the second ("1") group.
 *
 * @param unit A ChronoUnit greater than or equal to a day
 * @param n The number of units in each group.
 * @param start The starting point of the first group; group boundaries are offsets from this
 *     point
 */
default DoubleColumn timeWindow(ChronoUnit unit, int n, LocalTime start) {
    String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
    int packedStartTime = PackedLocalTime.pack(start);
    DoubleColumn numberColumn = DoubleColumn.create(newColumnName, size());
    for (int i = 0; i < size(); i++) {
        int packedTime = getIntInternal(i);
        int result;
        switch(unit) {
            case HOURS:
                result = PackedLocalTime.hoursUntil(packedTime, packedStartTime) / n;
                numberColumn.append(result);
                break;
            case MINUTES:
                result = PackedLocalTime.minutesUntil(packedTime, packedStartTime) / n;
                numberColumn.append(result);
                break;
            case SECONDS:
                result = PackedLocalTime.secondsUntil(packedTime, packedStartTime) / n;
                numberColumn.append(result);
                break;
            default:
                throw new UnsupportedTemporalTypeException("The ChronoUnit " + unit + " is not supported for timeWindows on times");
        }
    }
    numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
    return numberColumn;
}
Also used : DoubleColumn(tech.tablesaw.api.DoubleColumn) 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