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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM