记录几个日期处理的小方法

记录几个日期处理的小方法

1. 不同日期格式之间的转化
2. 计算两个日期相差的天数
......

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public final class DateUtil {

	private static final long MILLISECONDS_PER_DAY = 1L * 24 * 60 * 60 * 1000;

	private static final String DATE_FORMAT_YYYYMMDD = "yyyy-MM-dd";

	private static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

	private static final SimpleDateFormat df1 = new SimpleDateFormat(
			DATE_FORMAT_YYYYMMDD);

	private static final SimpleDateFormat df2 = new SimpleDateFormat(
			DATE_FORMAT_YYYYMMDDHHMMSS);

	private DateUtil() {
	}

	/**
	 * 根据给定的源日期的字符串内容和日期格式,返回一个以目标日期格式的字符串内容。
	 * 
	 * @param targetFormat
	 *            目标日期的格式
	 * @param sourceFormat
	 *            源日期的格式
	 * @param sourceDateString
	 *            源日期的字符串内容
	 * @return the string
	 * @throws ParseException
	 */
	public static String formatDateString(String targetFormat,
			String sourceFormat, String sourceDateString) {
		try {
			return new SimpleDateFormat(targetFormat)
					.format(new SimpleDateFormat(sourceFormat)
							.parse(sourceDateString));
		} catch (ParseException e) {
			// TODO
			return null;
		}
	}

	/**
	 * 返回两个日期相差的天数
	 */
	public static int getDiffInDays(Date startDate, Date endDate) {
		return (int) ((endDate.getTime() - startDate.getTime()) / MILLISECONDS_PER_DAY);
	}

	public static boolean isFutureDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.after(Calendar.getInstance());
	}

	public static boolean isPastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.before(Calendar.getInstance());
	}

	public static Date now() {
		return new Date();
	}

	public static Date addDay(Date date, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, days);
		return calendar.getTime();
	}

	public static Date addMonth(Date date, int months) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, months);
		return calendar.getTime();
	}

	public static Date addYear(Date date, int years) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, years);
		return calendar.getTime();
	}

	public static String formatToYYYYMMDD(Date date) {
		return df1.format(date);
	}

	public static String formatToYYYYMMDDHHMMSS(Date date) {
		return df2.format(date);
	}

	public static String getFormatDateTime(Date date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static boolean isLeapYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return isLeapYear(cal.get(Calendar.YEAR));
	}

	public static boolean isLeapYear(int year) {
		GregorianCalendar calendar = new GregorianCalendar();
		return calendar.isLeapYear(year);
	}

}

测试代码
import java.util.Date;

public class Test {
	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(DateUtil.formatToYYYYMMDD(date));
		System.out.println(DateUtil.formatToYYYYMMDDHHMMSS(date));
		System.out.println(DateUtil.formatDateString("yyyy-MM-dd",
				"MM/dd/yyyy", "06/30/2013"));
	}
}


测试输出结果如下:
2013-07-02
2013-07-02 12:33:27
2013-06-30

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