一 概念介绍

 

 

GMT 就是格林威治标准时间的英文缩写(Greenwich Mean Time 格林尼治标准时间). 

格林威治是伦敦泰晤士河南岸的一个地方,由于从19世纪开始,因为世界各国来往频繁,而欧洲大陆、美洲大陆和亚洲大陆都有各自的时区,所以为免混乱,各国的代表就在1884 年在美国华盛顿召开了国际大会,通过协议选出伦敦的格林威治,作为全球时间的中心点,格林威治标准时间因而诞生。所以有GMT功能的腕表就是说腕表拥有其中的小时表盘可以显示GMT时间。

格林尼治标准时间,现在也叫UTC

二 java中的相关类介绍

<1>java.util.Locale

 

Locale类的一个实例通常包含国家和语言信息。其中的每一个部分都是由基于国际标准化组织(ISO)制定的国家代码ISO-3166和语言代码ISO-639的两字符的字符串构成的

 
  
  1. // Get the current system date and time. 
  2. Date date = new Date(); 
  3.  
  4. // Get a France locale using a Locale constant. 
  5. Locale localeZN = Locale.CHINESE; 
  6.  
  7. // Create an English/US locale using the constructor. 
  8. Locale localeEN = new Locale("en""US" ); 
  9.  
  10. // Get a date time formatter for display in France. 
  11. DateFormat fullDateFormatFR = 
  12. DateFormat.getDateTimeInstance( 
  13. DateFormat.FULL, 
  14. DateFormat.FULL, 
  15. localeZN); 
  16.  
  17. // Get a date time formatter for display in the U.S. 
  18. DateFormat fullDateFormatEN = 
  19. DateFormat.getDateTimeInstance( 
  20. DateFormat.FULL, 
  21. DateFormat.FULL, 
  22. localeEN); 
  23.  
  24. System.out.println("Locale: " + localeZN.getDisplayName()); 
  25. System.out.println(fullDateFormatFR.format(date)); 
  26. System.out.println("Locale: " + localeEN.getDisplayName()); 
  27. System.out.println(fullDateFormatEN.format(date)); 

<2>java.util.TimeZone

TimeZone类的实例包含了一个与格林威治标准时间(GMT)相比较得出的以微秒为单位的时区偏移量,而且它还处理夏令时。getDefault从系统时钟返回时区数据

 
  
  1. /*TimeZone对象给我们的是原始的偏移量,也就是与GMT相差的微秒数,而且还会告诉我们这个时区是否使用夏令时*/ 
  2.          
  3.         TimeZone timeZoneFL = TimeZone.getDefault(); 
  4.         System.out.println("\n" + timeZoneFL.getDisplayName()); 
  5.         System.out.println("RawOffset: " + timeZoneFL.getRawOffset()); 
  6.         System.out.println("Uses daylight saving: " + timeZoneFL.useDaylightTime()); 
  7.  
  8.         TimeZone timeZoneLondon = TimeZone.getTimeZone("America/New_York"); 
  9.         System.out.println("\n" + timeZoneLondon.getDisplayName()); 
  10.         System.out.println("RawOffset: " + timeZoneLondon.getRawOffset()); 
  11.         System.out.println("Uses daylight saving: " + timeZoneLondon.useDaylightTime()); 
  12.  
  13.         TimeZone timeZoneParis = TimeZone.getTimeZone("Europe/Paris"); 
  14.         System.out.println("\n" + timeZoneParis.getDisplayName()); 
  15.         System.out.println("RawOffset: " + timeZoneParis.getRawOffset()); 
  16.         System.out.println("Uses daylight saving: " + timeZoneParis.useDaylightTime()); 

混合起来能够支持国际化的需求:

 
  
  1. Locale localeZN = Locale.CHINESE; 
  2.         Locale localeEN = Locale.US; 
  3.  
  4.         TimeZone timeZoneHZ = TimeZone.getDefault(); 
  5.         TimeZone timeZoneUS = TimeZone.getTimeZone("America/New_York"); 
  6.  
  7.          
  8.         DateFormat dateFormatter = DateFormat.getDateTimeInstance( 
  9.         DateFormat.FULL, 
  10.         DateFormat.FULL, 
  11.         localeZN); 
  12.         DateFormat dateFormatterParis = DateFormat.getDateTimeInstance( 
  13.         DateFormat.FULL, 
  14.         DateFormat.FULL, 
  15.         localeEN); 
  16.  
  17.         Date curDate = new Date(); 
  18.  
  19.         System.out.println("Display for China HZ."); 
  20.         System.out.println(timeZoneHZ.getDisplayName(localeZN)); 
  21.         dateFormatter.setTimeZone(timeZoneHZ); 
  22.         System.out.println(dateFormatter.format(curDate)); 
  23.  
  24.         dateFormatter.setTimeZone(timeZoneUS); 
  25.         System.out.println(timeZoneUS.getDisplayName(localeZN)); 
  26.         System.out.println(dateFormatter.format(curDate)); 
  27.  
  28.         System.out.println("\nDisplay for US office."); 
  29.         System.out.println(timeZoneHZ.getDisplayName(localeEN)); 
  30.         dateFormatterParis.setTimeZone(timeZoneHZ); 
  31.         System.out.println(dateFormatterParis.format(curDate)); 
  32.  
  33.         dateFormatterParis.setTimeZone(timeZoneUS); 
  34.         System.out.println(timeZoneUS.getDisplayName(localeEN)); 
  35.         System.out.println(dateFormatterParis.format(curDate)); 

<3>System.getCurrentTime()

返回当前系统的时间与UTC 1970:01:01 00:00:00时间之间的毫秒差距