日期类型的转换与加减

        工具类可直接通过类名调用方法,不需要创建一个对象来进行调用工具类的方法,因为该工具类里的方法都是静态的方法(static)

        静态方法,比较适合工具类、静态工厂等,必须考虑多线程问题。静态方法属于类本身,而非类的实例,所以可以用“类名.方法名”调用

        DateUtils time = new DateUtils();
        //计算相差天数 new Date() 获取当前日期
        int days = time.compareDay(new Date(), pay.getPlanPayTimechange());

        //计算相差天数 new Date() 获取当前日期
        int days = DateUtils.compareDay(new Date(), pay.getPlanPayTimechange());

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

/**
 * 日期处理
 * 
 * @author Howesen [email protected]
 * 
 */
public class DateUtils {
    /**
     * 长日期格式化:yyyy-MM-dd HH:mm:ss
     */
    public static String FORMATTER_L = "yyyy-MM-dd HH:mm:ss";
    /**
     * 公安部要求的日期格式
     */
    public static String DATESTR_FORMATTER_L = "yyyyMMddHHmmss";

    /**
     * 短日期格式化:yyyy-MM-dd
     */
    public static String FORMATTER_S = "yyyy-MM-dd";

    /**
     * 开始时间后缀
     */
    public static String DATE_BEGIN= "000000";
    /**
     * 结束时间后缀
     */
    public static String DATE_END = "235959";
	/**
	 * 自定义格式化日期输出
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String doFormatDate(Date date, String format) {
		return (new SimpleDateFormat(format)).format(date);
	}
    /**
	 * 将日期格式化为"yyyy-MM"字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String formatYearMonth(Date date) {
		return doFormatDate(date, "yyyy-MM");
	}
	/**
	 * 给一个日期加上N天或减去N天得到一个新的日期
	 * 
	 * @param startDate
	 *            需要增加的日期时间
	 * @param addnos
	 *            添加的天数,可以是正数也可以是负数
	 * @return 操作后的日期
	 */
	public static Date addDay(Date startDate, int addnos) {
		if (startDate == null)
			return null;
		Calendar cc = Calendar.getInstance();
		cc.setTime(startDate);
		cc.add(Calendar.DATE, addnos);
		return cc.getTime();
	}
	/**
	 * 给一个日期加上N月后或减去N月后得到的一个新日期
	 * 
	 * @param startDate
	 *            需要增加的日期时间
	 * @param addnos
	 *            添加的月数,可以是正数也可以是负数
	 * @return 操作后的日期
	 */
	public static Date addMonth(Date startDate, int addnos) {
		if (startDate == null)
			return null;
		Calendar cc = Calendar.getInstance();
		cc.setTime(startDate);
		cc.add(Calendar.MONTH, addnos);
		return cc.getTime();

	}
	/**
	 * 给一个日期加上N年后或减去N年后得到的一个新日期
	 * 
	 * @param startDate
	 *            需要增加的日期时间
	 * @param adddays
	 *            添加的年数,可以是正数也可以是负数
	 * @return 操作后的日期
	 */
	public static Date addYear(Date startDate, int addnos) {
		if (startDate == null)
			return null;
		Calendar cc = Calendar.getInstance();
		cc.setTime(startDate);
		cc.add(Calendar.YEAR, addnos);
		return cc.getTime();
	}
	/**
	 * 给一个日期加上N年后或减去N年后得到的一个新日期
	 * 
	 * @param startDate
	 *            需要增加的日期时间
	 * @param adddays
	 *            添加的年数,可以是正数也可以是负数
	 * @return 操作后的日期
	 */
	public static Date addYear(Date startDate, int addnos) {
		if (startDate == null)
			return null;
		Calendar cc = Calendar.getInstance();
		cc.setTime(startDate);
		cc.add(Calendar.YEAR, addnos);
		return cc.getTime();
	}

	/**
	 * 计算两个日期相差的月数
	 * 
	 * @param st
	 *            起始日期
	 * @param end
	 *            结束日期
	 * @return
	 */
	public static int compareMonth(Date st, Date end) {
		int y = Math.abs((getYear(end) < 0 ? 0 : getYear(end)) - (getYear(st) < 0 ? 0 : getYear(st)));
		int m = 0;
		if (y > 0) {
			y--;
			m = Math.abs(12 - getMonth(st) + getMonth(end));
		} else {
			m = Math.abs(getMonth(end) - getMonth(st));
		}
		return (y * 12) + m;
	}

	/**
	 * 
	 * compareDay:(计算两个日期相差的天数). <br/>
	 * @author zhenwang9
	 * @param start 
	 * @param end
	 * @return
	 * @since JDK 1.6
	 */
	public static int compareDay(Date start, Date end){
		
		if(start == null || end ==null || start.after(end) ){
			return 0;
		}else{
			long compare = compare(start,end);
			return (int) (compare/1000/3600/24);
		}
		
	}
	/**
	 * 计算两个日期相差的毫秒数
	 * 
	 * @param start
	 *            启始时间
	 * @param end
	 *            结束时间
	 * @return
	 */
	public static long compare(Date start, Date end) {
		if (start != null && end != null) {
			return end.getTime() - start.getTime();
		}
		return 0l;
	}
	/**
	 * 得到当前系统的日期或时间
	 * 
	 * @param b
	 *            为true 时返回详细时间格式,为false时返回日期格式,不含时分秒
	 * @return 当前的日期或时间
	 */
	public static String getDates(boolean b) {
		return doFormatDate(new Date(), b);
	}
    /**
     * 将java.util.Date类型的日期格式转换成java.util.Calendar格式的日期
     * 
     * @param dd
     * @return
     */
    public static Calendar DateToCalendar(Date dd)
    {
        Calendar cc = Calendar.getInstance();
        cc.setTime(dd);
        return cc;
    }
        /**
     * 将一个长整型数据转为日期
     * 
     * @param datenum
     * @return
     */
    public static Date longToDate(long datenum)
    {
        Calendar cc = Calendar.getInstance();
        cc.setTimeInMillis(datenum);
        return cc.getTime();
    }
    
    /**
     * 将一个长整型数据转为日期格式的字符串
     * 
     * @param datenum
     * @return
     */
    public static String longToDateString(long datenum)
    {
        return doFormatDate(longToDate(datenum), true);
    }
    /**
     * 将日期格式转为java.sql.Date
     * 
     * @param de
     * @return
     */
    public static java.sql.Date dateToSqlDate(Date de)
    {
        return new java.sql.Date(de.getTime());
    }
    
    /**
     * 格式化日期字符串 yyyymmddhh24miss
     * 
     * @param date
     * @return
     */
    public static String formatDS(String date)
    {
        if (date == null)
            return "";
        return date.replace("-", "").replace(":", "").replace(" ", "");
    }
    
    /**
     * 将公安部的时间格式转为日期格式
     * 
     * @param datestr
     * @return
     */
    public static Date strTdate(String datestr)
    {
        return str2Date(datestr, SystemConfig.DATESTR_FORMATTER_L);
    }
    
    /**
     * 将长时间格式转为日期格式
     * 
     * @param datestr
     * @return
     */
    public static Date strLTdate(String datestr)
    {
        return str2Date(datestr, SystemConfig.FORMATTER_L);
    }
    
    /**
     * 将短时间格式转为日期格式
     * 
     * @param datestr
     * @return
     */
    public static Date strSTdate(String datestr)
    {
        return str2Date(datestr, SystemConfig.FORMATTER_S);
    }
}


 

猜你喜欢

转载自blog.csdn.net/whz199511/article/details/81535391