繁体中文
设为首页
加入收藏
当前位置:JSP技术首页 >> 资料/其它 >> Java高级日期概念 (献给那些要国际化时间及SQL时间的兄弟)

Java高级日期概念 (献给那些要国际化时间及SQL时间的兄弟)

2005-02-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:                  Java高级日期概念 如果你的Java 程序向处在不同时区或者不同国家的用户显示时间和日期,那么你需要了解Java日期类的一些更加高级的方面 。 本文中讨论的类将包含java.text....

                  Java高级日期概念

如果你的Java 程序向处在不同时区或者不同国家的用户显示时间和日期,那么你需要了解Java日期类的一些更加高级的方面

本文中讨论的类将包含java.text.DateFormat,以及java.util.TimeZone和java.util.Locate。我们还将讨论如何使用一个java.util.Date的子类java.sql.Date来从Oracle数据库里提取和保存Java日期数据。

地区的问题

在我们国际化我们的日期数据以前,我们需要进一步的学习Locale类,也就是java.util.Locale。Locale类的一个实例通常包含国家和语言信息。其中的每一个部分都是由基于国际标准化组织(ISO)制定的国家代码ISO-3166和语言代码ISO-639的两字符的字符串构成的。

让我们来创建两个Locale实例,其中一个对应的是美国英语而另一个对应的是法国法语。见表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));

}

}

在这个例子中,我们用getDisplayName方法来显示Locale的一个更易读的文本。你还应该注意到我们在最后一次调用getDisplayName的时候,我们在对English Locale对象调用getDisplayName的时候同时传递了French Locale对象。这允许我们选择显示Locale对象所用的语言,让我们用英语显示法语Locale对象的内容。下面是这个例子的输出:

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)

多个地域的日期格式化

使用java.util.Locale和java.text.DateFormat类我们就能够格式化日期数据把它显示给在另一个地域的用户,比方法国。表B中的例子为英语和法语各创建了一个完整的日期格式化器。

表 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));

}

}

这个例子的输出是:

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

注意这个输出包括了时区信息:GMT-04:00 和 PM EDT。这个时区是人系统的时区设置里捕获的。你可以看见,日期是以那个地区的用户期望的格式显示的。让我们等一下来看看时区的概念。

时区

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

在一个SQL数据库中保存和提取日期数据

我们将要使用的下一个类是java.sql.Date,它是java.util.Date的子类但它使用了Java数据库连接(JDBC)方法

。让我们来看一个简单的只有一个表单--LAST_ACCESS的ORACLE数据库,它是用下面的SQL创建的:

create table LAST_ACCESS (

LAST_HIT date

);

这个表单只有一个记录,用下面的插入语句创建:

insert into LAST_ACCESS values (Sysdate);

表E演示了如何修改和提取LAST_HIT数据库域。

表 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.next()) {

// Return the last hit date.

returnDate = rs.getDate("LAST_HIT");

System.out.println(

"Successfully retrieved last hit.");

} 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;

}

}

这个例子的输出如下:

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

虽然这个例子没有为保存和提取日期数据提供性能上优良的方法,但它确实示范了如何为一条更新和删除语句将Java日期数据转换成SQL日期数据。从一个java.util.Date对象设置Oracle date数据域的过程是由以下的语句处理的:

ps.setDate(1, date);

它是我们预定义语句接口java.sql.PreparedStatement.setDate 的一个方法。

这行代码出现在我们的setLastHit方法里。它将Java以微秒为单位的长整型日期值转换成ORACLE的SQL日期格式。当我们能够在getLastHit方法里用java.sql.PreparedStatement.getDate从数据库取得日期数据的时候这种转换就能够完成。

你还应该注意到只有日期被设置了。小时,分钟,秒,和微秒都没有包括在从Java日期数据到SQL日期数据的转换过程中。

结论

一旦你掌握了这些概念,你就应该能够基于系统时间或者一个输入的时间创建日期对象了。另外,你还应该能够使用标准和定制的格式化过程格式化日期数据,将文本的日期数据解析成日期对象,并以多种语言和多种时区显示一个日期数据。最后,你将能够在一个SQL数据库里保存和提取日期值。

责任编辑:admin
相关文章