`
lucene3212
  • 浏览: 138078 次
  • 性别: Icon_minigender_2
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Time I18N

    博客分类:
  • java
阅读更多
    time i18n : 时间国际化
 
1. class diagram<br><img src="http://dl.iteye.com/upload/attachment/481139/6533ebc2-8c73-36ae-b019-527b7b2ab499.jpg" alt=""><br> calendar.settime(date date);
 calendar.gettime();
<span style="color: #ff0000;"> 返回一个表示此 <code>calendar</code> 时间值(从历元至现在的毫秒偏移量)的 <code>date</code> 对象。</span>
<span style="color: #ff0000;"> 历元 格林威治标准时间1970年1月1日的 00:00:00.000 </span>
 
<span style="color: #000000;">2. 实例</span>
通常有两种方式实现time i18n
1)在时间存取的时候统一转化为某一时区的值 type1
2)在时间存取的时候增加一timezone列 type2
 
<span style="color: #ff0000;">connectionfactory.java</span>
================================================================
package com.siyuan.time.i18n;
import java.sql.connection;<br>import java.sql.drivermanager;<br>import java.sql.sqlexception;
public class connectionfactory {<br> <br> public static string driver_class_name = "com.mysql.jdbc.driver";<br> <br> public static string user_name = "root";<br> <br> public static string password = "123456";<br> <br> public static string url = "jdbc:mysql://localhost:3306/year2";<br> <br> public static connection getconnection() throws classnotfoundexception, sqlexception {<br>  <br>  class.forname(driver_class_name);<br>  return drivermanager.getconnection(url, user_name, password);<br>  <br> }<br> <br>}
================================================================
<span style="color: #ff0000;">timei18ndao.java</span>
================================================================
package com.siyuan.time.i18n;
import java.sql.connection;<br>import java.sql.preparedstatement;<br>import java.sql.resultset;<br>import java.sql.sqlexception;<br>import java.sql.statement;<br>import java.sql.timestamp;
public class timei18ndao {<br> <br> public static final string sql_save = "insert into timei18n(time, timezone) values(?, ?)";<br> <br> public static final string sql_get = "select time, timezone from timei18n where id = ?";<br> <br> public static void save(timestamp time, string timezone) {<br>  connection conn = null;<br>  preparedstatement pstat = null;<br>  try {<br>   conn = connectionfactory.getconnection();<br>   conn.setautocommit(false);<br>   pstat = conn.preparestatement(sql_save);<br>   pstat.settimestamp(1, time);<br>   pstat.setstring(2, timezone);<br>   pstat.executeupdate();<br>   conn.commit();<br>  } catch (classnotfoundexception e) {<br>   e.printstacktrace();<br>   if (conn != null) {<br>    try {<br>     conn.rollback();<br>    } catch (sqlexception e1) {<br>     e1.printstacktrace();<br>    }<br>   }<br>  } catch (sqlexception e) {<br>   if (conn != null) {<br>    try {<br>     conn.rollback();<br>    } catch (sqlexception e1) {<br>     e1.printstacktrace();<br>    }<br>   }<br>   e.printstacktrace();<br>  } finally {<br>   close(null, pstat, conn);<br>  }<br> }<br> <br> public static object[] get(int id) {<br>  object[] result = new object[2];<br>  connection conn = null;<br>  preparedstatement pstat = null;<br>  resultset rs = null;<br>  try {<br>   conn = connectionfactory.getconnection();<br>   pstat = conn.preparestatement(sql_get);<br>   pstat.setint(1, id);<br>   rs = pstat.executequery();<br>   if (rs.next()) {<br>    result[0] = rs.gettimestamp(1);<br>    result[1] = rs.getstring(2);<br>   }<br>  } catch (classnotfoundexception e) {<br>   e.printstacktrace();<br>   if (conn != null) {<br>    try {<br>     conn.rollback();<br>    } catch (sqlexception e1) {<br>     e1.printstacktrace();<br>    }<br>   }<br>  } catch (sqlexception e) {<br>   if (conn != null) {<br>    try {<br>     conn.rollback();<br>    } catch (sqlexception e1) {<br>     e1.printstacktrace();<br>    }<br>   }<br>   e.printstacktrace();<br>  } finally {<br>   close(rs, pstat, conn);<br>  }<br>  return result;<br> }<br> <br> public static void close(resultset rs, statement stat, connection conn) {<br>  if (rs != null) {<br>   try {<br>    rs.close();<br>   } catch (sqlexception e) {<br>    e.printstacktrace();<br>   }<br>  }<br>  if (stat != null) {<br>   try {<br>    stat.close();<br>   } catch (sqlexception e) {<br>    e.printstacktrace();<br>   }<br>  }<br>  if (conn != null) {<br>   try {<br>    conn.close();<br>   } catch (sqlexception e) {<br>    e.printstacktrace();<br>   }<br>  }<br> }<br> <br>}
================================================================
<span style="color: #ff0000;">servertimezone.java</span>
================================================================
package com.siyuan.time.i18n;
import java.util.timezone;
public class servertimezone {<br> <br> private static final timezone servertz<br>   = timezone.gettimezone("gmt+02:00");<br> <br> public static timezone getservertimezone() {<br>  return servertz;<br> }<br> <br>}
================================================================
<span style="color: #ff0000;">timei18ntest.java</span>
================================================================
package com.siyuan.time.i18n;
import java.sql.timestamp;<br>import java.text.dateformat;<br>import java.text.parseexception;<br>import java.text.simpledateformat;<br>import java.util.calendar;<br>import java.util.date;<br>import java.util.gregoriancalendar;<br>import java.util.timezone;
public class timei18ntest {<br> <br> public static void settime(calendar calendar, <br>   int year, int month, int day, int hour, int minute) {<br>  calendar.set(calendar.year, year);<br>  calendar.set(calendar.month, month);<br>  calendar.set(calendar.day_of_month, day);<br>  calendar.set(calendar.hour_of_day, hour);<br>  calendar.set(calendar.minute, minute);<br> }<br> <br> /**<br>  * @param args<br>  * @throws parseexception <br>  */<br> public static void main(string[] args) throws parseexception {<br>  string input = "01/05/2011 13:30";<br>  dateformat datefmt = new simpledateformat("dd/mm/yyyy hh:mm");<br>  date inputdate = datefmt.parse(input);<br>  <br>  /*<br>   * type 1<br>   */<br>  //save<br>  timezone servertz = servertimezone.getservertimezone();<br>  calendar local = gregoriancalendar.getinstance();<br>  system.out.println(local.gettimeinmillis());<br>  local.settime(inputdate);<br>  system.out.println(local.gettimeinmillis());<br>  local.settimezone(servertz);<br>  system.out.println(local.gettimeinmillis());<br>  calendar save = gregoriancalendar.getinstance();<br>  settime(save, <br>    local.get(calendar.year), <br>    local.get(calendar.month), <br>    local.get(calendar.day_of_month), <br>    local.get(calendar.hour_of_day), <br>    local.get(calendar.minute));<br>  timei18ndao.save(new timestamp(save.gettime().gettime()), servertz.getid());<br>  <br>  //get<br>//  object[] result = timei18ndao.get(4);<br>//  calendar get = gregoriancalendar.getinstance();<br>//  get.settime((date)result[0]);<br>//  calendar view = gregoriancalendar.getinstance();<br>//  view.settimezone(servertz);<br>//  settime(view, <br>//    get.get(calendar.year), <br>//    get.get(calendar.month), <br>//    get.get(calendar.day_of_month), <br>//    get.get(calendar.hour_of_day), <br>//    get.get(calendar.minute));<br>//  <br>//  system.out.println(datefmt.format(view.gettime()));<br>  <br>  /*<br>   * type 2<br>   */<br>//  timezone local1tz = timezone.gettimezone("gmt+08:00");<br>//  calendar local1 = gregoriancalendar.getinstance();<br>//  local1.settime(inputdate);<br>//  timei18ndao.save(new timestamp(local1.gettime().gettime()), local1tz.getid());<br>//  <br>//  timezone local2tz = timezone.gettimezone("gmt+02:00");<br>//  object[] result = timei18ndao.get(4);<br>//  calendar get = gregoriancalendar.getinstance();<br>//  get.settime((date) result[0]);<br>//  <br>//  calendar local1 = gregoriancalendar.getinstance();<br>//  local1.settimezone(timezone.gettimezone((string) result[1]));<br>//  settime(local1,<br>//    get.get(calendar.year), <br>//    get.get(calendar.month), <br>//    get.get(calendar.day_of_month), <br>//    get.get(calendar.hour_of_day), <br>//    get.get(calendar.minute));<br>//  <br>//  calendar local2 = gregoriancalendar.getinstance();<br>//  local2.settimezone(local2tz);<br>//  local1.settimezone(local2tz);<br>//  settime(local2,<br>//    local1.get(calendar.year), <br>//    local1.get(calendar.month), <br>//    local1.get(calendar.day_of_month), <br>//    local1.get(calendar.hour_of_day), <br>//    local1.get(calendar.minute));<br>//  datefmt.settimezone(local2tz);<br>//  system.out.println(datefmt.format(local2.gettime()));<br> }
}
 
 
3.测试及结果
此实例需分次运行,比较复杂
type 1
================================
第一次:
1305336107484<br><span style="color: #ff0000;">1304227800000<br>1304227800000</span>
 <img src="http://dl.iteye.com/upload/attachment/481143/86a8f40f-94e6-34a1-964c-87fdda8cf11d.jpg" alt="">
第二次:
01/05/2011 13:30
================================
type 2
================================
第一次
 <img src="http://dl.iteye.com/upload/attachment/481145/959128f2-c402-392a-9572-1086762967de.jpg" alt="">
第二次 
01/05/2011 13:30
 
 
0
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics