Reputation: 173
I've an entity Date property:
@Temporal(TemporalType.TIMESTAMP)
private Date paymentDate;
that I've to convert to a String. Using the following code I receive the Cannot format given Object as a Date exception:
Format df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String pDate = df.format(item.getPaymentDate());
Where I wrong?
Upvotes: 0
Views: 1146
Reputation: 280030
The Format#format(Object)
method delegates to a sub type's format(Object, StringBuffer, FieldPosition)
method. The DateFormat
implementation which SimpleDateFormat
inherits is as follows
public final StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition)
{
if (obj instanceof Date)
return format( (Date)obj, toAppendTo, fieldPosition );
else if (obj instanceof Number)
return format( new Date(((Number)obj).longValue()), toAppendTo, fieldPosition );
else
throw new IllegalArgumentException("Cannot format given Object as a Date");
}
In other words, the value returned by item.getPaymentDate()
must either be null
or be of a type other than java.util.Date
or java.lang.Number
.
Upvotes: 1