1. 程式人生 > >日期工具類(獲取兩日期之前相差天數及String/Timestamp/Date物件之前的日期轉換)

日期工具類(獲取兩日期之前相差天數及String/Timestamp/Date物件之前的日期轉換)

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



public class DateUtil {
	
	public static void main(String[] args) throws ParseException {
		String start = "2013-11-27 00:00:00.0";
		String end = "2013-11-30 00:00:00.0";
		Timestamp costTime = Timestamp.valueOf(start); //收款時間
		Timestamp receivableTime = Timestamp.valueOf(end);//付款時間
		Timestamp curentTime = new Timestamp(System.currentTimeMillis());
		
		Timestamp test = Timestamp.valueOf("2013-12-14 00:00:00.0");
		System.out.println(getTrueDays(curentTime,test));
		
		int days = getDays(receivableTime,costTime);
		System.out.println("相差["+days+"]天");
		
		long str = 1385354603827L;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(str);
		System.out.println(format.format(calendar.getTime()));
		System.out.println(System.currentTimeMillis());
		
	}
	
	
	/**
	 * 計算兩個日期之間相差的天數
	 * @param costTime
	 * @param receivableTime
	 * @return
	 */
	public static int getDays(Timestamp end,Timestamp start){
		Calendar aCalendar = Calendar.getInstance();
        Calendar bCalendar = Calendar.getInstance();
        aCalendar.setTime(end);
        bCalendar.setTime(start);
        int days = 0;
        while(aCalendar.before(bCalendar)){
            days++;
            aCalendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        
        if(days==0){
        	aCalendar.setTime(start);
            bCalendar.setTime(end);
            while(aCalendar.before(bCalendar)){
                days++;
                aCalendar.add(Calendar.DAY_OF_YEAR, 1);
            }
        }
        return days;
	}
	
	/**
	 * 計算兩個日期之間相差的天數
	 * @param costTime
	 * @param receivableTime
	 * @return
	 */
	public static int getTrueDays(Timestamp end,Timestamp start){
		Calendar aCalendar = Calendar.getInstance();
        Calendar bCalendar = Calendar.getInstance();
        aCalendar.setTime(end);
        bCalendar.setTime(start);
        int days = 0;
        while(aCalendar.before(bCalendar)){
            days++;
            aCalendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
	}
	
	/**
	 * 將字串轉換為Timestamp型別
	 * @param str 日期字串
	 * @return Timestamp型別物件
	 */
	public static Timestamp stringToTimestamp(String str){
        return Timestamp.valueOf(str);   
	}
	
	/**
	 * 將Timestamp型別物件轉換為指定格式的字串
	 * @param timestamp Timestamp型別物件
	 * @param format 字串格式
	 * @return String字串
	 */
	public static String timestampToString(Timestamp timestamp,String format){
        DateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(timestamp);  
	}
	
	/**
	 * 將Timestamp型別物件轉換為Date型別物件
	 * @param timestamp Timestamp型別物件
	 * @return Date型別物件
	 */
	public static Date timestampToDate(Timestamp timestamp){
        Date date = new Date();  
        date = timestamp;  
        return date;
	}
	
	/**
	 * 將Date型別物件轉換為Timestamp型別物件
	 * @param date Date型別物件
	 * @return Timestamp型別物件
	 */
	public static Timestamp dateToTimestamp(Date date){
		return new Timestamp(date.getTime());
	}
	
	
	/**
	 * 將一個字串轉換為指定的日期格式,返回Date型別物件
	 * @param dateStr 日期字串
	 * @param format 日期格式
	 * @return Date型別物件
	 * @throws ParseException 轉換異常
	 */
	public static Date stringToDate(String dateStr,String format) throws ParseException{
		Date date = new Date();  
        //注意format的格式要與日期String的格式相匹配  
        DateFormat sdf = new SimpleDateFormat(format);  
        return sdf.parse(dateStr);  
	}
	
	/**
	 * 將一個Date型別物件轉換為指定格式的字串
	 * @param date Date型別物件
	 * @param format 指定的字串格式
	 * @return String字串
	 */
	public static String dateToString(Date date,String format){
        DateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(date);  
	}
	
}