Time I18N
TIME I18N : 时间国际化
1. Class Diagram
Calendar.setTime(Date date);
Calendar.getTime();
返回一个表示此 Calendar
时间值(从历元至现在的毫秒偏移量)的 Date
对象。
历元 格林威治标准时间1970年1月1日的 00:00:00.000
2. 实例
通常有两种方式实现Time I18N
1)在时间存取的时候统一转化为某一时区的值 Type1
2)在时间存取的时候增加一TimeZone列 Type2
ConnectionFactory.java
================================================================
package com.siyuan.time.i18n;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public static String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
public static String USER_NAME = "root";
public static String PASSWORD = "123456";
public static String URL = "jdbc:mysql://localhost:3306/YEAR2";
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(DRIVER_CLASS_NAME);
return DriverManager.getConnection(URL, USER_NAME, PASSWORD);
}
}
================================================================
TimeI18NDAO.java
================================================================
package com.siyuan.time.i18n;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
public class TimeI18NDAO {
public static final String SQL_SAVE = "INSERT INTO TimeI18N(time, timeZone) values(?, ?)";
public static final String SQL_GET = "SELECT time, timeZone FROM TimeI18N WHERE id = ?";
public static void save(Timestamp time, String timeZone) {
Connection conn = null;
PreparedStatement pstat = null;
try {
conn = ConnectionFactory.getConnection();
conn.setAutoCommit(false);
pstat = conn.prepareStatement(SQL_SAVE);
pstat.setTimestamp(1, time);
pstat.setString(2, timeZone);
pstat.executeUpdate();
conn.commit();
} catch (ClassNotFoundException e) {
e.printStackTrace();
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
} finally {
close(null, pstat, conn);
}
}
public static Object[] get(int id) {
Object[] result = new Object[2];
Connection conn = null;
PreparedStatement pstat = null;
ResultSet rs = null;
try {
conn = ConnectionFactory.getConnection();
pstat = conn.prepareStatement(SQL_GET);
pstat.setInt(1, id);
rs = pstat.executeQuery();
if (rs.next()) {
result[0] = rs.getTimestamp(1);
result[1] = rs.getString(2);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
} finally {
close(rs, pstat, conn);
}
return result;
}
public static void close(ResultSet rs, Statement stat, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
================================================================
ServerTimeZone.java
================================================================
package com.siyuan.time.i18n;
import java.util.TimeZone;
public class ServerTimeZone {
private static final TimeZone serverTZ
= TimeZone.getTimeZone("GMT+02:00");
public static TimeZone getServerTimeZone() {
return serverTZ;
}
}
================================================================
TimeI18NTest.java
================================================================
package com.siyuan.time.i18n;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class TimeI18NTest {
public static void setTime(Calendar calendar,
int year, int month, int day, int hour, int minute) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
String input = "01/05/2011 13:30";
DateFormat dateFmt = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date inputDate = dateFmt.parse(input);
/*
* Type 1
*/
//save
TimeZone serverTZ = ServerTimeZone.getServerTimeZone();
Calendar local = GregorianCalendar.getInstance();
System.out.println(local.getTimeInMillis());
local.setTime(inputDate);
System.out.println(local.getTimeInMillis());
local.setTimeZone(serverTZ);
System.out.println(local.getTimeInMillis());
Calendar save = GregorianCalendar.getInstance();
setTime(save,
local.get(Calendar.YEAR),
local.get(Calendar.MONTH),
local.get(Calendar.DAY_OF_MONTH),
local.get(Calendar.HOUR_OF_DAY),
local.get(Calendar.MINUTE));
TimeI18NDAO.save(new Timestamp(save.getTime().getTime()), serverTZ.getID());
//GET
// Object[] result = TimeI18NDAO.get(4);
// Calendar get = GregorianCalendar.getInstance();
// get.setTime((Date)result[0]);
// Calendar view = GregorianCalendar.getInstance();
// view.setTimeZone(serverTZ);
// setTime(view,
// get.get(Calendar.YEAR),
// get.get(Calendar.MONTH),
// get.get(Calendar.DAY_OF_MONTH),
// get.get(Calendar.HOUR_OF_DAY),
// get.get(Calendar.MINUTE));
//
// System.out.println(dateFmt.format(view.getTime()));
/*
* Type 2
*/
// TimeZone local1TZ = TimeZone.getTimeZone("GMT+08:00");
// Calendar local1 = GregorianCalendar.getInstance();
// local1.setTime(inputDate);
// TimeI18NDAO.save(new Timestamp(local1.getTime().getTime()), local1TZ.getID());
//
// TimeZone local2TZ = TimeZone.getTimeZone("GMT+02:00");
// Object[] result = TimeI18NDAO.get(4);
// Calendar get = GregorianCalendar.getInstance();
// get.setTime((Date) result[0]);
//
// Calendar local1 = GregorianCalendar.getInstance();
// local1.setTimeZone(TimeZone.getTimeZone((String) result[1]));
// setTime(local1,
// get.get(Calendar.YEAR),
// get.get(Calendar.MONTH),
// get.get(Calendar.DAY_OF_MONTH),
// get.get(Calendar.HOUR_OF_DAY),
// get.get(Calendar.MINUTE));
//
// Calendar local2 = GregorianCalendar.getInstance();
// local2.setTimeZone(local2TZ);
// local1.setTimeZone(local2TZ);
// setTime(local2,
// local1.get(Calendar.YEAR),
// local1.get(Calendar.MONTH),
// local1.get(Calendar.DAY_OF_MONTH),
// local1.get(Calendar.HOUR_OF_DAY),
// local1.get(Calendar.MINUTE));
// dateFmt.setTimeZone(local2TZ);
// System.out.println(dateFmt.format(local2.getTime()));
}
}
3.测试及结果
此实例需分次运行,比较复杂
Type 1
================================
第一次:
1305336107484
1304227800000
1304227800000
第二次:
01/05/2011 13:30
================================
Type 2
================================
第一次
第二次
01/05/2011 13:30