I have a query of inner join tables:
List<Object[]> resultList = entityManager.createNativeQuery(SOME QUERY).getResultList();
When i try to access the elements of Object[]
one of which is a Date
I tried doing this:
for(Object[] obj : resultList) {
Date createdDate = obj[7] !=null ? new Date(Long.valueOf(obj[7] + "")) : null;
}
This gave obvious exception:
nested exception is java.lang.NumberFormatException: For input string: "2012-11-20"
I changed the Date
to DateTime
get TO_CHAR(createdDate,'DD.MM.YYYY:HH24:MI:SS')
And tried the below code:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date createdDate = null;
try {
createdDate = df.parse(obj[7] + "");
} catch (ParseException e) {
e.printStackTrace();
}
This approach results: createdDate = null
Any Suggestions ?
You shouldn't have to parse dates coming from a SQL query. They should come back as Date or Timestamp instances. Show us your query.
You prabably have java.sql.Date
instances coming back, but you're transforming them to Strings by concatenating them with an empty string:
obj[7] + ""
Just do
Date createdDate = (Date) obj[7];