2.明確你要轉(zhuǎn)向時(shí)間所在的時(shí)區(qū)
3.獲取當(dāng)前時(shí)間所在時(shí)區(qū)相對(duì)GMT的偏移量
4.當(dāng)前時(shí)間-相對(duì)GMT的偏移量來獲得當(dāng)前時(shí)間的GMT的值
5.GMT的值+轉(zhuǎn)向時(shí)區(qū)相對(duì)GMT的偏移量獲取轉(zhuǎn)向時(shí)區(qū)的時(shí)間值
例如:你要從GMT+8時(shí)間轉(zhuǎn)向GMT+7時(shí)間
sourceTimeZone 為GMT+8
/**
*
*/
package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class DateTimeUtil {
static String DEFAULT_TIMEZONE = "GMT+8";
static String DEFAULT_FORMAT = "d-MMM-yyyy HH:mm (z)";
/**
* 轉(zhuǎn)換時(shí)間時(shí)區(qū)
* @param convertString 需要轉(zhuǎn)的時(shí)間字符串
* @param format 格式話字符串 例如d-MMM-yyyy HH:mm (z)
* @param sourceTimeZone 源時(shí)間時(shí)區(qū)
* @param targetTimeZone 目標(biāo)時(shí)間時(shí)區(qū)
* @return
* @throws ParseException
*/
public static Date ConverDateGMT(String convertString,String format,String sourceTimeZone,String targetTimeZone) throws ParseException
{
Date date=null;
if(isEmpty(sourceTimeZone)){
sourceTimeZone = DEFAULT_TIMEZONE;
}
if(isEmpty(targetTimeZone)){
targetTimeZone = DEFAULT_TIMEZONE;
}
if(isEmpty(format)){
format = DEFAULT_FORMAT;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
//獲取傳入的時(shí)間值
Long time = new Date(sdf.parse(convertString).getTime()).getTime();
//獲取源時(shí)區(qū)時(shí)間相對(duì)的GMT時(shí)間
Long sourceRelativelyGMT=time-TimeZone.getTimeZone(sourceTimeZone).getRawOffset();
//GMT時(shí)間+目標(biāo)時(shí)間時(shí)區(qū)的偏移量獲取目標(biāo)時(shí)間
Long targetTime=sourceRelativelyGMT+TimeZone.getTimeZone(targetTimeZone).getRawOffset();
date= new Date(targetTime);
return date;
}
/**
* Check empty string
* <pre>
* null: true
* "": true
* " ":true
* </>
*
* @param value
* @return
*/
public static boolean isEmpty(String value) {
boolean emptyFlg = false;
if (null == value || value.trim().length() <= 0) {
emptyFlg = true;
}
return emptyFlg;
}
}
這將涉及到字符解碼操作,我們?cè)趹?yīng)用中常常會(huì)用new String(fieldType.getBytes("iso-8859-1"), "UTF-8");等類似的方法去解碼。但這種方式受具體應(yīng)用環(huán)境限制,往往在應(yīng)用部署環(huán)境發(fā)生改變時(shí),還會(huì)出現(xiàn)中文亂碼。
在這里介紹一種解決方法,可以在任何應(yīng)用部署環(huán)境下通用。此方法分兩步:
1、在客戶端用escape(encodeURIComponent(fieldValue))方法編碼,例如:
title=escape(encodeURIComponent(title)); //這是js里的函數(shù)
url="<%=request.getContextPath()%>/print/printList!printTable.act
2、在服務(wù)端用java.net.URLDecoder.decode(getRequest().getParameter("title"),"UTF-8"),進(jìn)行解碼。
-----------------------------------------------------------------------------
parent.window.location.href 和 iframe中src的亂碼問題。
要在這兩個(gè)url地址中傳中文,必須加編碼,然后再解碼。
編碼:encodeURI(encodeURI("包含中文的串"))
解碼:java.net.URLDecoder.decode("需要解碼的串","utf-8");
encodeURI方法是正確的,只是需要使用兩次encodeURI方法,例如encodeURI(encodeURI("中文"));第一次是把中文編碼成%xy的格式,第二次是對(duì)%xy中的%進(jìn)行編碼,%編碼成%25。整個(gè)傳參過程大體應(yīng)該是:提交頁面使用encodeURI(encodeURI("中文"))編碼,把最后的編碼結(jié)果%25xy傳遞給處理頁面的過程中,瀏覽器獲取URL地址(注意openModelDialog方法,瀏覽器獲取不到參數(shù)編碼)后解碼成%xy,然后把%xy傳遞給處理頁面,處理頁面使用URLDecoder.decode(request.getParameter("參數(shù)名"),"UTF-8");完成解碼。
總結(jié):
1、漢字出現(xiàn)在URL路徑部分的時(shí)候不需要編碼解碼;
2、使用encodeURI進(jìn)行2次編碼;
3、在openModelDialog()打開的模式窗體里沒辦法用request.getParameter正確獲取參數(shù);
jdbc.driverClassName=com.ibm.db2.jcc.DB2Driver
#---------------------------------------------------
DEVELOP DATABASE
jdbc.url=jdbc:db2://10.10.0.163:50000/MACRODB
#jdbc.url=jdbc:db2://10.10.0.154:50000/SAMPLE
#---------------------------------------------------
#TEST DATABASE
#jdbc.url=jdbc:oracle:thin:@192.168.1.100:1521:orcl
#---------------------------------------------------
#LOCALHOST DATABASE
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#jdbc.username=db2inst1
#jdbc.password=db2inst1
jdbc.username=db2inst1
jdbc.password=123456
c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.minPoolSize=10
c3p0.maxPoolSize=15
c3p0.maxIdleTime=30
c3p0.idleConnectionTestPeriod=30
c3p0.maxStatements=100
c3p0.numHelperThreads=50
c3p0.checkoutTimeout=0
c3p0.validate=true
讀取配置文件:
package com.nci.macrodb.core.sql;
import java.util.ResourceBundle;
/**
*取得資源文件
*
*@authorldw
*
*/
publicclass C3P0SystemConfig {
static String configFile = "spring/jdbc";//根據(jù)具體配置文件名稱配置
/**
*根據(jù)屬性名得到資源屬性
*
*@paramitemIndex
*@return
*/
publicstatic String getConfigInfomation(String itemIndex) {
try {
ResourceBundle resource = ResourceBundle.getBundle(configFile);
return resource.getString(itemIndex);
} catch (Exception e) {
return"";
}
}
}
獲得連接:
package com.nci.macrodb.core.sql;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 編程調(diào)用c3p0
*
* @author xuhua
*
*/
public class C3P0DBConnectionManager {
private static ComboPooledDataSource cpds = null;
/**
* 初始化
*/
public static void init() {
// 建立數(shù)據(jù)庫連接池
String DRIVER_NAME = C3P0SystemConfig
.getConfigInfomation("jdbc.driverClassName"); // 驅(qū)動(dòng)器
String DATABASE_URL = C3P0SystemConfig.getConfigInfomation("jdbc.url"); // 數(shù)據(jù)庫連接url
String DATABASE_USER = C3P0SystemConfig
.getConfigInfomation("jdbc.username"); // 數(shù)據(jù)庫用戶名
String DATABASE_PASSWORD = C3P0SystemConfig
.getConfigInfomation("jdbc.password"); // 數(shù)據(jù)庫密碼
int Min_PoolSize = 5;
int Max_PoolSize = 50;
int Acquire_Increment = 5;
int Initial_PoolSize = 10;
// 每隔3000s測(cè)試連接是否可以正常使用
int Idle_Test_Period = 3000;
// 每次連接驗(yàn)證連接是否可用
String Validate = C3P0SystemConfig.getConfigInfomation("c3p0.validate");
if (Validate.equals("")) {
Validate = "false";
}
// 最小連接數(shù)
try {
Min_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.minPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 增量條數(shù)
try {
Acquire_Increment = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.acquireIncrement"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 最大連接數(shù)
try {
Max_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.maxPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 初始化連接數(shù)
try {
Initial_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.initialPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 每隔Idle_Test_Period s測(cè)試連接是否可以正常使用
try {
Idle_Test_Period = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.idleConnectionTestPeriod"));
} catch (Exception ex) {
ex.printStackTrace();
}
try {
cpds = new ComboPooledDataSource();
cpds.setDriverClass(DRIVER_NAME); // 驅(qū)動(dòng)器
cpds.setJdbcUrl(DATABASE_URL); // 數(shù)據(jù)庫url
cpds.setUser(DATABASE_USER); // 用戶名
cpds.setPassword(DATABASE_PASSWORD); // 密碼
cpds.setInitialPoolSize(Initial_PoolSize); // 初始化連接池大小
cpds.setMinPoolSize(Min_PoolSize); // 最少連接數(shù)
cpds.setMaxPoolSize(Max_PoolSize); // 最大連接數(shù)
cpds.setAcquireIncrement(Acquire_Increment); // 連接數(shù)的增量
cpds.setIdleConnectionTestPeriod(Idle_Test_Period); // 測(cè)連接有效的時(shí)間間隔
cpds.setTestConnectionOnCheckout(Boolean.getBoolean(Validate)); // 每次連接驗(yàn)證連接是否可用
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 取得鏈接
*
* @return
*/
public static Connection getConnection() {
Connection connection = null;
try {// 保證只進(jìn)行一次初始化
if (cpds == null) {
init();
}
// 取得connection
connection = cpds.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
}
return connection;
}
/**
* 釋放連接
*/
public static void release() {
try {
if (cpds != null) {
cpds.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Web.xml配置:
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.wes.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
配置一個(gè)Servlet 名稱為:MainServlet
MainServlet:
package com.wes.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class MainServlet extends HttpServlet{
public void init(){
String webAppRootKey = getServletContext().getRealPath("/");
System.setProperty("webapp.root" , webAppRootKey);
}
}
已經(jīng)配置完畢
這樣可以在java普通類或者其他地方可以System.getProperty("webapp.root"),得到項(xiàng)目的根路徑