欢迎光临
我们一直在努力

Java高级日期概念 (献给那些要国际化时间及SQL时间的兄弟)-JSP教程,Java基础

建站超值云服务器,限时71元/月
                  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数据库里保存和提取日期值。
 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Java高级日期概念 (献给那些要国际化时间及SQL时间的兄弟)-JSP教程,Java基础
分享到: 更多 (0)

相关推荐

  • 暂无文章