如果你的Java 程序向處在不同時(shí)區(qū)或者不同國(guó)家的用戶(hù)顯示時(shí)間和日期,那么你需要了解Java日期類(lèi)的一些更加高級(jí)的方面。在“使用Java Date和Calendar類(lèi)計(jì)算,定制和解析日期”的這篇文章里我們提供了對(duì)日期,日期數(shù)據(jù)的格式化,日期數(shù)據(jù)的解析和日期計(jì)算的一個(gè)概覽。對(duì)于這些概念的深入的理解對(duì)于討論更高級(jí)的諸如時(shí)區(qū),國(guó)際化標(biāo)準(zhǔn)格式和SQL日期數(shù)據(jù)等這些有關(guān)日期的問(wèn)題是關(guān)鍵的。
我們?cè)诒疚闹杏懻摰念?lèi)將包含java.text.DateFormat,以及java.util.TimeZone和java.util.Locate。我們還將討論如何使用一個(gè)java.util.Date的子類(lèi)java.sql.Date來(lái)從Oracle數(shù)據(jù)庫(kù)里提取和保存Java日期數(shù)據(jù)。
地區(qū)的問(wèn)題
在我們國(guó)際化我們的日期數(shù)據(jù)以前,我們需要進(jìn)一步的學(xué)習(xí)Locale類(lèi),也就是java.util.Locale。Locale類(lèi)的一個(gè)實(shí)例通常包含國(guó)家和語(yǔ)言信息。其中的每一個(gè)部分都是由基于國(guó)際標(biāo)準(zhǔn)化組織(ISO)制定的國(guó)家代碼ISO-3166和語(yǔ)言代碼ISO-639的兩字符的字符串構(gòu)成的。
讓我們來(lái)創(chuàng)建兩個(gè)Locale實(shí)例,其中一個(gè)對(duì)應(yīng)的是美國(guó)英語(yǔ)而另一個(gè)對(duì)應(yīng)的是法國(guó)法語(yǔ)。見(jiàn)表A。
表A
import java.util.Locale;
public class DateExample6 {
public static void main(String[] args) {
// Create a locale for the English language in the US.
Locale localeEN = new Locale("en", "US");
System.out.println("Display Name: " +
localeEN.getDisplayName());
System.out.println("Country: " + localeEN.getCountry());
System.out.println("Language: " + localeEN.getLanguage());
// Create a locale for the French language in France.
Locale localeFR = new Locale("fr", "FR");
System.out.println("\nDisplay Name: " +
localeFR.getDisplayName());
System.out.println("Country: " + localeFR.getCountry());
System.out.println("Language: " + localeFR.getLanguage());
// Display the English-US locale in French
System.out.println("\nen Display Name in French: " +
localeEN.getDisplayName(localeFR));
}
}
在這個(gè)例子中,我們用getDisplayName方法來(lái)顯示Locale的一個(gè)更易讀的文本。你還應(yīng)該注意到我們?cè)谧詈笠淮握{(diào)用getDisplayName的時(shí)候,我們?cè)趯?duì)English Locale對(duì)象調(diào)用getDisplayName的時(shí)候同時(shí)傳遞了French Locale對(duì)象。這允許我們選擇顯示Locale對(duì)象所用的語(yǔ)言,讓我們用英語(yǔ)顯示法語(yǔ)Locale對(duì)象的內(nèi)容。下面是這個(gè)例子的輸出:
Display Name: English (United States)
Country: US
Language: en
Display Name: French (France)
Country: FR
Language: fr
en Display Name in French: anglais (états-Unis)
多個(gè)地域的日期格式化
使用java.util.Locale和java.text.DateFormat類(lèi)我們就能夠格式化日期數(shù)據(jù)把它顯示給在另一個(gè)地域的用戶(hù),比方法國(guó)。表B中的例子為英語(yǔ)和法語(yǔ)各創(chuàng)建了一個(gè)完整的日期格式化器。
表 B
import java.util.Locale;
import java.util.Date;
import java.text.DateFormat;
public class DateExample7 {
public static void main(String[] args) {
// Get the current system date and time.
Date date = new Date();
// Get a France locale using a Locale constant.
Locale localeFR = Locale.FRANCE;
// 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,
localeFR);
// Get a date time formatter for display in the U.S.
DateFormat fullDateFormatEN =
DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL,
localeEN);
System.out.println("Locale: " + localeFR.getDisplayName());
System.out.println(fullDateFormatFR.format(date));
System.out.println("Locale: " + localeEN.getDisplayName());
System.out.println(fullDateFormatEN.format(date));
}
}
這個(gè)例子的輸出是:
Locale: French (France)
vendredi 5 octobre 2001 21 h 05 GMT-04:00
Locale: English (United States)
Friday, October 5, 2001 9:05:54 PM EDT
注意這個(gè)輸出包括了時(shí)區(qū)信息:GMT-04:00 和 PM EDT。這個(gè)時(shí)區(qū)是人系統(tǒng)的時(shí)區(qū)設(shè)置里捕獲的。你可以看見(jiàn),日期是以那個(gè)地區(qū)的用戶(hù)期望的格式顯示的。讓我們等一下來(lái)看看時(shí)區(qū)的概念
時(shí)區(qū)
TimeZone類(lèi),即java.util.TimeZone類(lèi)的實(shí)例包含了一個(gè)與格林威治標(biāo)準(zhǔn)時(shí)間(GMT)相比較得出的以微秒為單位的時(shí)區(qū)偏移量,而且它還處理夏令時(shí)
。要獲得一個(gè)所有支持的進(jìn)區(qū)的列表,你可以使用方法TimeZone.getAvailableIDs,它將返回一個(gè)包含了所有進(jìn)區(qū)ID的字符串?dāng)?shù)組。要知道關(guān)于TimeZone類(lèi)的更多細(xì)節(jié),可以參看Sun公司的Web站點(diǎn)。
為了演示這個(gè)概念,我們將創(chuàng)建三個(gè)時(shí)區(qū)對(duì)象。第一個(gè)對(duì)象將使用getDefault從系統(tǒng)時(shí)鐘返回時(shí)區(qū)數(shù)據(jù);第二個(gè)和第三個(gè)對(duì)象將傳入一個(gè)時(shí)區(qū)字符串ID。見(jiàn)表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
正如你所看見(jiàn)的,TimeZone對(duì)象給我們的是原始的偏移量,也就是與GMT相差的微秒數(shù),而且還會(huì)告訴我們這個(gè)時(shí)區(qū)是否使用夏令時(shí)。有個(gè)這個(gè)信息,我們就能夠繼續(xù)將時(shí)區(qū)對(duì)象和日期格式化器結(jié)合在一起在其它的時(shí)區(qū)和其它的語(yǔ)言顯示時(shí)間了。
國(guó)際化的時(shí)期顯示了時(shí)區(qū)轉(zhuǎn)換
讓我們來(lái)看一個(gè)結(jié)合了國(guó)際化顯示,時(shí)區(qū)和日期格式化的例子。表D為一個(gè)在邁阿密和巴黎擁有辦公室的公司顯示了當(dāng)前的完整日期和時(shí)間。對(duì)于邁阿密的辦公室,我們將在每個(gè)辦公室里用英語(yǔ)顯示完整的日期和時(shí)間。對(duì)于巴黎的辦公室,我們將用法語(yǔ)顯示完整的當(dāng)前日期和時(shí)間。
表 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));
}
}
這個(gè)例子的輸出是:
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
在一個(gè)SQL數(shù)據(jù)庫(kù)中保存和提取日期數(shù)據(jù)我們將要使用的下一個(gè)類(lèi)是java.sql.Date,它是java.util.Date的子類(lèi)但它使用了Java數(shù)據(jù)庫(kù)連接(JDBC)方法
。讓我們來(lái)看一個(gè)簡(jiǎn)單的只有一個(gè)表單--LAST_ACCESS的ORACLE數(shù)據(jù)庫(kù),它是用下面的SQL創(chuàng)建的:
create table LAST_ACCESS (
LAST_HIT date
);
這個(gè)表單只有一個(gè)記錄,用下面的插入語(yǔ)句創(chuàng)建:
insert into LAST_ACCESS values (Sysdate);
表E演示了如何修改和提取LAST_HIT數(shù)據(jù)庫(kù)域。
表 E
import java.sql.*;
import java.text.DateFormat;
import java.util.Date;
public class DateExample10 {
public static void main(String[] args) {
// Get a full date formatter.
DateFormat dateFormatter = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL);
// Get the system date and time.
java.util.Date utilDate = new Date();
// Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());
// Display the date before storing.
System.out.println(dateFormatter.format(date));
// Save the date to the database.
setLastHit(date);
// Get the date from the database.
Date dateFromDB = getLastHit();
// Display the date from the database.
System.out.println(dateFormatter.format(dateFromDB));
}
public static void setLastHit(java.sql.Date date) {
try {
// Load the class.
Class.forName("oracle.jdbc.driver.OracleDriver");
// Get a connection.
燙onnection connection = DriverManager.getConnection(
// Database URL
"jdbc:oracle:thin:@localhost:1521:buzz2",
"web_site", // Username
"web_site"); // Password
try {
/ Get a prepared statement fromthe connection
// specifying the update SQL.
PreparedStatement ps = connection.prepareStatement(
"update LAST_ACCESS set LAST_HIT=");
try {
/ set the date letting JDBC to the work of
// formatting the SQL appropriately.
ps.setDate(1, date);
// Execute the update statement.
int iRowsUpdated = ps.executeUpdate();
System.out.println("Rows updated: " + iRowsUpdated);
} finally {
ps.close();
}
} finally {
connection.close();
}
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
public static java.sql.Date getLastHit() {
java.sql.Date returnDate = null;
try {
// Load the driver class.
Class.forName("oracle.jdbc.driver.OracleDriver");
// Get the connection.
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:buzz2",
"web_site", "web_site");
try {
/ Get the prepared statement specifying the
// select SQL.
PreparedStatement ps = connection.prepareStatement(
"select LAST_HIT from LAST_ACCESS");
try {
// Execute the SQL and get the ResultSet object.
ResultSet rs = ps.executeQuery();
try {
// Retreive the record.
if (rs else {
燬ystem.out.println("Did not get last hit.");
}
}
finally {
rs.close();
}
} finally {
ps.close();
爙
} finally {
connection.close();
}
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
return returnDate;
}
}
這個(gè)例子的輸出如下:
Friday, October 5, 2001 10:42:34 PM EDT
Rows updated: 1
Successfully retrieved last hit.
Friday, October 5, 2001 12:00:00 AM EDT
雖然這個(gè)例子沒(méi)有為保存和提取日期數(shù)據(jù)提供性能上優(yōu)良的方法,但它確實(shí)示范了如何為一條更新和刪除語(yǔ)句將Java日期數(shù)據(jù)轉(zhuǎn)換成SQL日期數(shù)據(jù)。從一個(gè)java.util.Date對(duì)象設(shè)置Oracle date數(shù)據(jù)域的過(guò)程是由以下的語(yǔ)句處理的:
ps.setDate(1, date);
它是我們預(yù)定義語(yǔ)句接口java.sql.PreparedStatement.setDate 的一個(gè)方法。
這行代碼出現(xiàn)在我們的setLastHit方法里。它將Java以微秒為單位的長(zhǎng)整型日期值轉(zhuǎn)換成ORACLE的SQL日期格式。當(dāng)我們能夠在getLastHit方法里用java.sql.PreparedStatement.getDate從數(shù)據(jù)庫(kù)取得日期數(shù)據(jù)的時(shí)候這種轉(zhuǎn)換就能夠完成。
你還應(yīng)該注意到只有日期被設(shè)置了。小時(shí),分鐘,秒,和微秒都沒(méi)有包括在從Java日期數(shù)據(jù)到SQL日期數(shù)據(jù)的轉(zhuǎn)換過(guò)程中。
結(jié)論
一旦你掌握了這些概念,你就應(yīng)該能夠基于系統(tǒng)時(shí)間或者一個(gè)輸入的時(shí)間創(chuàng)建日期對(duì)象了。另外,你還應(yīng)該能夠使用標(biāo)準(zhǔn)和定制的格式化過(guò)程格式化日期數(shù)據(jù),將文本的日期數(shù)據(jù)解析成日期對(duì)象,并以多種語(yǔ)言和多種時(shí)區(qū)顯示一個(gè)日期數(shù)據(jù)。最后,你將能夠在一個(gè)SQL數(shù)據(jù)庫(kù)里保存和提取日期值