站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题Java高级日期概念二

Java高级日期概念二

摘要: Java高级日期概念二
<table cellSpacing=0 cellPadding=0 border=0 class="zh114" align="right"> <tr> <td > </td> </tr> </table>
  时区
  
  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());
  
  
  
  燭imeZone 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.
  
  燬ystem.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
  
  

<table width="96%"> <tr> <td background="http:///images/dian.gif" height="3"></td> </tr> </table>

↑返回目录
前一篇: Java高级日期概念三
后一篇: md5算法的java源代码