2

in my MySql database I store date as DATE type so I store only YYYY-MM-DD without any information about time (in the other table I use DATETIME to store also time information). My entity is :

@Entity
@Table(name = "clientlicense", catalog = "ats")
public class ClientLicense implements java.io.Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer idClientLicense;
    private Date startDate;
    private Date endDate;
    private int counter;
    private String macAddress;
    private String cpuId;

But when i get startDate and endDate from my WebUi I receive even the time like 2016-01-29 00:00:00.0 How can I store only the date as into database? Do I have to work in my HTML code?Thanks

2 Answers 2

2

Try with @Temporal JPA annotations.

Anotating your field and changing the type should help:

@Temporal(TemporalType.TIMESTAMP)  
private java.util.Date startDate;

The valid values are:

TemporalType.DATE
TemporalType.TIME
TemporalType.TIMESTAMP

It is equivalent of

DATE – equivalent of java.sql.Date
TIME – equivalent of java.sql.Time
TIMESTAMP – equivalent of java.sql.Timestamp
Sign up to request clarification or add additional context in comments.

Comments

0

Your entity mapping seems ok to me. The java.util.Date class ships also time information.

You will need to format the date object on the web tier. The implementation depends on the technology you are using on the front-end.

For example if you are on a plain JSP page you could format with a java.text.SimpleDateFormat object.

2 Comments

the only problem is that this date field is into an object with different fields, I'm using thymeleaf with this code <td th:text="${license.startDate}"></td> where license is an element of a list
@luca Maybe this is what you are looking for ;) stackoverflow.com/questions/28808838 . In your case this could be something like that: <td th:text="${#dates.format(license.startDate, 'dd/MMM/yyyy')}"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.