date 日期工具类

/**
* 两个日期相减获得月份
* @param startCal
* @param endCal
* @return
* @throws ParseException
*/
public static int twoDatagetmonth(String startCal,String endCal) throws ParseException{

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Date startDate = sdf.parse(startCal);
            Date endDate = sdf.parse(endCal);
            Calendar startCalendar = Calendar.getInstance();
            Calendar endCalendar = Calendar.getInstance();

            startCalendar.setTime(startDate);
            endCalendar.setTime(endDate);



            int day = endCalendar.get(Calendar.DAY_OF_MONTH)
                    - startCalendar.get(Calendar.DAY_OF_MONTH);
            int month = endCalendar.get(Calendar.MONTH)
                    - startCalendar.get(Calendar.MONTH);
            int year = endCalendar.get(Calendar.YEAR)
                    - startCalendar.get(Calendar.YEAR);

            if (day < 0) {
                month--;
            }
            if (year>0) {
                month +=year*12;
            }

            if (month < 0) {
                month += 12;
                year--;
            }
            return month;

}

/**
* date日期转换为timestamp
* @param time
* @return
* @throws ParseException
*/
public static Timestamp formatTimestamp(Date time) throws ParseException{

    //SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //Date d = (Date) f.parseObject(time);
    Timestamp ts = new Timestamp(time.getTime());
    //System.out.println(ts);
    return ts;
}

/**
* yyyy-MM-dd HH:mm:ss
* @param time
* @return
* @throws ParseException
*/
public static Timestamp formatTimestamp(String time) throws ParseException{

    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = (Date) f.parseObject(time);
    Timestamp ts = new Timestamp(d.getTime());
    //System.out.println(ts);
    return ts;
}

/**
* 几天后
* @param disday
* @return
*/
public static Timestamp getTimestamp(int disday) {
Calendar c;
c = Calendar.getInstance();
long realtime = c.getTimeInMillis();
realtime += 86400000 * disday;
return new Timestamp(realtime);
}

/**
* 判断日期是否过期
* @author yulong
* @description
* @date 2012-8-25
* @param date 生产日期
* @param minute 分钟
* @return
*/
public static boolean dateIsExpire(Date date,int minute){
Date expireDate=addMinute(date,minute);
if(expireDate.after(new Date())){
return false;
}
return true;
}

/**
* 增加或减少数分钟
* @author yulong
* @description
* @date 2012-8-25
* @param date
* @param n
* @return
*/
public static Date addMinute(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, n);
return cal.getTime();
}

/**
* 增加或减少数小时
* @author yulong
* @description
* @date 2012-8-25
* @param date
* @param n
* @return
*/
public static Date addHour(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, n);
return cal.getTime();
}
/*
*计算出多久之前 如:1小时前 一个月前
*
*/
public class PastTime {

 private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "个月前";
    private static final String ONE_YEAR_AGO = "年前";

 public static void main(String[] args) throws ParseException  {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse("2013-11-11 18:35:35");
        System.out.println(format(date));
    }

    public static String format(Date date) {
        long delta = new Date().getTime() - date.getTime();
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天";
        }
        if (delta < 30L * ONE_DAY) {
            long days = toDays(delta);
            return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
        }
        if (delta < 12L * 4L * ONE_WEEK) {
            long months = toMonths(delta);
            return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
        } else {
            long years = toYears(delta);
            return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
        }
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值