greenrobot/greenDAO · error · DaoException
Illegal date value: expected java.util.Date or Long for…
Error message
Illegal date value: expected java.util.Date or Long for value
What it means
For properties typed java.util.Date, checkValueForType() accepts only a Date instance or a Long (epoch millis) and converts both to a long for storage. Any other value type (String, Integer, etc.) triggers this DaoException. The conversion exists because greenDAO stores dates as numeric timestamps.
Solutions
- Parse the string to a Date first (e.g. new SimpleDateFormat("yyyy-MM-dd").parse(value)) and pass the Date.
- Pass epoch milliseconds as a Long instead.
- Check the property type in the generated Properties class; only Date-typed properties enforce this rule.
Example fix
// before
queryBuilder.where(UserDao.Properties.CreatedAt.eq("2024-01-01")); // DaoException
// after
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2024-01-01");
queryBuilder.where(UserDao.Properties.CreatedAt.eq(d)); Defensive patterns
Strategy: type-guard
Validate before calling
Object v = /* candidate */;
if (!(v instanceof java.util.Date || v instanceof Long)) {
throw new IllegalArgumentException("Date property needs Date or Long");
} Type guard
boolean isDateValue(Object v) { return v instanceof java.util.Date || v instanceof Long; } Try / catch
try { cond = Properties.CreatedAt.eq(v); } catch (DaoException e) { cond = Properties.CreatedAt.eq(parseDate(v)); } Prevention
- Normalize all date inputs to java.util.Date (or epoch Long) at your data boundary.
- Parse date strings from JSON/APIs before building queries.
- Check the generated Properties type before binding values.
When it happens
Trigger: where(Properties.CreatedAt.eq("2024-01-01")) or passing an Integer/other type to a condition on a Date-typed property.
Common situations: Passing dates as formatted strings from JSON/API payloads instead of parsing them to java.util.Date first.
Related errors
- Illegal value: found array, but simple object required
- Illegal boolean value: numbers must be 0 or 1, but was
- This operation only works with cached lazy lists
- Could not move to cursor location
- Loading of entity failed (null) at position
AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08).
Data as JSON: /api/errors/4919399e8875b9ae.
Report an issue: GitHub.
Appendix: source
Thrown at DaoCore/src/main/java/org/greenrobot/greendao/query/WhereCondition.java:84
}
}
}
}
class PropertyCondition extends AbstractCondition {
private static Object checkValueForType(Property property, Object value) {
if (value != null && value.getClass().isArray()) {
throw new DaoException("Illegal value: found array, but simple object required");
}
Class<?> type = property.type;
if (type == Date.class) {
if (value instanceof Date) {
return ((Date) value).getTime();
} else if (value instanceof Long) {
return value;
} else {
throw new DaoException("Illegal date value: expected java.util.Date or Long for value " + value);
}
} else if (property.type == boolean.class || property.type == Boolean.class) {
if (value instanceof Boolean) {
return ((Boolean) value) ? 1 : 0;
} else if (value instanceof Number) {
int intValue = ((Number) value).intValue();
if (intValue != 0 && intValue != 1) {
throw new DaoException("Illegal boolean value: numbers must be 0 or 1, but was " + value);
}
} else if (value instanceof String) {
String stringValue = ((String) value);
if ("TRUE".equalsIgnoreCase(stringValue)) {
return 1;
} else if ("FALSE".equalsIgnoreCase(stringValue)) {
return 0;
} else {
throw new DaoException(
"Illegal boolean value: Strings must be \"TRUE\" or \"FALSE\" (case insensitive), but was "View on GitHub (pinned to 0bbb338e17)