首页 > 代码库 > Java 时区之间时间转换

Java 时区之间时间转换

 1 SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 2         System.out.println("foo:" + foo.format(new Date())); 3          4         Calendar gc = GregorianCalendar.getInstance(); 5         System.out.println("gc.getTime():" + gc.getTime()); 6         System.out.println("gc.getTimeInMillis():" + new Date(gc.getTimeInMillis())); 7          8         // 当前系统默认时区的时间: 9         Calendar calendar = new GregorianCalendar();10         System.out.print("时区:" + calendar.getTimeZone().getID() + "  ");11         System.out.println("时间:" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));12         // 美国洛杉矶时区13         TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");14         // 时区转换15         calendar.setTimeZone(tz);16         System.out.print("时区:" + calendar.getTimeZone().getID() + "  ");17         System.out.println("时间:" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));18         Date time = new Date();19         20         // 1、取得本地时间:21         java.util.Calendar cal = java.util.Calendar.getInstance();22         23         // 2、取得时间偏移量:24         int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);25         26         // 3、取得夏令时差:27         int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);28         29         // 4、从本地时间里扣除这些差量,即可以取得UTC时间:30         cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));31         32         // 之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。33         System.out.println("UTC:" + new Date(cal.getTimeInMillis()));34         35         Calendar calendar1 = Calendar.getInstance();36         TimeZone tztz = TimeZone.getTimeZone("GMT");37         calendar1.setTimeZone(tztz);38         System.out.println(calendar.getTime());39         System.out.println(calendar.getTimeInMillis());40         41         // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss‘Z‘");42         // df.setTimeZone(TimeZone.getTimeZone("UTC"));43         // System.out.println(df.parse("2014-08-23T09:20:05Z").toString());44         45         SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSS‘Z‘");46         Date t = new Date();47         System.out.println(df1.format(t));48         System.out.println(df1.format(df1.parse("2014-08-27T18:02:59.676Z")) + "***********");49         df1.setTimeZone(TimeZone.getTimeZone("UTC"));50         System.out.println(df1.format(t));51         System.out.println("-----------");52         System.out.println(df1.format(df1.parse("2014-08-27T18:02:59.676Z")) + "***********");53         System.out.println("2014-08-27T18:02:59.676Z");

 

Java 时区之间时间转换