java时区-DateFormat和TimeZone关…

来源:互联网 发布:linux设定文件夹权限 编辑:程序博客网 时间:2024/06/11 17:49
TimeZone对象给我们的是原始的偏移量,也就是与GMT相差的微秒数,Java的Date对象里面存储着当前时刻到1970年1月1日0:00所经过的毫秒数,它与时区和地域没有关系(其实可以认为是GMT时间吧).而且还会告诉我们这个时区是否使用夏令时。有个这个信息,我们就能够继续将时区对象和日期格式化器结合在一起在其它的时区和其它的语言显示时间了。
国际化的时期显示了时区转换
让我们来看一个结合了国际化显示,时区和日期格式化的例子。表D为一个在迈阿密和巴黎拥有办公室的公司显示了当前的完整日期和时间。对于迈阿密的办公室,我们将在每个办公室里用英语显示完整的日期和时间。对于巴黎的办公室,我们将用法语显示完整的当前日期和时间。

import java.util.TimeZone;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
public class DateExample9 {
public static void main(String[] args) {
   Locale localeEN =Locale.US;
   Locale localeFrance =Locale.FRANCE;
    TimeZone timeZoneMiami =TimeZone.getDefault();
   TimeZone timeZoneParis =  TimeZone.getTimeZone("Europe/Paris");
   DateFormat dateFormatter= DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL,
localeEN);
   DateFormatdateFormatterParis = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL,
localeFrance);
   Date curDate = newDate(); //以GMT格式记录当前计算机时间
  System.out.println("Display for Miamioffice.");
// 显示"迈阿密时区"的英文格式显示名
  System.out.println(timeZoneMiami.getDisplayName(localeEN));
// 配置美国本地日期格式化器的时区为"迈阿密时区(本地时区-美国)"
  dateFormatter.setTimeZone(timeZoneMiami);
// 输出格式化的日期字符, 这时把当前GMT时间输出为"迈阿密所在时区"的时间
   System.out.println(dateFormatter.format(curDate));
// 配置日期格式化器Format的时区为"巴黎的时区"
  dateFormatter.setTimeZone(timeZoneParis);
// 输出"巴黎所在时区"的英文显示名
   System.out.println(timeZoneParis.getDisplayName(localeEN));
// 输出当前GMT时间表现为"巴黎时区"下的时间
   System.out.println(dateFormatter.format(curDate));
  System.out.println("\nDisplay for Parisoffice.");
// 输出"迈阿密所在时区"的法文显示名
   System.out.println(timeZoneMiami.getDisplayName(localeFrance));
// Set the timezone of the
// 配置巴黎本地日期格式化器的时区为"迈阿密所在时区"
   dateFormatterParis.setTimeZone(timeZoneMiami);
// 把当前GMT时间用法文格式输出为"迈阿密所在时区"的时间
   system.out.println(dateFormatterParis.format(curDate));
// 配置巴黎本地日期格式化器的时区为"巴黎所在的时区"
   dateFormatterParis.setTimeZone(timeZoneParis);
// 输出"巴黎所在时区"的法文格式显示名
   System.out.println(timeZoneParis.getDisplayName(localeFrance));
// 
//当前GMT时间用法文格式输出为"巴黎所在时区"的时间  System.out.println(dateFormatterParis.format(curDate));
}
}

输出结果为:
Display for Miami office.
Eastern Standard Time
Friday, October 5, 2001 10:28:02 PMEDT
Central European StandardTime
Saturday, October 6, 2001 4:28:02 AMCEST
Display for Paris office.
GMT-05:00
vendredi 5 octobre 2001 22 h 28GMT-04:00
GMT+01:00
samedi 6 octobre 2001 04 h 28GMT+02:00

由上面例子可以知道:

   1. 计算机内部记录的时间(Date date = newDate()), 为格林威治标准时(GMT). 即java.util.Date代表一个时间点,其值为距公元1970年1月1日00:00:00的毫秒数。所以它可以认为是没有时区和Locale概念的。

   2. 日期格式化类DateFormat,对于不同地区的配置一般有两个点, 一个是Locale , 一个是TimeZone

      前者(Locale)使DateFormat按所配置的地区特性来输出文字(例如中国,美国,法国不同地区对日期的表示格式不一样,中国可能是2001年10月5日)

     后者(TimeZone)让DateFormat知道怎么去转换,去调整时间偏移度,从而得到符合配置的时区的时间.

      (即假设取得当前时间(假设当前时区为GMT+0,即与newDate()最后转换的时间毫秒数一致)为2:00,那么如果你配置DateFormat.setTimeZome("GMT+8"), 即北京时间的时区,那么这时候格式化输出的就是10:00了,因为系统对原始毫秒数进行了时间偏移调整(调到你设置的时区),即加多8小时,之后再格式化输出日期的字符串形式)

   3. GMT与UTC的时区是一样的,都是以伦敦时间为基准.而GMT+8时区就是北京时间所在时区.同一时刻的时间比GMT快8小时.



本文内容来源于网络

0 0