解析具有不同时区的日期 [英] Parsing date with different time zones

查看:135
本文介绍了解析具有不同时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使使用Java大约有15年的时间,人们总是会在处理日期和时间这个话题上迷失方向...

Even with about 15 years in Java one always stumbles over the topic of handling dates and times...

在这种情况下:我从一些外部工具获得了时间戳记系统作为 String 表示形式。时间戳记的语义是它代表UTC日期。必须将此时间戳放入一个实体中,然后在 TIMESTAMP 字段中放入一个PostgreSQL数据库中。另外,我需要将与本地时间相同的时间戳记(在我的情况下为CEST)放入实体,然后在带时区的时间戳记字段中放入数据库。

Here's the situation: I get a timestamp from some external system as a String representation. The timestamp's semantic is that it represents an UTC date. This timestamp has to be put in an entity and then into a PostgreSQL database in a TIMESTAMP field. Additionally I need to put the same timestamp as local time (in my case CEST) into the entity and then into the database in a TIMESTAMP WITH TIME ZONE field.

什么是确保无论执行代码的机器的设置如何,时间戳都正确存储在实体中的正确方法(以其他UTC时间戳进行验证)

What is the right way to ensure that no matter what the settings of the machine executing the code are, the timestamps get stored correctly in the entity (to make some validations with other UTC timestamps) and in the database (to use them in reports later on)?

这是代码,在我的本地计算机上运行良好:

Here's the code, which worked fine on my local machine:

SimpleDateFormat sdfUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
sdfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
Date utcTimestamp = sdfUTC.parse(utcTimestampString);
// getMachinesTimezone is some internal util method giving the TimeZone object of the machines Location
Calendar localTimestamp = new GregorianCalendar(getMachinesTimezone());
localTimestamp.setTimeInMillis(utcTimestamp.getTime());

但是在服务器上执行相同的代码时,会导致不同的时间,所以我认为不是正确的处理方式。

But when executing the same code on the server, it resulted in different times, so I assume that it's not the correct way to handle it. Any suggestions?

PS:在此论坛上搜索时,我读到了有关Joda Time的信息,但是在给定的项目中,我无法引入新的库,因为我只更改了现有模块,所以我必须使用标准的JDK1.6

PS: I read about Joda Time when searching in this forum, but in the given project I'm not able to introduce new libraries since I only change an existing module, so I have to live with the standard JDK1.6

推荐答案

如果我理解正确,则需要设置时区在要打印的同一数据/日历对象上。像这样:

If I understand correctly, You need to set the timezone on the same data/calendar object that you are printing. Like this:

private Locale locale = Locale.US;
private static final String[] tzStrings = {
    "America/New_York",
    "America/Chicago",
    "America/Denver",
    "America/Los_Angeles",
};

  Date now = new Date();
  for ( TimeZone z : zones) {
        DateFormat df = new SimpleDateFormat("K:mm a,z", locale);
        df.setTimeZone(z);
        String result = df.format(now);
        System.out.println(result); 
  }

如果我将时区设置为SimpleDateFormat,则可以正常工作。

if i set timezone to SimpleDateFormat it is working fine.

这是示例代码...

String date="05/19/2008 04:30 AM (EST)";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa (z)");
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
long millis = sdf.parse(date).getTime();
sdf.setTimeZone(TimeZone.getDefault());
System.out.println(sdf.format(new Date(millis)));

这篇关于解析具有不同时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆