[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 synchronized 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 synchronized 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.錯誤日期時間格式

閱讀全文

[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

 

閱讀全文