时间计算工具类

package com.jzfq.fms.common.util; import com.jzfq.fms.common.common.PageVo; import com.jzfq.fms.common.util.DateEnum; import com.jzfq.fms.common.util.ServiceValidate;

import org.apache.commons.lang.StringUtils; import org.joda.time.DateTime; import org.joda.time.Days; import org.joda.time.LocalDate; import org.joda.time.Months; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Map;

/**

  • @author zhishuo 时间计算工具类 */ public final class DateUtils {

    /**

    • 获取当前时间
    • @return 返回java Date
    • @author zhishuo */ public static Date getNow() { return new DateTime().toDate(); }

    public static Date truncate(Date date, int field){ return org.apache.commons.lang.time.DateUtils.truncate(date, field); }

    /**

    • 获取当前时间
    • @return 返回JODA DateTime
    • @author zhishuo */ public static DateTime getDateTimeNow() { return new DateTime(); }

    public static DateTime dateToDateTime(Date date) { return new DateTime(date); }

    /**

    • 字符串时间 转换为时间
    • @param date
    • @return
    • @author zhishuo */ public static Date toDate(String date, DateEnum format) { ServiceValidate.notNull(format); DateTimeFormatter fmt = DateTimeFormat.forPattern(format.getText()); return fmt.parseDateTime(date.trim()).toDate(); }

    /**

    • 毫秒转换为Date
    • @param ms
    • @return
    • @author zhishuo */ public static Date msToDate(long ms) { ServiceValidate.notNull(ms); return new DateTime(ms).toDate(); }

    /**

    • 格式化时间
    • @param date 时间
    • @param template 格式
    • @return
    • @author zhishuo */ public static String dateToStr(Date date, DateEnum template) { ServiceValidate.notNull(date); ServiceValidate.notNull(template); SimpleDateFormat format = new SimpleDateFormat(template.getText()); return format.format(date); }

    /**

    • 传入日期增加几天
    • @param date
    • @param day
    • @return
    • @author zhishuo */ public static Date plusDay(Date date, int day) { ServiceValidate.notNull(date); return new DateTime(date).plusDays(day).toDate(); }

    /**

    • 传入日期增加几个月
    • @param date
    • @param
    • @return
    • @author zhishuo */ public static Date plusMonth(Date date, int month) { ServiceValidate.notNull(month); return new DateTime(date).plusMonths(month).toDate(); }

    /**

    • 传入日期减少几天
    • @param date
    • @param day
    • @return
    • @author zhishuo */ public static Date minusDay(Date date, int day) { ServiceValidate.notNull(date); return new DateTime(date).minusDays(day).toDate(); }

    /**

    • 传入日期减少月数
    • @param date
    • @param month
    • @return */ public static Date minusMonth(Date date, int month) { ServiceValidate.notNull(date); return new DateTime(date).minusMonths(month).toDate(); }

    /**

    • 传入日期减少几分钟
    • @param date
    • @param
    • @return */ public static Date minusMinutes(Date date, int minutes) { ServiceValidate.notNull(date); return new DateTime(date).minusMinutes(minutes).toDate(); }

    public static Date longToDate(long millis) { return new DateTime(millis).toDate(); }

    /**

    • 返回传入两个date相差的天数
    • END 大于 start 忽略时分秒
    • 例:start = 2015-06-05 10:40:30
    • end = 2015-06-06 10:40:20
    • 返回值 1
    • @param start
    • @param end
    • @return */ public static int daysBetween(Date start, Date end) { int days = Days.daysBetween(new LocalDate(start), new LocalDate(end)).getDays(); return days; }

    /**

    • 返回两个日期之间 月数
    • @param start
    • @param end
    • @return
    • @author zhishuo */ public static int monthsBetween(Date start, Date end) { return monthsBetween(new DateTime(start), new DateTime(end)); }

    /**

    • 返回两个日期之间 月数
    • @param start
    • @param end
    • @return
    • @author zhishuo */ public static int monthsBetween(DateTime start, DateTime end) { int m = Months.monthsBetween(start, end).getMonths(); return m; }

    /**

    • 返回当前日期距离N月之后之后的天数
    • <p>

    • 例如:计算1月5日,距离2个月之后的3月5日 返回相差天数
    • @param date
    • @param addmonths
    • @return */ public static int daysAfterMonths(Date date, int addmonths) { DateTime dateTime = new DateTime(date); DateTime dateTimeAfter = dateTime.plusMonths(addmonths); return daysBetween(date, dateTimeAfter.toDate()); }

    /**

    • 返回传入日期+N月之后的日期
    • @param date
    • @param addmonths
    • @return */ public static Date dateAfterMonths(Date date, int addmonths) { DateTime dateTime = new DateTime(date); DateTime dateTimeAfter = dateTime.plusMonths(addmonths); return dateTimeAfter.toDate(); }

    public static Date handleDateMix(Date date) { String str = DateUtils.dateToStr(date, DateEnum.DATE_SIMPLE) + " 00:00:00"; return DateUtils.toDate(str, DateEnum.DATE_FORMAT); }

    public static Date handleDateMax(Date date) { String str = DateUtils.dateToStr(date, DateEnum.DATE_SIMPLE) + " 23:59:59"; return DateUtils.toDate(str, DateEnum.DATE_FORMAT); }

    /**

    • 传入日期和当前日期比较(参数会转换成年月日格式)
    • @param dt
    • @return 小于-1,等于0,大于1 */ public static int compareDateToNow(DateTime dt) { return dt.toLocalDate().compareTo(new DateTime().toLocalDate()); }

    /**

    • 返回当前时间格式yyyyMMdd字符串
    • @return */ public static String getSimpleDate() { return dateToStr(getNow(), DateEnum.DATE_SIMPLE_MIN); }

    /**

    • 返回当前时间格式HHmmss字符串
    • @return */ public static String getMinTime() { return dateToStr(getNow(), DateEnum.DATE_TIME_MIN); }

    /**

    • @param date
    • @return 当前月第几天 */ public static int getDayOfMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_MONTH); }

    public static Date getDate(String str, DateEnum format) { SimpleDateFormat sdf = new SimpleDateFormat(format.getText()); Date date = null; try { date = sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; } /**

    • 日期处理30天工具类,天数大于30天和开始时间大于结束时间,有前端处理 */

    public static PageVo getNewOrderTimeAndOrderTimeEnd(Map<String, String[]> map) { String orderTime = null; String orderTimeEnd = null; PageVo pageVo = new PageVo(); for (Map.Entry<String, String[]> entry : map.entrySet()) { if (entry.getValue() != null) { if (entry.getKey().equals("orderTime")) {//开始时间不为空 orderTime = entry.getValue()[0]; } if (entry.getKey().equals("orderTimeEnd")) {//开始时间不为空 orderTimeEnd = entry.getValue()[0]; } pageVo.getParameters().put(entry.getKey(), entry.getValue()[0]); } } if (StringUtils.isBlank(orderTime) && StringUtils.isBlank(orderTimeEnd)) { orderTimeEnd = getSimpleDate();//如果不选择时间,默认导出最近30天的 orderTime = dateToStr(plusMonth(new Date(), -1), DateEnum.DATE_SIMPLE);

     }
     if (StringUtils.isBlank(orderTime) && StringUtils.isNotBlank(orderTimeEnd)) {//开始时间为空,结束时间不为空
         orderTime = dateToStr(plusMonth(toDate(orderTimeEnd, DateEnum.DATE_SIMPLE), -1), DateEnum.DATE_SIMPLE);
     }
     if (StringUtils.isNotBlank(orderTime) && StringUtils.isBlank(orderTimeEnd)) {//结束时间为空,开始时间不为空
         orderTimeEnd = dateToStr(plusMonth(toDate(orderTime, DateEnum.DATE_SIMPLE), 1), DateEnum.DATE_SIMPLE);
     }
     pageVo.getParameters().put("orderTime", orderTime);
     pageVo.getParameters().put("orderTimeEnd", orderTimeEnd);
     return pageVo;
    

    }

    public static Integer getMaxDayOfMonth(Date date){ date = truncate(date, Calendar.MONTH); date = plusMonth(date, 1); date = plusDay(date, -1); return date.getDate(); }

    public static boolean checkValidDate(String str,DateEnum dateEnum) { SimpleDateFormat format = new SimpleDateFormat(dateEnum.getText()); format.setLenient(false); try { format.parse(str); return true; } catch (ParseException e) { //如果throw java.text.ParseException或者NullPointerException,就说明格式不对 return false; } }

}

转载于:https://my.oschina.net/u/3229047/blog/1502573

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值