日期值格式错误 [英] Date value wrongly formatted

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

问题描述

我试图转换一个String DateTime 值,该值在平面文件中作为 Date 对象转换后在我的代码中解析平面文件。

I am trying to convert a String DateTime value which is present in a flat file as a Date object after parsing the flat file in my code.

我已经编写了代码来执行此操作但是当我格式化日期时,它总是给我一个超过1天的指定日期价值,有时它会增加5:30。

I have written the code to do that but when I format the date its always giving me a date more than 1 day for the specified value, some times it's adding 5:30.

以下是代码:

    DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
    Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
    System.out.println("---date----" + date);

上述输出是

    ---date----Wed Aug 24 08:30:03 IST 2011  

请告诉我这里的问题。我在 SimplaDateFormat 类中使用的模式是否存在问题,或者代码是否存在问题。
我已经在这个问题很长时间了。

Can you please let me know whats the issue here. Is there a problem in the pattern that I am using in the SimplaDateFormat class or is there a problem with the code. I have been scratching my head on this for a long time now.

推荐答案

您的系统时区不同。输出显示IST - 或印度标准时间,与PDT相差12.5h。代码正确解析给定日期,即PDT(UTC -7)并打印出IST(UTC + 5h30)。

Your system timezone is different. The output is showing IST - or Indian Standard Time, which is an 12.5h difference from PDT. The code is properly parsing the given date which is PDT (UTC -7) and printing out IST (UTC +5h30).

Java将日期存储为UTC日期。因此,当您解析PDT日期时,Java会将其转换为UTC并将其作为UTC时间戳存储在内部。当你打印时,如果你没有指定时区,它将默认为系统时区,在你的情况下,它似乎是IST。

Java stores Dates as UTC dates. So when you parse the PDT date, Java will convert it to UTC and store it internally as a UTC timestamp. When you print, if you do not specify the timezone, it will default to the system timezone, which in your case would appear to be IST.

指定一个确切的时区,在SimpleDateFormat中指定它:

To specify an exact timezone, specify it in the SimpleDateFormat:

DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
f.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
System.out.println("---date----" + f.format(date));

这篇关于日期值格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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