apache/shardingsphere · error · InvalidDatetimeFormatException

Invalid %s, datetime pattern should be '%s', value is '%s'.

Error message

Invalid %s, datetime pattern should be '%s', value is '%s'.

What it means

Thrown by AutoIntervalShardingAlgorithm when the datetime-lower property cannot be parsed with the fixed 'yyyy-MM-dd HH:mm:ss' formatter (DateTimeParseException is caught and converted). Auto-interval sharding needs a valid lower datetime anchor to compute time buckets, so initialization aborts.

Source

Thrown at features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/AutoIntervalShardingAlgorithm.java:75

    private long shardingSeconds;
    
    @Getter
    private int autoTablesAmount;
    
    @Override
    public void init(final Properties props) {
        dateTimeLower = getDateTime(props);
        shardingSeconds = getShardingSeconds(props);
        autoTablesAmount = (int) (Math.ceil((double) (parseDate(props.getProperty(DATE_TIME_UPPER_KEY)) / shardingSeconds)) + 2D);
    }
    
    private LocalDateTime getDateTime(final Properties props) {
        String value = props.getProperty(DATE_TIME_LOWER_KEY);
        ShardingSpherePreconditions.checkNotNull(value, () -> new AlgorithmInitializationException(this, String.format("%s cannot be null.", DATE_TIME_LOWER_KEY)));
        try {
            return LocalDateTime.parse(value, DateTimeFormatterFactory.getDatetimeFormatter());
        } catch (final DateTimeParseException ignored) {
            throw new InvalidDatetimeFormatException(DATE_TIME_LOWER_KEY, value, "yyyy-MM-dd HH:mm:ss");
        }
    }
    
    private long getShardingSeconds(final Properties props) {
        ShardingSpherePreconditions.checkContainsKey(props, SHARDING_SECONDS_KEY, () -> new AlgorithmInitializationException(this, String.format("%s cannot be null.", SHARDING_SECONDS_KEY)));
        return Long.parseLong(props.getProperty(SHARDING_SECONDS_KEY));
    }
    
    @Override
    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
        ShardingSpherePreconditions.checkNotNull(shardingValue.getValue(), NullShardingValueException::new);
        String tableNameSuffix = String.valueOf(doSharding(parseDate(shardingValue.getValue())));
        return ShardingAutoTableAlgorithmUtils.findMatchedTargetName(availableTargetNames, tableNameSuffix, shardingValue.getDataNodeInfo()).orElse(null);
    }
    
    @Override
    public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size(), 1F);

View on GitHub (pinned to e952770a21)

Solutions

  1. Write the value exactly as 'yyyy-MM-dd HH:mm:ss', e.g. datetime-lower: '2020-01-01 00:00:00'
  2. Remove quotes only if YAML still parses the space-containing scalar correctly (quoting is the safe choice)
  3. Also check datetime-upper with the same format since it is parsed by the same parser
  4. If you need a custom pattern, switch to INTERVAL sharding type which supports datetime-pattern

Example fix

# before
- !SHARDING_ALGORITHM
  type: AUTO_INTERVAL
  props:
    datetime-lower: 2020-01-01
# after
- !SHARDING_ALGORITHM
  type: AUTO_INTERVAL
  props:
    datetime-lower: '2020-01-01 00:00:00'
Defensive patterns

Strategy: validation

Validate before calling

try {
    LocalDateTime.parse(props.getProperty("datetime-lower"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} catch (DateTimeParseException ex) {
    throw new IllegalArgumentException("datetime-lower must be 'yyyy-MM-dd HH:mm:ss'");
}

Prevention

When it happens

Trigger: Setting sharding-algorithm props datetime-lower to a date-only value ('2020-01-01'), an ISO instant ('2020-01-01T00:00:00'), or any string not matching 'yyyy-MM-dd HH:mm:ss'.

Common situations: Copying values from IntervalShardingAlgorithm examples that allow custom patterns; omitting the time part; using a T separator; trailing spaces or quotes in YAML.

Related errors


AI-assisted analysis of apache/shardingsphere@e952770a21 (2026-08-14). Data as JSON: /api/errors/4ab5c7b219ecf966. Report an issue: GitHub.