将日期字符串(EST)转换为Java日期(UTC) [英] Convert date string (EST) to Java Date (UTC)
问题描述
我需要有关此java方法的一些建议。此方法的目的是采用表示日期的字符串-该字符串是从EST时区中的日期创建的-并将其转换为UTC时区中的java Date对象。
I need some advice on this java method. The intent of this method is to take a string that represents a date - this string was created from a date in the EST time zone - and convert it to a java Date object in the UTC time zone.
private Date buildValidationDate(String dateString) throws ParseException {
System.out.println("dateString " + dateString);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyy hh:mm a");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dateFormat.setLenient(true);
Date dt = dateFormat.parse(dateString);
System.out.println("dt " + dt);
return dt;
}
我看到的问题是dt的值似乎已关闭。例如,如果dateString为 2012年10月16日下午12:06-我希望dt(以UTC为单位)的值类似于 2012年10月16日星期二下午4:06。相反,dt的值为 2012年10月16日星期二CDT 2012。这似乎不是正确的UTC时间。
the problem I'm seeing is the value of dt seems to be off. For instance, if dateString is '10/16/2012 12:06 PM' - I'm expecting the value of dt (in UTC) to be something like 'Tuesday, October 16, 2012 4:06 PM'. Instead the value of dt is 'Tue Oct 16 07:06:00 CDT 2012'. This does not seem to be the correct UTC time.
我感谢任何建议,如果这是一个简单的问题,我对此感到很麻烦,对此我感到抱歉Java日期。我不确定我的编码是否错误或我的方法是否有问题。谢谢
I appreciate any advice, I'm sorry if this seems to be an easy question I have a lot of trouble with Java dates. I'm not sure if I'm coding something incorrectly or if there is something wrong with my methodology. Thanks
推荐答案
您的日期已正确转换。它仅以默认时区格式作为 java.util.Date
打印值,而与时区无关。如果要进行时区特定处理,请使用 java.util.Calendar
。
Your date is getting converted right. Its just printing value in your default timezone format as java.util.Date
is timezone independent. If you want timezone specific handling, please use java.util.Calendar
.
这篇关于将日期字符串(EST)转换为Java日期(UTC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!