Print timezone of server

大憨熊 提交于 2019-12-13 08:24:09

问题


I need show the timezone of server, example GMT-03:00, how I can make this in Java?

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

回答1:


I recommend Joda-Time library for work with time in Java. It's much better than default realization.

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());

will print GMT -03:00 for America/Sao_Paulo




回答2:


You can use a SimpleDateFormat to get a similar result:

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

The javadoc is available in this link.



来源:https://stackoverflow.com/questions/19913414/print-timezone-of-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!