【问题标题】:How to convert string timestamp into timestamp with a specific timezone如何将字符串时间戳转换为具有特定时区的时间戳
【发布时间】:2021-03-26 07:10:54
【问题描述】:

我正在尝试将其中包含时区的字符串时间戳转换为具有相同时区的时间戳。但是,在执行以下代码时,我得到了默认的系统时间戳。有人可以帮我解决这个问题。下面是我正在尝试的代码。

try {
    Date dt = new Date();
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    f.setTimeZone(TimeZone.getTimeZone("GMT"));
    String dateString = f.format(dt);
    System.out.println("DateString: "+dateString);
    Date parsedDate = f.parse(dateString);
    System.out.println("ParsedDate: "+parsedDate);
    Timestamp timestamp = new Timestamp(parsedDate.getTime());
    System.out.println("Timestamp: "+timestamp);
    
}catch (Exception e) {
    // TODO: handle exception
}

执行上述代码时,我得到以下结果:

DateString: 2021-03-26T06:57:05.982+0000
ParsedDate: Fri Mar 26 12:27:05 IST 2021
Timestamp: 2021-03-26 12:27:05.982

但我必须在时间戳中输出 2021-03-26T06:57:05.982+0000

【问题讨论】:

  • 阅读答案:stackoverflow.com/questions/2891361/…。如果您必须使用时区,最好的建议是不要使用 java.util.Date。使用 java.time 包中的类。
  • 这里(至少)要提到两件事:首先,考虑@Torben 给出的评论。其次,您为什么想知道格式化的输出与未格式化的输出不同?您首先打印带有SimpleDateFormatDate,然后是没有任何格式的打印,最后您打印的是Timestamp...
  • 我想得到带有时区的时间戳,格式为 2021-03-26T06:57:05.982+0000 加上 +0530 或 -0530,这里我必须得到时间格式为 yyyy-MM-dd 'T'HH:mm:ss.SSSZ 指定时区。当我试图获得上面的输出时,它正在工作,但它是字符串,所以想在时间戳中解析它。所以试图将该字符串转换为时间戳,但它返回的是系统时间戳而不是提到的时区时间戳。
  • a Timestamp(假设java.sql.Timestamp)只是一个瞬间,不涉及时区。您可以在不同的时区给出它的表示。也许您需要一个ZonedDateTime,即具有相应时区的日期和时间;甚至是完全没有时区的LocalDateTime

标签: java timestamp timestamp-with-timezone sql-timestamp


【解决方案1】:

您的代码使用 java.time 类:

        ZonedDateTime zdt = ZonedDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        DateTimeFormatter dfGMT = df.withZone(ZoneId.of("GMT"));
        
        String dateString = dfGMT.format(zdt);
        System.out.println("DateString: "+dateString);
        
        ZonedDateTime parsedDate = ZonedDateTime.parse(dateString,dfGMT);
        System.out.println("ParsedDate: "+ parsedDate);
        Timestamp timestamp = Timestamp.from(parsedDate.toInstant());
        System.out.println("Zoned Timestamp: "+timestamp);
        
        //ignoring zone info from date string
        LocalDateTime ldt = LocalDateTime.from(dfGMT.parse(dateString));
        timestamp = Timestamp.valueOf(ldt);
        System.out.println("Zone stripped GMT timestamp: "+timestamp);
        
        
        ZonedDateTime zdt1 = ldt.atZone(ZoneId.of("GMT"));
        zdt1 = zdt1.withZoneSameInstant(ZoneId.of("America/Chicago"));
        timestamp = Timestamp.valueOf(zdt1.toLocalDateTime());
        System.out.println("Zone stripped CST timestamp: "+timestamp);

输出:

DateString: 2021-03-26T09:10:37.537+0000
ParsedDate: 2021-03-26T09:10:37.537Z[GMT]
Zoned Timestamp: 2021-03-26 14:40:37.537
Zone stripped GMT timestamp: 2021-03-26 09:10:37.537
Zone stripped CST timestamp: 2021-03-26 04:10:37.537

【讨论】:

  • 感谢您的回复,这里我想输入时间戳为 2021-03-26T07:40:55.771+0000 你能帮忙吗
  • java.sql.Timestamp 处理本地时区的时间。因此,如果您将 GMT 时间提供给 Timestamp,如果您的服务器在印度,它会将其转换为 IST。你要问的是忽略日期字符串提供的时区,只使用本地时区的日期时间。
  • 我已经修改了答案。要获得您想要的结果,请查找 Zone stripped timestamp 输出。
  • 完成! CST 比格林威治标准时间晚 5 小时。
【解决方案2】:

java.time 和 JDBC 4.2

您不需要任何格式化、解析或转换。将当前时间戳插入您的 SQL 数据库:

    OffsetDateTime currentTimestamp = OffsetDateTime.now(ZoneOffset.UTC);
    String sql = "insert into your_table(your_timestamp_with_time_zone_column) values (?);";
    try (PreparedStatement prepStmt = yourDatabaseConnection.prepareStatement(sql)) {
        prepStmt.setObject(1, currentTimestamp);
        prepStmt.executeUpdate();
    }

【讨论】:

    猜你喜欢
    • 2020-11-22
    • 2015-03-03
    • 2013-09-25
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多