一个new Date()
是“旨在反映协调世界时(世界标准时间)。”用户在时钟上看到的内容取决于TimeZone
,这可以在用于呈现日期的DateFormat
中指定。下面的例子打印格林尼治标准时间相同的时刻,以及从纽约到柏林的一系列时区。
附录:我发现它的帮助想Date
为模型和DateFormat
作为模型的视图。
03-Feb-2013 18:01:42 GMT GMT 1359914502673
03-Feb-2013 13:01:42 EST America/New_York 1359914502673
03-Feb-2013 14:01:42 AST America/Aruba 1359914502673
03-Feb-2013 15:01:42 ART America/Buenos_Aires 1359914502673
03-Feb-2013 16:01:42 BRST America/Sao_Paulo 1359914502673
03-Feb-2013 17:01:42 AZOT Atlantic/Azores 1359914502673
03-Feb-2013 18:01:42 GMT Europe/London 1359914502673
03-Feb-2013 19:01:42 CET Europe/Berlin 1359914502673
包日期;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/** @see http://stackoverflow.com/a/14675418/230513 */
public class TestSDF {
private static final String s = "dd-MMM-yyyy HH:mm:ss zz";
private static final DateFormat f = new SimpleDateFormat(s);
public static void main(String[] args) {
Date date = new Date();
print("GMT", date);
print("America/New_York", date);
print("America/Aruba", date);
print("America/Buenos_Aires", date);
print("America/Sao_Paulo", date);
print("Atlantic/Azores", date);
print("Europe/London", date);
print("Europe/Berlin", date);
}
private static void print(String tz, Date d) {
f.setTimeZone(TimeZone.getTimeZone(tz));
System.out.println(f.format(d)
+ " " + tz
+ " " + d.getTime());
}
}
我粘贴了代码,但我没有得到正确的结果。 –
没有得到正确的结果?你得到了什么样的错误结果 – exexzian