2.明確你要轉向時間所在的時區
3.獲取當前時間所在時區相對GMT的偏移量
4.當前時間-相對GMT的偏移量來獲得當前時間的GMT的值
5.GMT的值+轉向時區相對GMT的偏移量獲取轉向時區的時間值
例如:你要從GMT+8時間轉向GMT+7時間
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)";
/**
* 轉換時間時區
* @param convertString 需要轉的時間字符串
* @param format 格式話字符串 例如d-MMM-yyyy HH:mm (z)
* @param sourceTimeZone 源時間時區
* @param targetTimeZone 目標時間時區
* @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);
//獲取傳入的時間值
Long time = new Date(sdf.parse(convertString).getTime()).getTime();
//獲取源時區時間相對的GMT時間
Long sourceRelativelyGMT=time-TimeZone.getTimeZone(sourceTimeZone).getRawOffset();
//GMT時間+目標時間時區的偏移量獲取目標時間
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;
}
}
這將涉及到字符解碼操作,我們在應用中常常會用new String(fieldType.getBytes("iso-8859-1"), "UTF-8");等類似的方法去解碼。但這種方式受具體應用環境限制,往往在應用部署環境發生改變時,還會出現中文亂碼。
在這里介紹一種解決方法,可以在任何應用部署環境下通用。此方法分兩步:
1、在客戶端用escape(encodeURIComponent(fieldValue))方法編碼,例如:
title=escape(encodeURIComponent(title)); //這是js里的函數
url="<%=request.getContextPath()%>/print/printList!printTable.act
2、在服務端用java.net.URLDecoder.decode(getRequest().getParameter("title"),"UTF-8"),進行解碼。
-----------------------------------------------------------------------------
parent.window.location.href 和 iframe中src的亂碼問題。
要在這兩個url地址中傳中文,必須加編碼,然后再解碼。
編碼:encodeURI(encodeURI("包含中文的串"))
解碼:java.net.URLDecoder.decode("需要解碼的串","utf-8");
encodeURI方法是正確的,只是需要使用兩次encodeURI方法,例如encodeURI(encodeURI("中文"));第一次是把中文編碼成%xy的格式,第二次是對%xy中的%進行編碼,%編碼成%25。整個傳參過程大體應該是:提交頁面使用encodeURI(encodeURI("中文"))編碼,把最后的編碼結果%25xy傳遞給處理頁面的過程中,瀏覽器獲取URL地址(注意openModelDialog方法,瀏覽器獲取不到參數編碼)后解碼成%xy,然后把%xy傳遞給處理頁面,處理頁面使用URLDecoder.decode(request.getParameter("參數名"),"UTF-8");完成解碼。
總結:
1、漢字出現在URL路徑部分的時候不需要編碼解碼;
2、使用encodeURI進行2次編碼;
3、在openModelDialog()打開的模式窗體里沒辦法用request.getParameter正確獲取參數;
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";//根據具體配置文件名稱配置
/**
*根據屬性名得到資源屬性
*
*@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;
/**
* 編程調用c3p0
*
* @author xuhua
*
*/
public class C3P0DBConnectionManager {
private static ComboPooledDataSource cpds = null;
/**
* 初始化
*/
public static void init() {
// 建立數據庫連接池
String DRIVER_NAME = C3P0SystemConfig
.getConfigInfomation("jdbc.driverClassName"); // 驅動器
String DATABASE_URL = C3P0SystemConfig.getConfigInfomation("jdbc.url"); // 數據庫連接url
String DATABASE_USER = C3P0SystemConfig
.getConfigInfomation("jdbc.username"); // 數據庫用戶名
String DATABASE_PASSWORD = C3P0SystemConfig
.getConfigInfomation("jdbc.password"); // 數據庫密碼
int Min_PoolSize = 5;
int Max_PoolSize = 50;
int Acquire_Increment = 5;
int Initial_PoolSize = 10;
// 每隔3000s測試連接是否可以正常使用
int Idle_Test_Period = 3000;
// 每次連接驗證連接是否可用
String Validate = C3P0SystemConfig.getConfigInfomation("c3p0.validate");
if (Validate.equals("")) {
Validate = "false";
}
// 最小連接數
try {
Min_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.minPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 增量條數
try {
Acquire_Increment = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.acquireIncrement"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 最大連接數
try {
Max_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.maxPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 初始化連接數
try {
Initial_PoolSize = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.initialPoolSize"));
} catch (Exception ex) {
ex.printStackTrace();
}
// 每隔Idle_Test_Period s測試連接是否可以正常使用
try {
Idle_Test_Period = Integer.parseInt(C3P0SystemConfig
.getConfigInfomation("c3p0.idleConnectionTestPeriod"));
} catch (Exception ex) {
ex.printStackTrace();
}
try {
cpds = new ComboPooledDataSource();
cpds.setDriverClass(DRIVER_NAME); // 驅動器
cpds.setJdbcUrl(DATABASE_URL); // 數據庫url
cpds.setUser(DATABASE_USER); // 用戶名
cpds.setPassword(DATABASE_PASSWORD); // 密碼
cpds.setInitialPoolSize(Initial_PoolSize); // 初始化連接池大小
cpds.setMinPoolSize(Min_PoolSize); // 最少連接數
cpds.setMaxPoolSize(Max_PoolSize); // 最大連接數
cpds.setAcquireIncrement(Acquire_Increment); // 連接數的增量
cpds.setIdleConnectionTestPeriod(Idle_Test_Period); // 測連接有效的時間間隔
cpds.setTestConnectionOnCheckout(Boolean.getBoolean(Validate)); // 每次連接驗證連接是否可用
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 取得鏈接
*
* @return
*/
public static Connection getConnection() {
Connection connection = null;
try {// 保證只進行一次初始化
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>
配置一個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);
}
}
已經配置完畢
這樣可以在java普通類或者其他地方可以System.getProperty("webapp.root"),得到項目的根路徑