[JAVA] About date / time type of Doma2 entity class

Premise of this topic

--Use Doma2. --Automatically generate Entity with Doma-Gen. --UTC is epoch seconds. --The DB TIMESTAMP type shall be able to store UTC (epoch seconds).

The site that I used as a reference

Which type should handle DB TIMESTAMP type (date and time) in entity class?

When there is a TIMESTAMP type (date and time) in the DB table, I was wondering which type to handle. Please give us your opinion.

Date / time type supported by Doma2

Doma2 supports the following linked types as date / time types. http://doma.readthedocs.io/ja/stable/basic/#id4

Among them, when dealing with date + time, it will be one of the following types.

Doma-Gen generated type

In Doma-Gen in the Doma1 era, the type corresponding to the TIMESTAMP type (date and time) was java.sql.Timestamp. http://doma.seasar.org/extension/doma_gen_gen_task.html#EntityConfig

However, in Doma-Gen of Doma2, it is java.time.LocalDateTime. http://doma-gen.readthedocs.io/ja/stable/gen/#entityconfig

What I think is a problem with java.time.LocalDateTime

For more information on the following site, java.time.LocalDateTime does not store the date and time in UTC, but rather the date and time, which does not contain any timezone information. https://qiita.com/dmikurube/items/15899ec9de643e91497c#javatimelocaldatetime

In DB TIMESTAMP type (date and time), the date and time are stored in UTC, so if you handle it with java.time.LocalDateTime, the time zone information will be lost.

By the way, in PostgreSQL, it is stored internally in UTC. https://www.postgresql.jp/document/9.6/html/datatype-datetime.html

Should I use java.sql.Timestamp?

java.sql.Timestamp can hold UTC, so if you specify a time zone, the date and time in that time zone will be determined. In other words, if the date and time are handled in UTC inside the system, you can specify the time zone and convert it to the date and time representation of that time zone at the time of output. In that sense, I think it's better to use java.sql.Timestamp.

On the other hand, in the case of java.time.LocalDateTime, it is necessary to be aware of which time zone the date and time representation is, which is the date and time that does not include any time zone information. For example, if both the DB and the system can be limited to one time zone, such as only the time zone in Japan, it may be sufficient.

How to change the type corresponding to TIMESTAMP type (date and time) to java.sql.Timestamp in Doma-Gen

Prepare a custom GenDialect class.

Prepare a custom class by inheriting the GenDialect class of the DB to be used (GenDialect class of PostgreSQL in this example).

/**
 *A class that customizes dialects for PostgreSQL
 */
public class CustomPostgresGenDialect extends PostgresGenDialect {
    public CustomPostgresGenDialect() {
        //Timestamp with timezone is java.sql.Map to Timestamp.
        classNameMap.put("timestamptz", Timestamp.class.getName());
    }
}

Specify in the top level parameter of Doma-Gen.

Run Doma-Gen with the above class in the top level genDialectClassName. (The specified class must be included in the class path when executing the task.) http://doma-gen.readthedocs.io/ja/stable/gen/#id2

Recommended Posts

About date / time type of Doma2 entity class
Use of Date class
[Ruby] Date, Time, Datetime class basics
Explanation of Ruby Time and Date objects
[Basic knowledge of Java] About type conversion
About next () and nextLine () of the Scanner class
The date time of java8 has been updated
Date and time
About class inheritance.
About Java class
[Java] How to use compareTo method of Date class
Speed comparison at the time of generation at the time of date conversion
Usability of date and time classes in each language
Output of the day of the week using Ruby's Date class
Handling of date and time in Ruby. Use Date and Time properly.