`` `11: 12: 13.14``` (hours: minutes: seconds.milliseconds) obtained as a String type INSERT into MySQL And how to display up to milliseconds. (No date information required)
*** Isn't it possible to implement it normally? *** *** Do you think
It didn't work unless it seemed easy and had troublesome processing. .. .. If anyone knows a smarter way, I would be very happy if you could tell me! !!
Spring
build.gradle
id 'org.springframework.boot' version '2.2.2.BUILD-SNAPSHOT'
sourceCompatibility = '1.8'
runtimeOnly('mysql:mysql-connector-java')
implementation 'org.springframework.boot:spring-boot-starter'
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
MySQL 8.0
Supports up to 3 decimal places MySQL :: MySQL 5.6 Reference Manual :: 11.1.2 Date and Time Type Overview
create table timesample(
...
start_time TIME(3)
);
Since JPA does not support java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime as the field types of entities, you need to create the following AttributeConverter.
package app.entities.converter;
import java.time.LocalTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class LocalTimeConverter implements AttributeConverter<LocalTime, java.sql.Time> {
@Override
public java.sql.Time convertToDatabaseColumn(LocalTime time) {
return time == null ? null : java.sql.Time.valueOf(time);
}
@Override
public LocalTime convertToEntityAttribute(java.sql.Time value) {
return value == null ? null : value.toLocalTime();
}
}
Entity
@Column(name = "start_time")
private LocalTime startTime;
entity.setStartTime(LocalTime.of(11, 12, 13, 14*10000000));
repository.save(entity);
There are no milliseconds!
Change the convertToDatabaseColumn method of the LocalTimeConverter created earlier [How to map spring-millisecond java.time.LocalTime to millisecond java.sql.Time](https://stackoverflow.com/questions/51182366/how-to-map-java-time-localtime- with-milliseconds-to-a-java-sql-time-with-milisec)
LocalTimeConverter
package app.entities.converter;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class LocalTimeConverter implements AttributeConverter<LocalTime, java.sql.Time> {
@Override
public java.sql.Time convertToDatabaseColumn(LocalTime time) {
if(time == null) return null;
//This date will be discarded by conversion, so anything is OK
long epochMilli = time.atDate(LocalDate.of(2019, 1, 1)).atZone(java.time.ZoneId.systemDefault())
.toInstant().toEpochMilli();
java.sql.Time sqlTime = new java.sql.Time(epochMilli);
return sqlTime;
}
@Override
public LocalTime convertToEntityAttribute(java.sql.Time value) {
if (value == null) return null;
String localTimeStr = new SimpleDateFormat("HH:mm:ss.SSS").format(value);
LocalTime localTime = LocalTime.parse(localTimeStr);
return localTime;
}
}
Recommended Posts