我有一个场景,需要将日期时间字符串从时区 A(UTC) 转换为时区 B(EST)。
此处发生此转换的 JVM 是时区 C(HKT) 日期对象。
所以代码块如下:
String dateString="20141114213000";
dateString += " UTC";
String formatString ="yyyyMMddHHmmss";
SimpleDateFormat sdf = new SimpleDateFormat(formatString + " z");
Date localDate = sdf.parse(dateString);
System.out.println("Local Date::"+localDate); // Local Date::Sat Nov 15 05:30:00 HKT 2014
Calendar localCal = Calendar.getInstance();
localCal.setTime(localDate);
TimeZone estTimeZone = TimeZone.getTimeZone("America/New_York");
localCal.setTimeZone(estTimeZone);
sdf.setTimeZone(estTimeZone);
System.out.println(sdf.format(localCal.getTime()));//20141114163000 EST
System.out.println(localCal.getTime());//Sat Nov 15 05:30:00 HKT 2014
我期望的输出是最后一个语句的“Fri Nov 14 16:30:00 EST 2014”,即,可用的最终日期对象应该在 EST强>。
如果有人有这方面的信息,请告知。
<小时/>更新:
为了明确我的要求,输出应该仅在日期对象中。
我打印的示例代码和输出仅用于更清晰的解释。
因此,基本上,字符串“20141114213000”(即 UTC 日期字符串)需要转换为 EST 日期对象,而 JVM 则在此转换发生在HKT。
最佳答案
Java 的日期和时间类似乎存在值得注意的问题。
让我们使用流行的Joda-Time - Java date and time API 。 只需下载最新的稳定版本并将 jar 文件添加到项目的构建路径中即可。
我逐行注释掉了您的代码并重写了 Joda Time 替代方案。这样您就可以了解我如何将现有代码转换为 Joda Time API。
import java.text.ParseException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class JodaTimeTransition {
public static void main(String[] args) throws ParseException {
String dateString="20141114213000";
dateString += " UTC";
/*new*/
String formatString = "yyyyMMddHHmmss z";
// String formatString ="yyyyMMddHHmmss";
/*new*/
DateTimeFormatter dtf = DateTimeFormat.forPattern(formatString);
// SimpleDateFormat sdf = new SimpleDateFormat(formatString + " z");
/* new - Create localDate using JodaTime DateTime class */
DateTime localDate = DateTime.parse(dateString, dtf);
// Date localDate = sdf.parse(dateString);
/* new - convert time to MST, since it is currently UTC*/
localDate = localDate.toDateTime(DateTimeZone.forID("America/Denver"));
/* new - print out <local date> using specified format.*/
System.out.println("Local Date::" + localDate.toString("EEE MMM dd HH:mm:ss z yyyy"));
/* Where did you get the local date mentioned at comments of line below? */
// System.out.println("Local Date::"+localDate); // Local Date::Sat Nov 15 05:30:00 HKT 2014
/* new - Get reference to current date/time as <localCal>
* (This step can be omitted, and the previous localDate variable can be used)
*/
DateTime localCal = DateTime.now();
// Calendar localCal = Calendar.getInstance();
/* new - Set <localCal> to <localDate> */
localCal = localDate;
// localCal.setTime(localDate);
/* new - Create new EST time zone*/
DateTimeZone estTimeZone= DateTimeZone.forID("America/New_York");
// TimeZone estTimeZone = TimeZone.getTimeZone("America/New_York");
/* new - set <localCal> time zone from MST to EST */
localCal = localCal.toDateTime(estTimeZone);
// localCal.setTimeZone(estTimeZone);
// sdf.setTimeZone(estTimeZone);
/* new - print <localCal> as new EST time zone */
System.out.println(localCal.toString("yyyyMMddHHmmss z"));
// System.out.println(sdf.format(localCal.getTime()));//20141114163000 EST
/* new - print in desired format: Fri Nov 14 16:30:00 EST 2014 */
System.out.println(localCal.toString("EEE MMM dd HH:mm:ss z yyyy"));
// System.out.println(localCal.getTime());//Sat Nov 15 05:30:00 HKT 2014
}
}
关于java - 将日期时间字符串从一个时区 A 转换为时区 B,其中 jvm 在时区 C 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26979786/