服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java高级日期概念二


  时区
  
  timezone类,即java.util.timezone类的实例包含了一个与格林威治标准时间(gmt)相比较得出的以微秒为单位的时区偏移量,而且它还处理夏令时
  
  。要获得一个所有支持的进区的列表,你可以使用方法timezone.getavailableids,它将返回一个包含了所有进区id的字符串数组。要知道关于timezone类的更多细节,可以参看sun公司的web站点。
  
  
  
  为了演示这个概念,我们将创建三个时区对象。第一个对象将使用getdefault从系统时钟返回时区数据;第二个和第三个对象将传入一个时区字符串id。见表c中的代码。
  
  
  
  表 c
  
  
  
  
  
  import java.util.timezone;
  
  import java.util.date;
  
  import java.text.dateformat;
  
  import java.util.locale;
  
  
  
  public class dateexample8 {
  
  
  
  public static void main(string[] args) {
  
  // get the system time zone.
  
  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("europe/london");
  
  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());
  
  }
  
  }
  
  
  
  
  
  
  
  其输出如下:
  
  
  
  eastern standard time
  
  rawoffset: -18000000
  
  uses daylight saving: true
  
  gmt+00:00
  
  rawoffset: 0
  
  uses daylight saving: true
  
  
  
  central european standard time
  
  rawoffset: 3600000
  
  uses daylight saving: true
  
  
  
  正如你所看见的,timezone对象给我们的是原始的偏移量,也就是与gmt相差的微秒数,而且还会告诉我们这个时区是否使用夏令时。有个这个信息,我们就能够继续将时区对象和日期格式化器结合在一起在其它的时区和其它的语言显示时间了。
  
  
  
  国际化的时期显示了时区转换
  
  让我们来看一个结合了国际化显示,时区和日期格式化的例子。表d为一个在迈阿密和巴黎拥有办公室的公司显示了当前的完整日期和时间。对于迈阿密的办公室,我们将在每个办公室里用英语显示完整的日期和时间。对于巴黎的办公室,我们将用法语显示完整的当前日期和时间。
  
  
  
  表 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);
  
  dateformat dateformatterparis = dateformat.getdatetimeinstance(
  
  dateformat.full,
  
  dateformat.full,
  
  localefrance);
  
  
  
  date curdate = new date();
  
  
  
  system.out.println("display for miami office.");
  
  // print the miami time zone display name in english
  
  system.out.println(timezonemiami.getdisplayname(localeen));
  
  // set the time zone of the dateformatter to miami time zone.
  
  dateformatter.settimezone(timezonemiami);
  
  // print the formatted date.
  
  system.out.println(dateformatter.format(curdate));
  
  
  
  // set the time zone of the date formatter to paris time zone.
  
  dateformatter.settimezone(timezoneparis);
  
  // print the paris time zone display name in english.
  
  system.out.println(timezoneparis.getdisplayname(localeen));
  
  // print the paris time in english.
  
  system.out.println(dateformatter.format(curdate));
  
  
  
  system.out.println("/ndisplay for paris office.");
  
  // print the miami time zone display name in french
  
  system.out.println(timezonemiami.getdisplayname(localefrance));
  
  // set the timezone of the
  
  // dateformatterparis to miami time zone.
  
  dateformatterparis.settimezone(timezonemiami);
  
  // print the formatted date in french.
  
  ?system.out.println(dateformatterparis.format(curdate));
  
  
  
  // set the timezone of the date formatter to paris time zone.
  
  dateformatterparis.settimezone(timezoneparis);
  
  // print the paris time zone display name in french.
  
  system.out.println(timezoneparis.getdisplayname(localefrance));
  
  // print the paris time in french.
  
  system.out.println(dateformatterparis.format(curdate));
  
  }
  
  }
  
  
  
  
  
  
  
  这个例子的输出是:
  
  
  
  display for miami office.
  
  eastern standard time
  
  friday, october 5, 2001 10:28:02 pm edt
  
  central european standard time
  
  saturday, october 6, 2001 4:28:02 am cest
  
  display for paris office.
  
  gmt-05:00
  
  vendredi 5 octobre 2001 22 h 28 gmt-04:00
  
  gmt+01:00
  
  samedi 6 octobre 2001 04 h 28 gmt+02:00

扫描关注微信公众号