Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture milliseconds in grails domain

Tags:

time

mysql

grails

I have a grails domain that stores data in MySQL.

In it I have a Date property which creates a DateTime field in the DB. However, I also wish to capture the milliseconds in addition to the date.

Is there a way to capture milliseconds?

Example:

Class MyClass {

  Date dateCreated

}

stores data in MySQL as 2014-10-06 16:21:57 but I'd like to capture milliseconds as well.

like image 655
Anthony Avatar asked Jan 26 '26 18:01

Anthony


1 Answers

MySQL 5.6.4 and up can save milliseconds in the database for datetime objects by defining the amount of fractional points for the object. (Read more here: MySQL reference manual). By default Grails/Gorm doesn't do this. Fortunately, this is easily solved by defining a sqlType in the domain like this:

class ExampleDomain {
    Date date

    static mapping = {
        date sqlType: 'datetime(3)'
    }
}
like image 167
ascu Avatar answered Jan 28 '26 08:01

ascu



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!