[Java]日期常用工具函數整理

字串轉換成Timestamp格式

package yku;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class DateUtils {
	private static SimpleDateFormat datetimeformat = new SimpleDateFormat(
			"yyyy/MM/dd HH:mm:ss");
	private static SimpleDateFormat dateformat = new SimpleDateFormat(
			"yyyy/MM/dd");

	public static Timestamp convertDate(String str) {
		if (str == null || str.length() == 0)
			return null;

		try {
			return new Timestamp(dateformat.parse(str).getTime());
		} catch (Exception e) {
			//e.printStackTrace();

		}
		return null;
	}

	public static Timestamp convertDateTime(String str) {
		if (str == null || str.length() == 0)
			return null;

		try {
			return new Timestamp(datetimeformat.parse(str).getTime());
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[]) {
		System.out.println("1."+DateUtils.convertDate("2014/01/01").toString());
		System.out.println("2."+(DateUtils.convertDate("2014/0101")==null?"錯誤日期格式":"正確日期格式"));
		System.out.println("這裡是分割線---------------------------------------------------");
		System.out.println("3."+DateUtils.convertDateTime("2014/01/01 12:33:21").toString());
		System.out.println("4."+(DateUtils.convertDate("2014/0101 123321")==null?"錯誤日期時間格式":"正確日期時間格式"));
		
	}
}

Ans:

1.2014-01-01 00:00:00.0
2.錯誤日期格式
這裡是分割線---------------------------------------------------
3.2014-01-01 12:33:21.0
4.錯誤日期時間格式

Read More

[JAVA]JDBC Driver整理

jTDS Driver


Microsoft SQL Server (6.5, 7, 2000, 2005, 2008 and 2012) and Sybase Adaptive Server Enterprise (10, 11, 12 and 15)

Drive Class: net.sourceforge.jtds.jdbc.Driver

Drive Location: http://jtds.sourceforge.net/

JDBC Url Format: jdbc:jtds:sqlserver://<host>:<port>/<database_name>

MsSQL Server預設port為1433

Examples:

jdbc:jtds:sqlserver://blog.yslifes.com:1433/test

jdbc:jtds:sqlserver://127.0.0.1:1433/test

 

Read More

Eclipse開發Dynamic Web Project 網頁程式-設定Tomcat與程式測試

安裝Eclipse、tomcat建立專案程式後,再來就需要啟動及設定Server來執行剛才的測試程式。

一.設定Tomcat

設定server.xml,把專案目錄的WebContent加入Content裡,啟動tomcat時啟會自動專入專案。

tomcat目錄下的confi/server.xml

修改Tomcat Server.xm

<Context path="/MyWeb" docBase="C:\workspace\MyWeb\WebContent" debug="0" crosscontext="true" reloadable="true">

    <Manager className="org.apache.catalina.session.PersistentManager" saveOnRestart="true">
          <Store className="org.apache.catalina.session.FileStore"/>
    </Manager>
</Context>

其中SaveOnRestart=true時,自動Complier後會自動重新載入,並且自動回復Session,如果有登入機制則不會被導出。
path是web url的subDir名稱,docBase則選擇專案目錄下的WebContent就可以了
complier後的classes

新增Container
Read More

Eclipse開發Dynamic Web Project 網頁程式-建立專案

下載Eclpse及Tomcat,解壓縮後,開發環境基本上已經準備好了,再來就是建立開發的jsp專案。

一.新增及建立Eclipse的Dynamic Web Project

也就是jsp專案 File->New->Dynamic Web Project

(舊版本Eclipse可能放在Other裡,點選後再選取)

建立Dynamic Web Project

二.建立專案名稱

這裡選擇Dynamic web module version 2.4以上

version 2.2 – J2EE 1.2
version 2.3 – J2EE 1.3
version 2.4 – J2EE 1.4

jsp專案名稱

專案產生如下圖

jsp專案內容及路徑 Read More