日期工具类
package com.payease.chains.risk.utils; import org.apache.commons.lang.time.DateUtils; import java.text.Format; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @Author : zhangwen * @Data : 2017/9/18 * @Description : */ public class DateUtil { //日期时间格式 public static String FORMAT_PATTERN_DATE = "yyyy-MM-dd"; //时分秒时间格式 public static String FORMAT_PATTERN_TIME = "yyyy-MM-dd HH:mm:ss"; /** * @Author zhangwen * @MethodName getDaySpace * @Params [fromDay, toDay] * @Return int * @Date 2017/9/18 上午10:38 * @Desp 获取两个日期的时间差值 */ public static int getDaySpace(Date fromDay, Date toDay) throws Exception { SimpleDateFormat simpleFormat = new SimpleDateFormat(FORMAT_PATTERN_TIME); String fromDate = simpleFormat.format(fromDay); String toDate = simpleFormat.format(toDay); long from = simpleFormat.parse(fromDate).getTime(); long to = simpleFormat.parse(toDate).getTime(); int days = (int) ((to - from) / (1000 * 60 * 60 * 24)); return days; } /** * @Author zhangwen * @MethodName getNow * @Params [pattern] * @Return java.util.Date * @Date 2017/9/18 上午10:40 * @Desp 获取系统当前时间 */ public static Date getNow(String pattern) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.parse(sdf.format(new Date())); } /** * @Author zhangwen * @MethodName parseDate * @Params [parseStr, pattern] * @Return java.util.Date * @Date 2017/9/18 上午10:45 * @Desp 格式化时间 返回时间类型 */ public static Date parseDate(String parseStr, String pattern) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.parse(parseStr); } /** * @Author zhangwen * @MethodName formatDate * @Params [date, pattern] * @Return java.lang.String * @Date 2017/9/18 下午4:36 * @Desp 时间格式化 返回字符串类型 */ public static String formatDate(Date date, String pattern) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(date); } public static void main(String[] args) { try { Date from = parseDate("2017-06-12", FORMAT_PATTERN_DATE); Date now = getNow(FORMAT_PATTERN_DATE); System.out.println(getDaySpace(from, now)); } catch (Exception e) { e.printStackTrace(); } } /** * 得到一天的最后一秒钟 * * @param d * @return * @Author lch */ public static Date endOfDay(Date d) { return DateUtils.addSeconds( DateUtils.addDays(DateUtils.truncate(d, Calendar.DATE), 1), -1); } }