++ String t = "2011-10-20 10:20:21 +0800"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss Z"); try { Calendar c = Calendar.getInstance(); c.setTime(sdf.parse(t)); TimeZone zone = c.getTimeZone(); c.getTimeZone().setRawOffset(((zone.getRawOffset() / 3600) + 1000) * 3600); System.err.println(sdf.format(c.getTime())); } catch(ParseException e) { e.printStackTrace(); } 为啥这样不行呢。
"GMT+10" 和 "GMT+0010" 分别意味着比 GMT 提前 10 小时和 10 分钟。 格式是与区域无关的,并且数字必须取自 Unicode 标准的 Basic Latin 块。 没有夏令时转换安排可以用自定义时区 ID 指定。如果指定的字符串与语法不匹配,就使用 "GMT"。
格林威治时间是全球统一的。计算机表示时,一般是一个长整型数据。 这个数据,不会因为时区不同而变化。 所以,楼主,可以考虑,在时间的显示方面下功夫。 比如: 我们让时间以东九区的形式显示(而不是默认的东八区形式) public static void main(String[] args) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); Date time = new Date(); System.out.println(format.format(time)); format.setTimeZone(TimeZone.getTimeZone("GMT+9:00")); System.out.println(format.format(time)); }
String time1 = "2011-10-20 10:20:21 +0800"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); Date time2 = format.parse(time1);
TimeZone
http://hi.baidu.com/seoyingjie/blog/item/c45fe020e15e0f3d8744f9e9.html
String time1 = "2011-10-20 10:20:21 +0800"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date time2 = format.parse(time1);
format.applyPattern("yyyy-MM-dd HH:mm:ss Z");
format.setTimeZone(TimeZone.getTimeZone("GMT+9"));
System.out.println(format.format(time2));
++
String t = "2011-10-20 10:20:21 +0800";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss Z"); try {
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(t));
TimeZone zone = c.getTimeZone();
c.getTimeZone().setRawOffset(((zone.getRawOffset() / 3600) + 1000) * 3600);
System.err.println(sdf.format(c.getTime()));
}
catch(ParseException e) {
e.printStackTrace();
}
为啥这样不行呢。
格式是与区域无关的,并且数字必须取自 Unicode 标准的 Basic Latin 块。
没有夏令时转换安排可以用自定义时区 ID 指定。如果指定的字符串与语法不匹配,就使用 "GMT"。
这个数据,不会因为时区不同而变化。
所以,楼主,可以考虑,在时间的显示方面下功夫。
比如:
我们让时间以东九区的形式显示(而不是默认的东八区形式)
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date time = new Date();
System.out.println(format.format(time));
format.setTimeZone(TimeZone.getTimeZone("GMT+9:00"));
System.out.println(format.format(time));
}
Date time2 = format.parse(time1);
format.applyPattern("yyyy-MM-dd HH:mm:ss Z");
format.setTimeZone(TimeZone.getTimeZone("GMT+9"));
System.out.println(format.format(time2));