[英]TimeZone Conversion with SimpleDateFormat in Java
I have a SimpleDateFormat parser that parse in this way: 我有一个SimpleDateFormat解析器,它以这种方式解析:
sdf = new java.text.SimpleDateFormat("yyyy-MM-DD HH:mm:ss z").parse("2013-10-25 17:35:14 EDT");
log.debug(sdf);
This give me Sat Jan 26 03:05:14 IST 2013 这给了我星期六2013年1月26日03:05:14
What am i missing here? 我在这里想念什么?
First of all, DD stands for day in year, You should use dd instead. 首先,DD代表一年中的每一天,您应该使用dd代替。
Also, if you want to print a date in a specific format, you need to use two SimpleDateFormat
s. 另外,如果要以特定格式打印日期,则需要使用两个
SimpleDateFormat
。
SimpleDateFormat.parse
returns a Date
object represents the date you specified at the given format. SimpleDateFormat.parse
返回一个Date
对象,该对象表示您以给定格式指定的日期。
The Date
object itself is saved as a regular Date
, no format attached to it . Date
对象本身将保存为常规Date
, 不附带任何格式 。
If you want to print it in a specific format, you need to use another SimpleDateFormat
and call format
method. 如果要以特定格式打印,则需要使用另一种
SimpleDateFormat
和调用format
方法。
you should use Format 您应该使用格式
SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:SS z");
String sdf = sdf1.format(sdf1.parse("2013-10-25 17:35:14 EDT"));
There are two things. 有两件事。
sdf
is an object of Date
, which represents a specific instant in time (milliseconds elapsed since another instant known as "the epoch"). sdf
是Date
的对象,它表示特定的时间点(自另一个称为“纪元”的瞬间起经过的毫秒数)。 There is no format which is known to this object. 没有该对象已知的格式。 And how this object is printed is solely handled by its class'
toString
method, which prints the date in this format: 而此对象的打印方式仅由其类的
toString
方法处理,该方法以以下格式打印日期:
dow mon dd hh:mm:ss zzz yyyy
This is exactly what you see in your output. 这正是您在输出中看到的。 Note that the timezone of the machine running the program is printed in this case.
请注意,在这种情况下,将打印运行程序的机器的时区。 If you wish to print this object in a format of your choice you should use a
DateFormat
. 如果希望以所选格式打印该对象,则应使用
DateFormat
。 To get output for a specific timezone you have to explicitly tell it to the DateFormat
object (see below). 要获取特定时区的输出,必须将其显式地告知
DateFormat
对象(请参见下文)。
Another thing is you should be using dd
instead of DD
in the pattern. 另一件事是您应该在模式中使用
dd
而不是DD
。 D
is for day in year and d
is for day in month, which I believe is what you want. D
是一年中的一天, d
是月中的一天,我相信这是您想要的。 Now keeping in mind both the points, this is one of the solutions to your problem: 现在牢记这两点,这是解决问题的方法之一:
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("EDT")); // Time Zone for output
Date d = sdf.parse("2013-10-25 17:35:14 EDT");
System.out.println(sdf.format(d)); // You need format method
which outputs: 输出:
2013-10-25 17:35:14 EDT
What are the people answering not getting here is more the question: 人们回答没有到达这里的是更多的问题:
2013-10-25 17:35:14 EDT != Sat Jan 26 03:05:14 IST 2013
I think it is because 'EDT' is the timezone and so, when it is 17:35 in EDT is is 3:05 in the UK, ignoring Daylight saving adjustments. 我认为这是因为“ EDT”是时区,因此在英国东部夏令时(EDT)为17:35时在英国是3:05,而忽略了夏令时调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.