Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring - MySQL timestamp field and millisecond precision

I'm trying to store some time information in MySQL database with millisecond precision. The format that I want to store is 2015-08-13 10:56:24.570 like that.

The problem is, I have to create table column with length attributes (3 or 6, depends on fractional part). The application that I wrote with Java Spring Framework automatically creates mysql table if it isn't created before. But I can't set the length attributes of date field.

"@Column annotation has the length argument but it only considerable with String fields." says the document of Spring Jpa.

Another interesting part is I couldn't use @Temporal annotation. My IDE gives an error that "The annotation @Temporal is disallowed for this location". I wonder why I given this error also.

Any help would be great for me.

EDIT: My Java code for creating mysql table

@Entity
@Table(name = "dbTable")
public class Report
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "myDate", nullable = false, length = 6)
    private java.util.Date date;

    // Getters Setters etc.

}
like image 301
msaiducar Avatar asked Mar 25 '26 01:03

msaiducar


1 Answers

Make the column of type

java.sql.Timestamp

while storing the time related information in MySQL.

Simple Example:-

  public static void main(String[] args){
    System.out.println(java.sql.Timestamp.valueOf("2013-10-10 10:49:29.12000"));
}

Output:- 2013-10-10 10:49:29.12

Read this, if it helps.

Change the column type to java.sql.Timestamp from java.util.Timestamp.

like image 78
Amit Bhati Avatar answered Mar 26 '26 14:03

Amit Bhati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!