英文:
How to convert Timestamp from CET mariadb to java UTC Localdatetime?
问题
我有一个包含许多Unix Epoch时间戳的CET MariaDB数据库,我想将这些数据转换为带有UTC时区的LocalDateTime。
如您所见,我的MariaDB数据库使用了CET时区(第一行是命令,第二行是SQL结果):
select @@system_time_zone as tz;
CET
而我的应用程序使用了UTC(第一行是命令,第二行是控制台结果):
System.out.println("Setting the timezone::>" + TimeZone.getDefault());
Setting the timezone::>sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
我使用的是Spring Boot 2.6.4。
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.FieldNameConstants;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;
@Getter
@Setter
@Entity(name = "example")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@FieldNameConstants
public class ExampleEntity implements IEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long notificationId;
@Column(updatable = false)
private LocalDateTime creationDate;
}
这个实体由Spring JPA仓库使用(findAll方法):
import com.application.infra.db.entity.ExampleEntity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ExampleRepository extends CrudRepository<ExampleEntity, Long>, JpaSpecificationExecutor<ExampleEntity> {
}
而且,我已经配置了Spring Boot的数据源:
spring:
application:
name: '@project.name@'
datasource:
url: jdbc:mariadb://mariadbdatabase:6666/example}
username: example_user
password: example_password
driver-class-name: org.mariadb.jdbc.Driver
是否可能获取已转换为UTC时区的LocalDateTime?
我尝试使用
timezone
连接器参数,但它没有起作用。
感谢您的帮助!
英文:
I have a CET MariaDB Database containing a lot of Timestamp (Unix Epoch) and i want to convert this data to LocalDateTime with UTC Timezone.
As you can see my MariaDB Database used the CET timezone (the first line is the command, the second is the sql result) :
select @@system_time_zone as tz;
CET
And my application used UTC (the first line is the command, the second is the console result) :
System.out.println("Setting the timezone::>" + TimeZone.getDefault());
Setting the timezone::>sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
I use springboot 2.6.4.
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.FieldNameConstants;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;
@Getter
@Setter
@Entity(name = "example")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@FieldNameConstants
public class ExampleEntity implements IEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long notificationId;
@Column(updatable = false)
private LocalDateTime creationDate;
}
This entity is used by Spring jpa repository (findAll method) :
import com.application.infra.db.entity.ExampleEntity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ExampleRepository extends CrudRepository<ExampleEntity, Long>, JpaSpecificationExecutor<ExampleEntity> {
}
And i have configured springboot datasource :
spring:
application:
name: '@project.name@'
datasource:
url: jdbc:mariadb://mariadbdatabase:6666/example}
username: example_user
password: example_password
driver-class-name: org.mariadb.jdbc.Driver
Is it possible to get a LocalDateTime converted to UTC TimeZone ?
> I have try to use timezone
connector parameter but it does nothing.
Thanks for your help !
答案1
得分: 0
如果数据库配置的时区与应用程序的默认时区不同,我们需要指示JDBC使用数据库时区进行转换。添加以下属性:
spring.jpa.properties.hibernate.jdbc.time_zone=CET
英文:
If the time zone configured in the database differs from the default time zone of the application we need to instruct JDBC to use the database time zone for the conversion. Add following property:
spring.jpa.properties.hibernate.jdbc.time_zone=CET
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论