问题
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