【问题标题】:Get the Gregorian date from a java.sql.Timestamp从 java.sql.Timestamp 获取公历日期
【发布时间】:2020-10-04 20:17:22
【问题描述】:

我已经使用实体管理器执行了查询。 它从多个表返回数据。在那种情况下,我会得到对象数组的列表。所以我写了一个循环如下。

for (Object[] row : rows) {
    row[0]; //row[0] has the date from database as timestamp.
}

这里row[0]是一个sql时间戳。
如果我们通过调试进行评估,我会得到带有结果的时间戳:2020-05-27 18:37:39.0。

我可以看到cdate,一个私有变量,其值为2020-05-27T18:37:39.000+0530。这是公历日期。 我不能使用它,因为它是一个私有变量,它的类型是BaseCalendar.Date。

如果你做某事并将其转换为 UTC,我将得到 2020-05-27T13:07:39Z

但我希望它为2020-05-27T18:37:39.000+0530

【问题讨论】:

  • row[0] 是 java.sql.Timestamp 类型吗?还是有任何自定义类型?
  • @deHaar 是的,它的类型是 java.sql.Timestamp
  • 这能回答你的问题吗? How to convert string date to Timestamp in java?
  • @DigvijayS 将返回 2020-05-27T13:07:39Z 的 UTC,但我需要它作为 2020-05-27T18:37:39.000+0530。

标签: java sql spring-boot date java-8


【解决方案1】:

我也建议您使用现代 Java 日期和时间 API java.time。如果可以,请修改您的查询,使其不返回老式的 Timestamp 对象,而是返回一个 Instant 或至少一个 LocalDateTime。

那么你可以这样做:

    // Example row
    Object[] row = { Instant.parse("2020-05-27T13:07:39.000Z") };
    System.out.println(row[0]);

    ZonedDateTime zdt = ((Instant) row[0]).atZone(ZoneId.systemDefault());
    System.out.println(zdt);

当在亚洲/加尔各答时区运行时,此示例的输出是:

2020-05-27T13:07:39Z
2020-05-27T18:37:39+05:30[Asia/Kolkata]

如果您无法更改查询的返回类型,则正确的转换是:

    // Example row
    Object[] row = { Timestamp.from(Instant.parse("2020-05-27T13:07:39.000Z")) };
    System.out.println(row[0]);

    ZonedDateTime zdt = ((Timestamp) row[0]).toInstant()
            .atZone(ZoneId.systemDefault());
    System.out.println(zdt);

输出是:

2020-05-27 18:37:39.0
2020-05-27T18:37:39+05:30[Asia/Kolkata]

请注意:转换时不要使用任何字符串,存在更直接的转换。

我假设您使用的是该值,不一定使用您在调试器中看到的相同格式。如果您确实想要这种格式:您看到的是BaseCalendar.Date.toString() 的结果,它是国际标准 ISO 8601 格式的变体。要获得它,请使用格式化程序。例如:

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .append(DateTimeFormatter.ISO_LOCAL_DATE)
            .appendLiteral('T')
            .appendPattern("HH:mm:ss.SSSxx")
            .toFormatter();
    String formattedDateTime = zdt.format(formatter);
    System.out.println(formattedDateTime);
2020-05-27T18:37:39.000+0530

链接

【讨论】:

    【解决方案2】:

    您可以使用java.time,它使您能够在不考虑区域或偏移量的情况下解析日期时间,但可以在不更改或调整解析值的情况下随后添加一个:

    public static void main(String[] args) {
        // let's assume you have a timestamp with the following value
        Timestamp ts = Timestamp.valueOf("2020-05-27 18:37:39");
        // then you can parse it to a datetime that doesn't consider a zone or offset
        LocalDateTime ldt = LocalDateTime.parse(ts.toString(),
                                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));
        // print it once
        System.out.println(ldt);
        // then add an offset without changing / adjusting the time values
        OffsetDateTime odt = ldt.atOffset(ZoneOffset.of("+05:30"));
        // and print that
        System.out.println(odt);
        // or use a default format to exactly meet your requirements
        System.out.println(odt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")));
    }
    

    这个输出

    2020-05-27T18:37:39
    2020-05-27T18:37:39+05:30
    2020-05-27T18:37:39.000+0530
    

    【讨论】:

      【解决方案3】:

      如下使用modern date/time API:

      import java.time.OffsetDateTime;
      import java.time.format.DateTimeFormatter;
      
      public class Main {
          public static void main(String args[]) {
              // Define the format
              DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
      
              // Parse the date/time string using the defined format
              OffsetDateTime odt = OffsetDateTime.parse("2020-05-27T18:37:39.000+0530", formatter);
      
              // Display
              System.out.println(odt);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        • 1970-01-01
        • 1970-01-01
        • 2015-11-22
        相关资源
        最近更新 更多