Example usage for java.time.temporal TemporalAccessor isSupported

List of usage examples for java.time.temporal TemporalAccessor isSupported

Introduction

In this page you can find the example usage for java.time.temporal TemporalAccessor isSupported.

Prototype

boolean isSupported(TemporalField field);

Source Link

Document

Checks if the specified field is supported.

Usage

From source file:Main.java

public static void main(String[] args) {
    Clock defaultClock = Clock.systemDefaultZone();

    Instant instant = Instant.now(defaultClock);

    TemporalQuery query = (TemporalAccessor x) -> x.isSupported(ChronoField.MILLI_OF_DAY);

    System.out.println(instant.query(query));
}

From source file:Main.java

public static Boolean queryFrom(TemporalAccessor temporal) {
    if (temporal.isSupported(ChronoField.DAY_OF_MONTH) && temporal.isSupported(ChronoField.DAY_OF_WEEK)) {
        int dayOfMonth = temporal.get(ChronoField.DAY_OF_MONTH);
        int weekDay = temporal.get(ChronoField.DAY_OF_WEEK);
        DayOfWeek dayOfWeek = DayOfWeek.of(weekDay);
        if (dayOfMonth == 1 && dayOfWeek == DayOfWeek.MONDAY) {
            return Boolean.TRUE;
        }//from www .  j  a va2 s.c  om
    }
    return Boolean.FALSE;
}

From source file:msi.gama.util.GamaDate.java

private static Temporal parse(final IScope scope, final String original, final DateTimeFormatter df) {
    if (original == null || original.isEmpty() || original.equals("now")) {
        return LocalDateTime.now(GamaDateType.DEFAULT_ZONE);
    }// w w  w .j av  a  2  s .  co  m
    Temporal result = null;

    if (df != null) {
        try {
            final TemporalAccessor ta = df.parse(original);
            if (ta instanceof Temporal) {
                return (Temporal) ta;
            }
            if (!ta.isSupported(ChronoField.YEAR) && !ta.isSupported(ChronoField.MONTH_OF_YEAR)
                    && !ta.isSupported(ChronoField.DAY_OF_MONTH)) {
                if (ta.isSupported(ChronoField.HOUR_OF_DAY)) {
                    return LocalTime.from(ta);
                }
            }
            if (!ta.isSupported(ChronoField.HOUR_OF_DAY) && !ta.isSupported(ChronoField.MINUTE_OF_HOUR)
                    && !ta.isSupported(ChronoField.SECOND_OF_MINUTE)) {
                return LocalDate.from(ta);
            }
            return LocalDateTime.from(ta);
        } catch (final DateTimeParseException e) {
            e.printStackTrace();
        }
        GAMA.reportAndThrowIfNeeded(scope,
                GamaRuntimeException.warning(
                        "The date " + original + " can not correctly be parsed by the pattern provided", scope),
                false);
        return parse(scope, original, null);
    }

    String dateStr;
    try {
        // We first make sure all date fields have the correct length and
        // the string is correctly formatted
        String string = original;
        if (!original.contains("T") && original.contains(" ")) {
            string = StringUtils.replaceOnce(original, " ", "T");
        }
        final String[] base = string.split("T");
        final String[] date = base[0].split("-");
        String other;
        if (base.length == 1) {
            other = "00:00:00";
        } else {
            other = base[1];
        }
        String year, month, day;
        if (date.length == 1) {
            // ISO basic date format
            year = date[0].substring(0, 4);
            month = date[0].substring(4, 6);
            day = date[0].substring(6, 8);
        } else {
            year = date[0];
            month = date[1];
            day = date[2];
        }
        if (year.length() == 2) {
            year = "20" + year;
        }
        if (month.length() == 1) {
            month = '0' + month;
        }
        if (day.length() == 1) {
            day = '0' + day;
        }
        dateStr = year + "-" + month + "-" + day + "T" + other;
    } catch (final Exception e1) {
        throw GamaRuntimeException.error("The date " + original
                + " is not correctly formatted. Please refer to the ISO date/time format", scope);
    }

    try {
        result = LocalDateTime.parse(dateStr);
    } catch (final DateTimeParseException e) {
        try {
            result = OffsetDateTime.parse(dateStr);
        } catch (final DateTimeParseException e2) {
            try {
                result = ZonedDateTime.parse(dateStr);
            } catch (final DateTimeParseException e3) {
                throw GamaRuntimeException.error(
                        "The date " + original
                                + " is not correctly formatted. Please refer to the ISO date/time format",
                        scope);
            }
        }
    }

    return result;
}

From source file:Main.java

@Override
public Boolean queryFrom(TemporalAccessor temporal) {
    if (temporal.isSupported(ChronoField.DAY_OF_MONTH) && temporal.isSupported(ChronoField.DAY_OF_WEEK)) {
        int dayOfMonth = temporal.get(ChronoField.DAY_OF_MONTH);
        int weekDay = temporal.get(ChronoField.DAY_OF_WEEK);
        DayOfWeek dayOfWeek = DayOfWeek.of(weekDay);
        if (dayOfMonth == 1 && dayOfWeek == DayOfWeek.MONDAY) {
            return Boolean.TRUE;
        }/*from w w  w .  j a va2  s. c o m*/
    }
    return Boolean.FALSE;
}

From source file:org.talend.dataprep.transformation.actions.date.ExtractDateTokens.java

@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
    final RowMetadata rowMetadata = context.getRowMetadata();
    final String columnId = context.getColumnId();
    final Map<String, String> parameters = context.getParameters();
    final ColumnMetadata column = rowMetadata.getById(columnId);

    // Create new columns for date tokens
    final Map<String, String> dateFieldColumns = new HashMap<>();
    for (DateFieldMappingBean date_field : DATE_FIELDS) {
        if (Boolean.valueOf(parameters.get(date_field.key))) {
            final String newColumn = context.column(column.getName() + SEPARATOR + date_field.key);
            dateFieldColumns.put(date_field.key, newColumn);
        }//from   w  ww  . j ava 2  s.c o m
    }

    // Get the most used pattern formatter and parse the date
    final String value = row.get(columnId);
    if (value == null) {
        return;
    }
    TemporalAccessor temporalAccessor = null;
    try {
        temporalAccessor = Providers.get().parse(value, context.getRowMetadata().getById(columnId));
    } catch (DateTimeException e) {
        // temporalAccessor is left null, this will be used bellow to set empty new value for all fields
        LOGGER.debug("Unable to parse date {}.", value, e);
    }

    // insert new extracted values
    for (final DateFieldMappingBean date_field : DATE_FIELDS) {
        if (Boolean.valueOf(parameters.get(date_field.key))) {
            String newValue = StringUtils.EMPTY;
            if (temporalAccessor != null && // may occurs if date can not be parsed with pattern
                    temporalAccessor.isSupported(date_field.field)) {
                newValue = String.valueOf(temporalAccessor.get(date_field.field));
            }
            row.set(dateFieldColumns.get(date_field.key), newValue);
        }
    }
}