hibernate/hibernate-orm · error · HibernateException

could not parse time string %s

Error message

could not parse time string %s

What it means

fromEncodedString() rehydrates java.sql.Date values that Hibernate stored as encoded text (used on databases without a native DATE type, where the value lives in a VARCHAR column). It parses with a formatter built from ISO_DATE plus an optional 'T'+ISO_LOCAL_TIME tail. When the stored text is not ISO-8601 date, the DateTimeParseException is wrapped in HibernateException. Note the message text says "could not parse time string" although this is the date descriptor - a copy-paste artifact in the message, not an indication that a time was requested.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JdbcDateJavaType.java:271

	@Override
	public Date fromString(CharSequence string) {
		try {
			final var temporalAccessor = LITERAL_FORMATTER.parse( string );
			return java.sql.Date.valueOf( temporalAccessor.query( LocalDate::from ) );
		}
		catch ( DateTimeParseException pe) {
			throw new HibernateException( "could not parse date string " + string, pe );
		}
	}

	@Override
	public Date fromEncodedString(CharSequence charSequence, int start, int end) {
		try {
			final var temporalAccessor = ENCODED_FORMATTER.parse( subSequence( charSequence, start, end ) );
			return java.sql.Date.valueOf( temporalAccessor.query( LocalDate::from ) );
		}
		catch ( DateTimeParseException pe) {
			throw new HibernateException( "could not parse time string " + subSequence( charSequence, start, end ), pe );
		}
	}

	@Override
	public void appendEncodedString(SqlAppender sb, Date value) {
		LITERAL_FORMATTER.formatTo( fromDate( value ), sb );
	}

	@Override
	public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) {
		return context.getJdbcType( Types.DATE );
	}

	@Override
	protected TemporalJavaType<Date> forDatePrecision(TypeConfiguration typeConfiguration) {
		return this;
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Inspect the actual column content and normalize it to ISO date text (yyyy-MM-dd, optionally yyyy-MM-ddTHH:mm:ss)
  2. If the database has a native DATE type, alter the column so Hibernate never uses the encoded-string path
  3. Add an AttributeConverter that parses the legacy format into java.sql.Date for that entity field
  4. Reject or clean empty strings and whitespace-padded values in the data

Example fix

// before (varchar column contains '31-DEC-24')
// read of entity triggers HibernateException: could not parse time string 31-DEC-24

// after
UPDATE events SET day_text = TO_CHAR(TO_DATE(day_text,'DD-MON-RR'),'YYYY-MM-DD');
-- or map the column with an AttributeConverter parsing 'DD-MON-RR'
Defensive patterns

Strategy: validation

Validate before calling

static boolean parsesAsEncodedDate(CharSequence s) {
    try {
        java.time.format.DateTimeFormatter f = java.time.format.DateTimeFormatter.ISO_DATE;
        f.parse(s); return true;
    } catch (java.time.format.DateTimeParseException e) { return false; }
}

// guard reads of text columns backing Date attributes:
if (!parsesAsEncodedDate(row.get("day"))) logAndQuarantine(row);

Try / catch

try { entity.setDay(row.getDate("day")); }
catch (HibernateException e) {
    if (e.getCause() instanceof DateTimeParseException) { /* flag row for data repair, skip */ }
    else throw e;
}

Prevention

When it happens

Trigger: A DATE column materialized as text containing '2024/12/31', '31-DEC-24', an empty string, or text with a space separator; running on a dialect that encodes dates as strings after the data was written by a different tool/version in another format; reading rows restored from a dump that reformatted dates.

Common situations: Database migrations between dialects with different textual date encodings; legacy varchar columns holding dates now mapped as Date; data written by an older Hibernate version or an external ETL job.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/8e0c2a6651e43372. Report an issue: GitHub.