在gorm中使用时间的数据类型?

时间:2023-01-26 17:01:01

how can i handle a time (like 14:33) during calculations and savings in database? What datatype can i use for this in a HSQL DB?

我如何在计算和数据库节省期间处理时间(如14:33)?我可以在HSQL DB中使用什么数据类型?

I want to calculate things like workload or rest time. Is there a built in possibility to convert times to decimals?

我想计算工作量或休息时间等事情。是否存在将时间转换为小数的可能性?

Thanks!

谢谢!

1 个解决方案

#1


4  

As for the time representation, the most convenient type ought to be Joda Time's LocalTime.
There are several converters available to map the LocalTime type to the database.

至于时间表示,最方便的类型应该是Joda Time的LocalTime。有几个转换器可用于将LocalTime类型映射到数据库。

You need the JAR packages joda-time and joda-time-hibernate.

你需要JAR包joda-time和joda-time-hibernate。

Your domain class then might look like this:

您的域类可能如下所示:

import org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime
import org.joda.time.LocalTime

class TimeDomain {
    LocalTime localTime

    static mapping = {
        localTime type: PersistentLocalTimeAsTime
    }

    // business logic would belong to a service class
    LocalTime plusMinutes(int minutes) {
        localTime.plusMinutes(minutes)
    }
}

#1


4  

As for the time representation, the most convenient type ought to be Joda Time's LocalTime.
There are several converters available to map the LocalTime type to the database.

至于时间表示,最方便的类型应该是Joda Time的LocalTime。有几个转换器可用于将LocalTime类型映射到数据库。

You need the JAR packages joda-time and joda-time-hibernate.

你需要JAR包joda-time和joda-time-hibernate。

Your domain class then might look like this:

您的域类可能如下所示:

import org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime
import org.joda.time.LocalTime

class TimeDomain {
    LocalTime localTime

    static mapping = {
        localTime type: PersistentLocalTimeAsTime
    }

    // business logic would belong to a service class
    LocalTime plusMinutes(int minutes) {
        localTime.plusMinutes(minutes)
    }
}