How to grab milliseconds in grails domain

I have a grails domain that stores data in MySQL.

In it I have a property Date

that creates a field DateTime

in the DB. However, I would also like to record the milliseconds in addition to the date.

Is there a way to capture milliseconds?

Example:

Class MyClass {

  Date dateCreated

}

      

stores data in MySQL like 2014-10-06 16:21:57

, but I would also like to record milliseconds.

+3


source to share


2 answers


MySQL 5.6.4 and up can store milliseconds in the database for datetime objects by specifying the number of decimal points for the object. (Read more here: MySQL Reference Manual ). By default, Grails / Gorm does not. Fortunately, this can be easily solved by defining the sqlType on the domain like this:



class ExampleDomain {
    Date date

    static mapping = {
        date sqlType: 'datetime(3)'
    }
}

      

+2


source


You can use bigint (20). Your groovy / grails domain might look like this if you want to save nanoseconds since unix automatically when the instance is saved:



class myClass {

  Integer id
  Long timestamp = System.nanoTime()

  static mapping = {
    timestamp sqlType: "bigint"
  }
}

      

0


source







All Articles