Search code examples
jpajpa-2.0openjpa

openJPA date format


I created entity from the tables . Here is my employee entity.

Class Emp{ String id, java.sql.date joinDate }

As i see to_date() is not applicable in JPA, How can i persist the joined date in the same format 2010-12-25T12:00:00 into the database by using jpa.

How can i specify this format yyyy-MM-dd'T'HH:mm:ss.

Later on i want to search the emp who has joined in the format (yyyy-mm-dd) 2010-12-25, so that i can get all the values from the database 2010-12-25T12:00:00 2010-12-25T11:00:00 2010-12-25T10:05:00

@NamedQuery(name="getEmployeeByDate",query="select p from Emp p where p.joinDate =? ")


Solution

  • We can't save the date value "2010-12-25T12:00:00" directly in the database.So we have to convert that to the date time using SimpleDateFormat's parse method.with the help of parse method you can get the date time value.Then you can store that value into the column which has the type as TimeStamp.

    For Retrieval the value,get the values as the dateTime,then format the value using the simpledatformat class's format method in java.text package.