如何将来自CET MariaDB的时间戳转换为Java UTC LocalDateTime?

huangapple go评论68阅读模式
英文:

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连接器参数,但它没有起作用。 如何将来自CET MariaDB的时间戳转换为Java UTC LocalDateTime?

感谢您的帮助!

英文:

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(&quot;Setting the timezone::&gt;&quot; + TimeZone.getDefault());
Setting the timezone::&gt;sun.util.calendar.ZoneInfo[id=&quot;Etc/UTC&quot;,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 = &quot;example&quot;)
@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&lt;ExampleEntity, Long&gt;, JpaSpecificationExecutor&lt;ExampleEntity&gt; {

}

And i have configured springboot datasource :

spring:
  application:
    name: &#39;@project.name@&#39;
  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. 如何将来自CET MariaDB的时间戳转换为Java UTC LocalDateTime?

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

huangapple
  • 本文由 发表于 2023年6月29日 00:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定