简体   繁体   中英

How to capture milliseconds in grails domain

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.

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)'
    }
}

You could use bigint(20). Your groovy/grails domain could look like that if you want to store the nanoseconds since unix epoch automatically when a instance will be saved:

class myClass {

  Integer id
  Long timestamp = System.nanoTime()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM