av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Java高級日期概念二

瀏覽:3日期:2024-06-21 17:12:55
內(nèi)容: 出自:zdnet 時區(qū)TimeZone類,即java.util.TimeZone類的實(shí)例包含了一個與格林威治標(biāo)準(zhǔn)時間(GMT)相比較得出的以微秒為單位的時區(qū)偏移量,而且它還處理夏令時。要獲得一個所有支持的進(jìn)區(qū)的列表,你可以使用方法TimeZone.getAvailableIDs,它將返回一個包含了所有進(jìn)區(qū)ID的字符串?dāng)?shù)組。要知道關(guān)于TimeZone類的更多細(xì)節(jié),可以參看Sun公司的Web站點(diǎn)。為了演示這個概念,我們將創(chuàng)建三個時區(qū)對象。第一個對象將使用getDefault從系統(tǒng)時鐘返回時區(qū)數(shù)據(jù);第二個和第三個對象將傳入一個時區(qū)字符串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 TimeRawOffset: -18000000Uses daylight saving: trueGMT+00:00RawOffset: 0Uses daylight saving: trueCentral European Standard TimeRawOffset: 3600000Uses daylight saving: true正如你所看見的,TimeZone對象給我們的是原始的偏移量,也就是與GMT相差的微秒數(shù),而且還會告訴我們這個時區(qū)是否使用夏令時。有個這個信息,我們就能夠繼續(xù)將時區(qū)對象和日期格式化器結(jié)合在一起在其它的時區(qū)和其它的語言顯示時間了。國際化的時期顯示了時區(qū)轉(zhuǎn)換讓我們來看一個結(jié)合了國際化顯示,時區(qū)和日期格式化的例子。表D為一個在邁阿密和巴黎擁有辦公室的公司顯示了當(dāng)前的完整日期和時間。對于邁阿密的辦公室,我們將在每個辦公室里用英語顯示完整的日期和時間。對于巴黎的辦公室,我們將用法語顯示完整的當(dāng)前日期和時間。表 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 EnglishSystem.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 FrenchSystem.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 TimeFriday, October 5, 2001 10:28:02 PM EDTCentral European Standard TimeSaturday, October 6, 2001 4:28:02 AM CESTDisplay for Paris office. GMT-05:00vendredi 5 octobre 2001 22 h 28 GMT-04:00GMT+01:00samedi 6 octobre 2001 04 h 28 GMT+02:00 在一個SQL數(shù)據(jù)庫中保存和提取日期數(shù)據(jù)我們將要使用的下一個類是java.sql.Date,它是java.util.Date的子類但它使用了Java數(shù)據(jù)庫連接(JDBC)方法 。讓我們來看一個簡單的只有一個表單--LAST_ACCESS的ORACLE數(shù)據(jù)庫,它是用下面的SQL創(chuàng)建的:create table LAST_ACCESS (LAST_HIT date);這個表單只有一個記錄,用下面的插入語句創(chuàng)建:insert into LAST_ACCESS values (Sysdate);表E演示了如何修改和提取LAST_HIT數(shù)據(jù)庫域。表 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.Datejava.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'); // Passwordtry {/ 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;}}這個例子的輸出如下:Friday, October 5, 2001 10:42:34 PM EDTRows updated: 1Successfully retrieved last hit.Friday, October 5, 2001 12:00:00 AM EDT雖然這個例子沒有為保存和提取日期數(shù)據(jù)提供性能上優(yōu)良的方法,但它確實(shí)示范了如何為一條更新和刪除語句將Java日期數(shù)據(jù)轉(zhuǎn)換成SQL日期數(shù)據(jù)。從一個java.util.Date對象設(shè)置Oracle date數(shù)據(jù)域的過程是由以下的語句處理的:ps.setDate(1, date);它是我們預(yù)定義語句接口java.sql.PreparedStatement.setDate 的一個方法。這行代碼出現(xiàn)在我們的setLastHit方法里。它將Java以微秒為單位的長整型日期值轉(zhuǎn)換成ORACLE的SQL日期格式。當(dāng)我們能夠在getLastHit方法里用java.sql.PreparedStatement.getDate從數(shù)據(jù)庫取得日期數(shù)據(jù)的時候這種轉(zhuǎn)換就能夠完成。你還應(yīng)該注意到只有日期被設(shè)置了。小時,分鐘,秒,和微秒都沒有包括在從Java日期數(shù)據(jù)到SQL日期數(shù)據(jù)的轉(zhuǎn)換過程中。結(jié)論一旦你掌握了這些概念,你就應(yīng)該能夠基于系統(tǒng)時間或者一個輸入的時間創(chuàng)建日期對象了。另外,你還應(yīng)該能夠使用標(biāo)準(zhǔn)和定制的格式化過程格式化日期數(shù)據(jù),將文本的日期數(shù)據(jù)解析成日期對象,并以多種語言和多種時區(qū)顯示一個日期數(shù)據(jù)。最后,你將能夠在一個SQL數(shù)據(jù)庫里保存和提取日期值 Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 午夜av网站| 中文字幕在线观看一区 | 老司机免费福利视频 | 中文字幕国产 | 啪啪免费网站 | 中文字幕在线免费观看视频 | 亚洲精品在线视频观看 | 国产中文字幕av | 亚洲高清视频在线观看 | 中文字幕色哟哟 | 午夜爱| 天天干天天操天天爽 | 国产精品久久久久久久成人午夜 | 亚洲欧美在线播放 | 黄色一级视频免费看 | 日日摸天天添天天添破 | 免费一级片 | 特黄一级片| 天天色天天爱 | 亚洲精品午夜精品 | 一区二区色 | 中文字幕在线观看亚洲 | 在线网站免费观看18 | 激情五月婷婷 | 免费的av | 黄色片在线播放 | 欧美日韩成人一区二区三区 | 97视频免费在线观看 | 人人看av | 中文字幕专区 | 久草免费福利视频 | 国产精品美女久久久久久久久 | 久久久久久久久国产 | 国产福利在线播放 | 亚洲激情久久 | 在线色综合 | 亚洲免费在线播放 | 欧美在线免费观看 | 一级黄色在线观看 | 亚洲精品国产精品国自产观看浪潮 | 性久久久久 |