日期和时间的工具类

来源:互联网 发布:东南大学软件学院学费 编辑:程序博客网 时间:2024/05/23 21:13

日期和时间的工具类

public class DateTimeUtil {    /***     * 将时间戳转换为日期时间     * @param timestamp long类型的时间     * @param format yyyy-MM-dd HH:mm:ss/yyyy-MM-dd/HH:mm:ss     * @return yyyy-MM-dd HH:mm:ss/yyyy-MM-dd/HH:mm:ss     */    public static String stampToDateTime(long timestamp,String format) {        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);        Date date = new Date(timestamp);        return simpleDateFormat.format(date);    }     /***     * 计算两个时间点相差了几分钟     * @param strDateBegin 开始的时间点,格式必须是HH:mm:ss     * @param strDateEnd 结束的时间点,格式必须是HH:mm:ss     * @return 相差的时间分钟     */    public static int calc2TimesIntervalMinutes(String strDateBegin, String strDateEnd) {        DateFormat df = new SimpleDateFormat("HH:mm:ss");// 创建日期转换对象HH:mm:ss为时分秒,年月日为yyyy-MM-dd        try {            Date beginTime = df.parse(strDateBegin);// 将字符串转换为date类型            Date endTime = df.parse(strDateEnd);            long interval = endTime.getTime() - beginTime.getTime();            return (int) (interval / 60000);        } catch (Exception e) {            e.printStackTrace();        }        return 0;    }    /**     * 获取当前系统时间     * @param format 获取的时间格式 ,如: yyyy-MM-dd HH:mm:ss     * @return 对应格式的时间字符串, 如:2014-08-07 12:11:00     */    public static String getCurrentDateTime(String format) {        SimpleDateFormat dateFormat = new SimpleDateFormat(format);        try {            Calendar cal = Calendar.getInstance();            return dateFormat.format(cal.getTime());        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    /**     * 指定时间加上天数后的时间     * @param days 为增加的天数,可以为负数     */    public static long plusDay(long timeMillis, int days) {        Date date = new Date(timeMillis);        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.add(Calendar.DATE, days);        return calendar.getTimeInMillis();    }    /**     * 判断时间是否在时间段内     * @param timeNeedJude HH:mm:ss 待判断的时间     * @param strDateBegin 24小时制,开始时间 00:00:00     * @param strDateEnd   24小时制,结束时间 00:05:00     * @return true-在时间段内,反之false     */    public static boolean isInDate(String timeNeedJude, String strDateBegin, String strDateEnd) {        DateFormat df = new SimpleDateFormat("HH:mm:ss");        try {            Date beginTime = df.parse(strDateBegin);            Date endTime = df.parse(strDateEnd);            Date nowTimeDate = df.parse(timeNeedJude);            if (beginTime.getTime() <= nowTimeDate.getTime() && nowTimeDate.getTime() <= endTime.getTime()) {                return true;            }        } catch (Exception e) {            e.printStackTrace();        }        return false;    }     /***     * 计算两个时间相差了几个月分     * @param t1 时间     * @param t2 时间     * @return t1到t2相差的月数     */    public static int intervalMonths(long t1, long t2){        Calendar calendar = Calendar.getInstance();        Date date1 = new Date(t1);        Date date2 = new Date(t2);        calendar.setTime(date1);        int year1 = calendar.get(Calendar.YEAR);        int month1 = calendar.get(Calendar.MONTH);        calendar.setTime(date2);        int year2 = calendar.get(Calendar.YEAR);        int month2 = calendar.get(Calendar.MONTH);        if(year1 == year2){            int ms = month1-month2;            return Math.abs(ms);        }else {            int ys = Math.abs(year1-year2);//相差了几年            if(year1 > year2){                return ys*12 - month2 + month1;            }else{                return ys*12 - month1 + month2;            }        }    }    /**     * 计算两个时间戳间隔几天     * @param beginTimeMillis 开始时间     * @param endTimeMillis 结束时间     */    public static int intervalDays(long beginTimeMillis, long endTimeMillis) {        long difftime = endTimeMillis - beginTimeMillis;        long adayTimeMillis = 86400000;// 一天的毫秒数        return (int) (difftime / adayTimeMillis) + 1;    }}
原创粉丝点击