在java中从一个时区转换为另一个时区

时间:2013-11-19 19:47:55

标签: java

我有一个UTC格式的UTC时区日期时间(“yyyy-MM-dd'T'HH:mm:ss'Z')。是否有一种简单的方法(不使用joda)将其转换为另一个时区( PDT,EST等)在某个/或标准(到那个时区)的日期格式?

由于

3 个答案:

答案 0 :(得分:0)

你可以考虑使用date4j而不是joda(虽然我更喜欢joda,除非有明确的理由不使用它)。

DateTime dt = DateTime.now(someTimeZone);
dt.changeTimeZone(fromOneTimeZone, toAnotherTimeZone);

也许是这样的:

DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss Z");
  formatter.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
  System.out.println(formatter.format(date));

  formatter.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
  System.out.println(formatter.format(instance2.getTime()))

答案 1 :(得分:0)

创建一个日历对象,并根据本地而不是德国使用EST和PDT设置时间

Calendar localTime = Calendar.getInstance();
    localTime.set(Calendar.HOUR, 17);
    localTime.set(Calendar.MINUTE, 15);
    localTime.set(Calendar.SECOND, 20);

int hour = localTime.get(Calendar.HOUR);
int minute = localTime.get(Calendar.MINUTE);
int second = localTime.get(Calendar.SECOND);


// Print the local time
System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);


// Create a calendar object for representing a Germany time zone. Then we
// wet the time of the calendar with the value of the local time

Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
germanyTime.setTimeInMillis(localTime.getTimeInMillis());
hour = germanyTime.get(Calendar.HOUR);
minute = germanyTime.get(Calendar.MINUTE);
second = germanyTime.get(Calendar.SECOND);

    // Print the local time in Germany time zone
    System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);

答案 2 :(得分:0)

public String formatInTimezone(String utcDateString, String intendedTimeZone) {
    SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date theDate = utcFormat.parse(utcDateString);

    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z");
    outputFormat.setTimeZone(TimeZone.getTimeZone(intendedTimeZone));
    return outputFormat.format(theDate);
}