我们将oracle12cdb用于spring批处理应用程序。系统时区为pst。但是我需要utc时区的元表中与作业和步骤相关的条目。有什么建议吗?
p4tfgftt1#
按照最佳实践,将时间预设为 UTC 被认为是最佳实践,为了避免将来springbootjpa出现错误,请在应用程序中使用以下代码。属性文件,显然您可以根据自己的选择修改时区:
UTC
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 选择和投射对象数组,例如:您可以使用oracle功能将pst转换为utc
JPA Native Query
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);}
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);
}
命令行运行程序
@Componentpublic class LoaderBootStrap implements CommandLineRunner { private final EmployeeRepository employeeRepository; @Override public void run(String... args) throws Exception { List<Object[]> withSalary = employeeRepository.findWithSalary(10001); }}
@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(); }
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();
1条答案
按热度按时间p4tfgftt1#
按照最佳实践,将时间预设为
UTC
被认为是最佳实践,为了避免将来springbootjpa出现错误,请在应用程序中使用以下代码。属性文件,显然您可以根据自己的选择修改时区:也可以直接附加为:
但要解决这个问题,你有两个选择:
1-
JPA Native Query
选择和投射对象数组,例如:您可以使用oracle功能将pst转换为utc
jpa存储库:
命令行运行程序
2-使用java在时区之间转换日期和时间,例如(utc+8:00)亚洲/新加坡-新加坡时间到(utc-5:00)美洲/纽约-东部标准时间: