繁体   English   中英

Spring 批处理 - 元表 - Oracle 12C - UTC 时区条目

[英]Spring Batch - Meta tables - Oracle 12C - Entries in UTC timezone

我们将 Oracle 12C DB 用于 Spring 批量应用程序。 系统时区是 PST。 但我希望在 UTC 时区的元表中与作业和步骤相关的条目。 有什么建议吗?

遵循最佳实践,将预设时间设置为UTC被认为是最佳实践,并避免将来出现错误 Spring 启动 JPA,在您的应用程序中使用以下代码。 属性文件,显然您可以根据自己的选择修改时区:

spring.jpa.properties.hibernate.jdbc.time_zone = UTC

或者您可以直接将其附加为:

spring.datasource.url=jdbc:oracle:thin://localhost:3306/linkedin?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false

但是要解决问题,您有两种选择:

1- JPA Native Query到 select 并转换对象数组,例如:

您可以利用 oracle 功能将 PST 转换为 UTC

select cast(coltime as timestamp) at time zone 'UTC' from ...

JPA 存储库:

public interface EmployeeRepository extends JpaRepository<Employee, Long> {
    @Query(value = "select cast(DD.Ch_Status_Validfrom as timestamp) at time zone 'UTC', za.first_name, za.birth_date, ts.salary\n" +
            "from employees za, salaries ts  \n" +
            "where za.emp_no= :id" +
            " LIMIT 10 OFFSET 20"
            ,nativeQuery = true)
    List<Object[]> findWithSalary(@Param("id")Integer id);

}

CommandLineRunner

@Component
public class LoaderBootStrap implements CommandLineRunner {

    private final EmployeeRepository employeeRepository;

    @Override
    public void run(String... args) throws Exception {

        List<Object[]> withSalary = employeeRepository.findWithSalary(10001);
    }
}

2-将时区之间的日期和时间转换为 java 例如 (UTC+8:00) 亚洲/新加坡 - 新加坡时间到 (UTC-5:00) 美国/纽约 - 东部标准时间:

 private final void demo(){
     ZoneId assiaZone = ZoneId.of("Asia/Singapore");
     ZoneId americaZone = ZoneId.of("America/New_York");

     ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));

     System.out.println("Current Time As UTC : " + zonedDateTime);
     System.out.println("Current time As America time : " + dateConvertWithZoneId(zonedDateTime, americaZone));
     System.out.println("Current time As Asia time : " + dateConvertWithZoneId(zonedDateTime, assiaZone));
        
}
        
 private final LocalDateTime dateConvertWithZoneId(ZonedDateTime actualDate, ZoneId withZone){

    ZonedDateTime date = actualDate;

    return date.withZoneSameInstant(withZone).toLocalDateTime();

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM