2つの日付の差を計算

1400 ワード

小機能、直接コードを貼って、みんなが知っていると信じています.2つの日付間の差の計算に使用
package demo.datecalculate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

	/**
	 * @param first
	 * @param second
	 * @return
	 * @throws Exception
	 *  
	 */
	public static long calculate(Date first,Date second) throws Exception{
		long result = 0;
		long sub = first.getTime() - second.getTime();
		System.out.println(sub);
		result = sub/(3600*24*1000);
		return result;
	}
	
	/**
	 * @param dateStr
	 * @param pattern
	 * @return
	 * @throws ParseException
	 *  pattern 
	 */
	public static Date parse(String dateStr, String pattern) throws ParseException{
		Date resDate = null;
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		resDate = format.parse(dateStr);
		return resDate;
	}
	
	public static void main(String[] args) throws Exception{
		String first = "20130809";
		String second = "20130807";
		Date fDate = parse(first, "yyyyMMdd");
		Date sDate = parse(second, "yyyyMMdd");
		System.out.println(" "+calculate(fDate,sDate)+" ");
	}

}