简体   繁体   English

将字符串转换为java.util.date并保留时区信息

[英]Convert a string to java.util.date AND preserve the timezone information

Before anybody downvotes this question, I have browsed the web and StackOverflow for the situation that I am facing and did not find anything, hence I am posting as a new question. 在有人否决该问题之前,我已经浏览了Web和StackOverflow以查找我所面临的情况,但未找到任何内容,因此,我将其发布为新问题。

I have a situation with java date and timezones. 我有一个Java日期和时区的情况。

Situation: 情况:

There are 2 servers on 2 different timezones, lets say PST and CST. 有2个服务器位于2个不同的时区,例如PST和CST。 I am receiving dateString(date as a string) from those servers. 我正在从这些服务器接收dateString(日期为字符串)。 But, when I try to convert the string back to date, using SimpleDateFormat, the date information(Year, month, day, hours, minutes, seconds) is being converted properly. 但是,当我尝试使用SimpleDateFormat将字符串转换回日期时,日期信息(年,月,日,小时,分钟,秒)已正确转换。 But the timezone information is not being preserved. 但是未保留时区信息。

If I run my code on a server in EST, the pstDateString is converted to Date format, but the Timezone is being set to EDT, instead of PST. 如果我在EST的服务器上运行代码,则pstDateString会转换为Date格式,但是Timezone会设置为EDT而不是PST。

I thought about it in many different ways but may be that I am stressed, I am not able to find a solution. 我以许多不同的方式考虑过它,但可能是我感到压力重重,无法找到解决方案。 Any help ? 有什么帮助吗?

Code block that would simulate the situation : 可以模拟这种情况的代码块:

        DateFormat outDF1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        outDF1.setTimeZone(TimeZone.getTimeZone("PST"));        
        String pstDateString = outDF1.format(new Date());

        DateFormat outDF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        outDF2.setTimeZone(TimeZone.getTimeZone("CST"));
        String cstDateString = outDF2.format(new Date());

        System.out.println("pstDateString "+pstDateString);
        System.out.println("cstDateString "+cstDateString);

        Date cstDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").parse(cstDateString);
        Date pstDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").parse(pstDateString);

        System.out.println("Date after format from string: "+pstDate);
        System.out.println("Date after format from string: "+cstDate);

Output Currently: 当前输出:

pstDateString 2012-06-07 10:26:689
cstDateString 2012-06-07 12:26:694
Date after format from string: Thu Jun 07 10:26:00 EDT 2012
Date after format from string: Thu Jun 07 12:26:00 EDT 2012

Output Expected: 预期输出:

pstDateString 2012-06-07 10:26:689
cstDateString 2012-06-07 12:26:694
Date after format from string: Thu Jun 07 10:26:00 PST 2012
Date after format from string: Thu Jun 07 12:26:00 CST 2012

the java.util.Date class has no timezone, it is always in UTC. java.util.Date类没有时区,始终为UTC。 if you want to preserve the incoming timezone, you will need a new class which combines a Date and a TimeZone. 如果要保留传入的时区,则需要一个新的类,该类结合了Date和TimeZone。 you could create a simple holder class, or possibly use a Calendar. 您可以创建一个简单的holder类,也可以使用Calendar。

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

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