luca
luca

Reputation: 3318

Mapping MySql Date into java object

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

Upvotes: 2

Views: 2618

Answers (2)

Fonexn
Fonexn

Reputation: 301

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

Upvotes: 2

Luca Putzu
Luca Putzu

Reputation: 1456

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.

Upvotes: 0

Related Questions