时间的相关操作

public class Time {

    /**
     * 计算两个时间相差的天数
     * @param beforeTime 前一个时间点
     * @param afterTime 后一个时间点
     * @return 相差的天数
     * calendar 对日期进行时间操作
     * getTimeInMillis() 获取日期的毫秒显示形式
     */
    public static int getTimeInterval(Date beforeTime, Date afterTime){
        Calendar cal = Calendar.getInstance();
        cal.setTime(beforeTime);
        long time1 = cal.getTimeInMillis();
        cal.setTime(afterTime);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
	 * 
	 * @方法名:getLastDayOfMonth
	 * @描述:获取某月的最后一天
	 * @作者:lgm
	 * @时间:2019-10-29 09:41:21
	 * @参数:@param date
	 * @参数:@return 
	 * @返回值:Date
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		calendar.roll(Calendar.DATE, -1);
		return calendar.getTime();
	}

	/**
	 * 
	 * @方法名:getTimeInterval
	 * @描述:计算机两个时间间隔几天
	 * @作者:lgm
	 * @时间:2019-10-29 09:48:07
	 * @参数:@param beforeTime
	 * @参数:@param afterTime
	 * @参数:@return 
	 * @返回值:int
	 */
	public static int getTimeInterval(String beforeTime, String afterTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		int days = 0;
		try {
			Date date1 = format.parse(beforeTime);
			Date date2 = format.parse(afterTime);
			days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days;
	}

	/**
	 * 
	 * @方法名:getDaysOfMonth
	 * @描述:获取某月有多少天
	 * @作者:lgm
	 * @时间:2019-10-29 09:40:27
	 * @参数:@param date
	 * @参数:@return 
	 * @返回值:int
	 */
	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lgm灵均

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值