使用时区java将字符串转换为适当的日期 [英] Convert string to appropriate date with timezone java
问题描述
我正在使用时区,我想将其转换为另一个时区,例如我有时间'GMT-06:00'的日期'3/15/2013 3:01:53 PM'。我想在GMT-05:00时区转换。我有很多的搜索,我很困惑,实际上日期是如何工作的。如何应用时区到目前为止。我尝试使用SimpleDateFormat,Calender和偏移量。
I am Having Date with it's timezone, I want to convert it to another Timezone, E.g. I have Date '3/15/2013 3:01:53 PM' which is in TimeZone 'GMT-06:00'. I want to convert this in 'GMT-05:00' timezone. I have search lot, and I am confuse about How actually Date is working. How to Apply timezone to date. I have try with SimpleDateFormat, Calender and also with offset.
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aaa XXX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date dt = null;
try
{
dt = df.parse("3/15/2013 3:01:53 PM -06:00");
}catch (ParseException e) {
e.printStackTrace();
}
String newDateString = df.format(dt);
System.out.println(newDateString);
它返回输出
03/15/2013 09:01 :53 AM Z
。
我猜应该是
03/15/2013 09:01:53 PM Z
,因为在GMT-06:00时区的时间,所以在GMT时间应该是HH + 6。我想要日期在yyyy-MM-dd HH:mm:ss格式,其中HH在24小时内。请帮助我举个例子。提前致谢。
It returns output
03/15/2013 09:01:53 AM Z
.
I guess it should be
03/15/2013 09:01:53 PM Z
, because time in 'GMT-06:00' timezone, so it should be HH+6 to get time in GMT. I want Date in "yyyy-MM-dd HH:mm:ss" format where HH is in 24 hour.Please Help me with example. Thanks in advance.
编辑:
我使用SimpleDateFormat将字符串转换为日期
I am converting the string into date using SimpleDateFormat
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aaa");
Date dt = null;
try {
dt = df.parse("3/15/2013 3:01:53 PM");
} catch (ParseException e) {
e.printStackTrace();
}
现在,正如你所说,我指定了Calender,我的日期属于到GMT-06:00'时区,并设置我的日期
Now, as you say, I am specify to Calender that my date is belong to 'GMT-06:00' timezone and set my date,
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-6"));
cal.setTime(dt);
现在,我要告诉日历我想要在GMT中的日期
Now, I am telling calender that i want date in 'GMT'
cal.setTimeZone (TimeZone.getTimeZone("GMT"));
System.out.println(cal.getTime());
System.out.println(cal.getTime());
OutPut:
Fri Mar 15 03:01:53 CDT 2013
请知道我是否出错。
推荐答案
您需要两个格式对象,一个用于解析,另一个用于打印,因为您使用两个不同的时区,请参见此处:
You need TWO format objects, one for parsing and another one for printing because you use two different timezones, see here:
// h instead of H because of AM/PM-format
DateFormat parseFormat = new SimpleDateFormat("M/dd/yyyy hh:mm:ss aaa XXX");
Date dt = null;
try {
dt = parseFormat.parse("3/15/2013 3:01:53 PM -06:00");
}catch (ParseException e) {
e.printStackTrace();
}
DateFormat printFormat = new SimpleDateFormat("M/dd/yyyy hh:mm:ss aaa XXX");
printFormat.setTimeZone(TimeZone.getTimeZone("GMT-05"));
String newDateString = printFormat.format(dt);
System.out.println(newDateString);
输出:3/15/2013 04:01:53 PM -05:00
Output: 3/15/2013 04:01:53 PM -05:00
如果你想要HH:mm:ss(24小时格式),那么你只需要替换
If you want HH:mm:ss (24-hour-format) then you just replace
hh:mm:ss aaa
by
HH:mm:ss
对其他问题的评论:
A java.util.Date
没有内部时区,总是指定UTC。您不能在此对象内更改它。对于格式化的字符串可能进行时区转换,但是如我的代码示例(您想转换为GMT-05区域)所示。
A java.util.Date
has no internal timezone and always refers to UTC by spec. You cannot change it inside this object. A timezone conversion is possible for the formatted string, however as demonstrated in my code example (you wanted to convert to zone GMT-05).
然后,该问题切换到以ISO格式打印 Date
-object的新要求,使用UTC时区(符号Z)。这可以通过用yyyy-MM-dd'T'HH:mm:ssXXX替换模式进行格式化,并将printFormat的时区显式设置为GMT + 00。您应该澄清您真正想要的格式化输出。
The question then switches to the new requirement to print the Date
-object in ISO-format using UTC timezone (symbol Z). This can be done in formatting by replacing the pattern with "yyyy-MM-dd'T'HH:mm:ssXXX" and explicitly setting the timezone of printFormat to GMT+00. You should clarify what you really want as formatted output.
关于 java.util.GregorianCalendar
:设置时区为正在以程序方式更改日历对象,因此影响方法调用,如 calendar.get(Calendar.HOUR_OF_DAY)
。这与格式化无关!
About java.util.GregorianCalendar
: Setting the timezone here is changing the calendar-object in a programmatical way, so it affects method calls like calendar.get(Calendar.HOUR_OF_DAY)
. This has nothing to do with formatting however!
这篇关于使用时区java将字符串转换为适当的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!