国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java高級日期概念二

2019-11-18 12:10:58
字體:
來源:轉載
供稿:網友

  時區
  
  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.  
  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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙井市| 岳池县| 玉屏| 开远市| 阳西县| 宁南县| 吕梁市| 贵阳市| 紫阳县| 塔河县| 新泰市| 涿鹿县| 凤阳县| 奈曼旗| 那曲县| 岳阳县| 定远县| 红河县| 右玉县| 鄯善县| 河西区| 明星| 十堰市| 桦甸市| 开阳县| 昌宁县| 永昌县| 合作市| 黎城县| 凌源市| 大庆市| 淮南市| 抚顺市| 新平| 盈江县| 浦东新区| 襄樊市| 巨野县| 织金县| 冷水江市| 体育|