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




日期Date或Timestamp轉換成字串格式

package yku;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.sql.Date;

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 String convertDateStr(java.sql.Date str) {
		if (str == null)
			return null;

		try {
			return dateformat.format(str);
		} catch (Exception e) {
			// e.printStackTrace();

		}
		return null;
	}

	public static String convertDateTimeStr(java.sql.Date str) {
		if (str == null)
			return null;

		try {
			return datetimeformat.format(str);
		} catch (Exception e) {
			// e.printStackTrace();

		}
		return null;
	}

	public static String convertDateStr(Timestamp str) {
		if (str == null)
			return null;

		try {
			return dateformat.format(str);
		} catch (Exception e) {
			// e.printStackTrace();

		}
		return null;
	}

	public static String convertDateTimeStr(Timestamp str) {
		if (str == null)
			return null;

		try {
			return datetimeformat.format(str);
		} catch (Exception e) {
			// e.printStackTrace();

		}
		return null;
	}

	public static void main(String args[]) {
		Calendar cal = Calendar.getInstance();
		cal.set(2014, 1, 31,22,33,11);
		Date d = new Date(cal.getTimeInMillis());
		
		System.out.println("1."+ DateUtils.convertDateStr(d));
		System.out.println("2."+DateUtils.convertDateTimeStr(d));
		
		System.out.println("這裡是分割線---------------------------------------------------");
		
		Timestamp ts = new Timestamp(cal.getTimeInMillis());
		
		System.out.println("3."+ DateUtils.convertDateStr(ts));
		System.out.println("4."+ DateUtils.convertDateTimeStr(ts));

	}
}

Ans:

1.2014/03/03
2.2014/03/03 22:33:11
這裡是分割線---------------------------------------------------
3.2014/03/03
4.2014/03/03 22:33:11

 

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料