/** * Get the {@link ZonedDateTime} with provided timezone. * The field must have {@link Schema.LogicalType#TIMESTAMP_MILLIS} or {@link Schema.LogicalType#TIMESTAMP_MICROS} * as its logical type. * * @param fieldName zoned date time field to get. * @param zoneId zone id for the field * @return value of the field as a {@link ZonedDateTime} * @throws UnexpectedFormatException if the provided field is not of {@link LogicalType#TIMESTAMP_MILLIS} or * {@link LogicalType#TIMESTAMP_MICROS} type. */ @Nullable public ZonedDateTime getTimestamp(String fieldName, ZoneId zoneId) { LogicalType logicalType = validateAndGetLogicalType(schema.getField(fieldName), EnumSet.of(LogicalType.TIMESTAMP_MILLIS, LogicalType.TIMESTAMP_MICROS)); Object value = fields.get(fieldName); if (value == null || logicalType == null) { return null; } if (logicalType == LogicalType.TIMESTAMP_MILLIS) { return getZonedDateTime((long) value, TimeUnit.MILLISECONDS, zoneId); } return getZonedDateTime((long) value, TimeUnit.MICROSECONDS, zoneId); }
/** * Get the {@link ZonedDateTime} with provided timezone. * The field must have {@link Schema.LogicalType#TIMESTAMP_MILLIS} or {@link Schema.LogicalType#TIMESTAMP_MICROS} * as its logical type. * * @param fieldName zoned date time field to get. * @param zoneId zone id for the field * @return value of the field as a {@link ZonedDateTime} * @throws UnexpectedFormatException if the provided field is not of {@link LogicalType#TIMESTAMP_MILLIS} or * {@link LogicalType#TIMESTAMP_MICROS} type. */ @Nullable public ZonedDateTime getTimestamp(String fieldName, ZoneId zoneId) { LogicalType logicalType = validateAndGetLogicalType(schema.getField(fieldName), EnumSet.of(LogicalType.TIMESTAMP_MILLIS, LogicalType.TIMESTAMP_MICROS)); Object value = fields.get(fieldName); if (value == null || logicalType == null) { return null; } if (logicalType == LogicalType.TIMESTAMP_MILLIS) { return getZonedDateTime((long) value, TimeUnit.MILLISECONDS, zoneId); } return getZonedDateTime((long) value, TimeUnit.MICROSECONDS, zoneId); }