DateUtil

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

public class DateUtil {

	/**
	 * @param args
	 */
	private static java.util.Calendar CALENDAR = Calendar.getInstance();

	// private SimpleDateFormat simpleDateFormat = null;

	public static void main(String[] args) throws ParseException {

		// Timestamp t = new Timestamp(1000);
		String t = "2008-07-10";
		java.text.DateFormat format = java.text.DateFormat.getDateInstance();
		java.util.Date date = format.parse(t);
		System.out.println(getORACLEdatestring(new Timestamp(date.getTime())));
		// System.out.println("now:" + getCurFormatDate());
		// System.out.println("getDate2All:" + getDate2All(CALENDAR.getTime()));
		// System.out.println("getDate2LStr:" +
		// getDate2LStr(CALENDAR.getTime()));
		// System.out
		// .println("getDate2LStr2:" + getDate2LStr2(CALENDAR.getTime()));
		// System.out.println("getDate2MStr:" +
		// getDate2MStr(CALENDAR.getTime()));
		// System.out.println("getPastdayDate:" + getPastdayDate(1));
		System.out.println("getPastdayDate2:" + getPastdayDate2(-2));

	}

	public static String getORACLEdatestring(Object t) {
		return "to_date('"
				+ new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(t)
				+ "','yyyy.mm.dd hh24:mi:ss')";
	}

	/**
	 * 取得当前时间
	 * 
	 * @return
	 */
	public static String getCurFormatDate() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		java.util.Date now = CALENDAR.getTime();
		String formatDate = simpleDateFormat.format(now);
		return formatDate;
	}

	/**
	 * 取得型如:yyyyMMddhhmmss的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String getDate2All(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss");
		String formatDate = null;
		if (date != null) {
			formatDate = simpleDateFormat.format(date);
		}
		return formatDate;
	}

	/**
	 * 将日期转换为长字符串(包含:年-月-日 时:分:秒)
	 * 
	 * @param date
	 * @return
	 */
	public static String getDate2LStr(java.util.Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String formatDate = null;
		if (date != null) {
			formatDate = simpleDateFormat.format(date);
		}
		return formatDate;
	}

	/**
	 * 将日期转换为长字符串(包含:年/月/日 时:分:秒)
	 * 
	 * @param date
	 * @return
	 */
	public static String getDate2LStr2(java.util.Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		String formatDate = null;
		if (date != null) {
			formatDate = simpleDateFormat.format(date);
		}
		return formatDate;
	}

	/**
	 * 将日期转换为中长字符串(包含:年-月-日 时:分)
	 * 
	 * @param date
	 * @return
	 */
	public static String getDate2MStr(java.util.Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm");
		String formatDate = null;
		if (date != null) {
			formatDate = simpleDateFormat.format(date);
		}
		return formatDate;
	}

	/**
	 * 取得过去i天的时间
	 * 
	 * @param i
	 * @return
	 */
	public static String getPastdayDate(int i) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date now = CALENDAR.getTime();
		Date pastday = new Date(now.getTime() - i * 24 * 60 * 60 * 1000);
		String formatDate = simpleDateFormat.format(pastday);

		return formatDate;
	}

	/**
	 * 取得距当前时间i天的时间,i为负数时表示过去i天
	 * 
	 * @param i
	 * @return
	 */
	public static String getPastdayDate2(int i) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		CALENDAR.add(CALENDAR.DAY_OF_MONTH, i);// 距i天
		CALENDAR.add(CALENDAR.MONTH, i);// 距i月
		CALENDAR.add(CALENDAR.YEAR, i);// 距i年
		Date date = CALENDAR.getTime();
		String formatDate = simpleDateFormat.format(date);

		return formatDate;
	}

你可能感兴趣的:(java,sql)