-1

我需要显示服务器的时区,例如 GMT-03:00,我如何在 Java 中做到这一点?

public static void main(String[] args) {
    logger.error(Calendar.getInstance().getTimeZone().getTimeZone("America/Sao_Paulo"));        
}
4

2 回答 2

1

我推荐Joda-Time库来处理 Java 中的时间。它比默认实现要好得多。

DateTimeZone zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("ZZ");
System.out.println("GMT "+dateTimeFormatter.withZone(zone).print(0) +" for "+zone.toString());

将打印GMT -03:00 for America/Sao_Paulo

于 2013-11-11T19:05:51.857 回答
0

您可以使用 aSimpleDateFormat来获得类似的结果:

SimpleDateFormat format = new SimpleDateFormat("z XXX");
System.out.println(format.format(calendar.getTime()));

此链接中提供了 javadoc 。

于 2013-11-11T19:23:17.567 回答