日期操作

package com.saicfc.pmpf.common.utils;

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

/**
 * 日期操作辅助类
 * 
 * @author lizhiyong
 * 
 */
public class DateUtils {

	
	/**
	 * 根据传入的日期格式自动转换参数日期
	 * @param format
	 *          日期格式
	 * @param date
	 *          日期
	 * @return
	 * @throws ParseException
	 */
	public static final String convertDateToString(String format, Date date){
		SimpleDateFormat fm = new SimpleDateFormat(format);
		String strDate = null;
		try {
			strDate = fm.format(date);
		} catch (Exception e) {
			e.getStackTrace();
		}
		return strDate;
	}
	
	/**
	 * format 'yyyy/MM/dd'
	 * @param date
	 * @return strDate
	 */
    public static final String convertDateToStringFormat(Date date) {
        String strDate = null;
        try {
            strDate = convertDateToString("yyyy/MM/dd", date);
        } catch (Exception e) {
			e.getStackTrace();
		}
        return strDate;
    }
    
    /**
     * format 'yyyy-MM-dd'
     * @param date
     * @return strDate
     */
    public static final String convertDateToStringFromPage(Date date) {
        String strDate = null;
        try {
            strDate = convertDateToString("yyyy-MM-dd", date);
        } catch (Exception e) {
			e.getStackTrace();
		}
        return strDate;
    }
    
    /**
     * format 'HH:mm:ss'
     * @param date
     * @return strDate
     */
    public static final String convertDateToHourStringFormat(Date date){
    	String strDate = null;
    	try {
			strDate = convertDateToString("HH:mm:ss",date);
		} catch (Exception e) {
			e.getStackTrace();
		}
    	return strDate;
    }
    
    /**
     * format 'yyyy-MM-dd HH:mm:ss'
     * @param date
     * @return strDate
     */
    public static final String convertDateToFullFormat(Date date){
    	String strDate = null;
    	try {
			strDate = convertDateToString("yyyy/MM/dd HH:mm:ss", date);
		} catch (Exception e) {
			e.getStackTrace();
		}
    	return strDate; 
    }
    
       /**
     * 把String类型转化成Date类型的数据
     * @param strDate
     *               要转化的日期字符串
     * @param format
     *              转化的格式
     * @return date
     *        转化后的Date日期
     */
    public static final Date stringToDateFormat(String strDate,String format){
    	SimpleDateFormat fmt =new SimpleDateFormat(format);
        Date date;
		try {
			date = fmt.parse(strDate);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
    } 
    
    
    /**
     * 求两个日期之间相差的天数
     * @param startTime
     *               第一个日期
     * @param endTime
     *               第二个日期
     * @return date1 与 date2 之间相差的天数
     */
    public static final int dateDiff(Date startTime, Date endTime) {
    	return (int)((startTime.getTime() - endTime.getTime()) / (24 * 60 * 60 * 1000));
    }
    
    /**
     * 求两个日期之间相差的小时数
     * @param startTime
     *                第一的日期
     * @param endTime
     *               第二的日期
     * @return  两个日期之间相差的小时数
     */
    public static final int hourDiff(Date startTime, Date endTime){
    	return (int)((startTime.getTime() - endTime.getTime()) / (60 * 60 * 1000));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值