一 概念介绍
GMT 就是格林威治标准时间的英文缩写(Greenwich Mean Time 格林尼治标准时间).
格林威治是伦敦泰晤士河南岸的一个地方,由于从19世纪开始,因为世界各国来往频繁,而欧洲大陆、美洲大陆和亚洲大陆都有各自的时区,所以为免混乱,各国的代表就在1884 年在美国华盛顿召开了国际大会,通过协议选出伦敦的格林威治,作为全球时间的中心点,格林威治标准时间因而诞生。所以有GMT功能的腕表就是说腕表拥有其中的小时表盘可以显示GMT时间。
格林尼治标准时间,现在也叫UTC
二 java中的相关类介绍
<1>java.util.Locale
Locale类的一个实例通常包含国家和语言信息。其中的每一个部分都是由基于国际标准化组织(ISO)制定的国家代码ISO-3166和语言代码ISO-639的两字符的字符串构成的
- // Get the current system date and time.
- Date date = new Date();
- // Get a France locale using a Locale constant.
- Locale localeZN = Locale.CHINESE;
- // Create an English/US locale using the constructor.
- Locale localeEN = new Locale("en", "US" );
- // Get a date time formatter for display in France.
- DateFormat fullDateFormatFR =
- DateFormat.getDateTimeInstance(
- DateFormat.FULL,
- DateFormat.FULL,
- localeZN);
- // Get a date time formatter for display in the U.S.
- DateFormat fullDateFormatEN =
- DateFormat.getDateTimeInstance(
- DateFormat.FULL,
- DateFormat.FULL,
- localeEN);
- System.out.println("Locale: " + localeZN.getDisplayName());
- System.out.println(fullDateFormatFR.format(date));
- System.out.println("Locale: " + localeEN.getDisplayName());
- System.out.println(fullDateFormatEN.format(date));
<2>java.util.TimeZone
TimeZone类的实例包含了一个与格林威治标准时间(GMT)相比较得出的以微秒为单位的时区偏移量,而且它还处理夏令时。getDefault从系统时钟返回时区数据
- /*TimeZone对象给我们的是原始的偏移量,也就是与GMT相差的微秒数,而且还会告诉我们这个时区是否使用夏令时*/
- TimeZone timeZoneFL = TimeZone.getDefault();
- System.out.println("\n" + timeZoneFL.getDisplayName());
- System.out.println("RawOffset: " + timeZoneFL.getRawOffset());
- System.out.println("Uses daylight saving: " + timeZoneFL.useDaylightTime());
- TimeZone timeZoneLondon = TimeZone.getTimeZone("America/New_York");
- System.out.println("\n" + timeZoneLondon.getDisplayName());
- System.out.println("RawOffset: " + timeZoneLondon.getRawOffset());
- System.out.println("Uses daylight saving: " + timeZoneLondon.useDaylightTime());
- TimeZone timeZoneParis = TimeZone.getTimeZone("Europe/Paris");
- System.out.println("\n" + timeZoneParis.getDisplayName());
- System.out.println("RawOffset: " + timeZoneParis.getRawOffset());
- System.out.println("Uses daylight saving: " + timeZoneParis.useDaylightTime());
混合起来能够支持国际化的需求:
- Locale localeZN = Locale.CHINESE;
- Locale localeEN = Locale.US;
- TimeZone timeZoneHZ = TimeZone.getDefault();
- TimeZone timeZoneUS = TimeZone.getTimeZone("America/New_York");
- DateFormat dateFormatter = DateFormat.getDateTimeInstance(
- DateFormat.FULL,
- DateFormat.FULL,
- localeZN);
- DateFormat dateFormatterParis = DateFormat.getDateTimeInstance(
- DateFormat.FULL,
- DateFormat.FULL,
- localeEN);
- Date curDate = new Date();
- System.out.println("Display for China HZ.");
- System.out.println(timeZoneHZ.getDisplayName(localeZN));
- dateFormatter.setTimeZone(timeZoneHZ);
- System.out.println(dateFormatter.format(curDate));
- dateFormatter.setTimeZone(timeZoneUS);
- System.out.println(timeZoneUS.getDisplayName(localeZN));
- System.out.println(dateFormatter.format(curDate));
- System.out.println("\nDisplay for US office.");
- System.out.println(timeZoneHZ.getDisplayName(localeEN));
- dateFormatterParis.setTimeZone(timeZoneHZ);
- System.out.println(dateFormatterParis.format(curDate));
- dateFormatterParis.setTimeZone(timeZoneUS);
- System.out.println(timeZoneUS.getDisplayName(localeEN));
- System.out.println(dateFormatterParis.format(curDate));
<3>System.getCurrentTime()
返回当前系统的时间与UTC 1970:01:01 00:00:00时间之间的毫秒差距
转载于:https://blog.51cto.com/drizzlewalk/484195