一 概述
借助工具包实现,不同时间的处理:joda time
引入的类
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
引入的包
<!--Joda-time-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
二 不同时间方法实现
声明时间格式
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** * 时间格式(yyyy-MM-dd) */ private final static String DATE_PATTERN = "yyyy-MM-dd"; public static String today(String pattern) { Date date = new Date(); if (pattern == null) { pattern = STANDARD_FORMAT; } if (!pattern.equals(STANDARD_FORMAT) && !pattern.equals(DATE_PATTERN)) { return StringUtils.EMPTY; } return format(date, pattern); }
/** * 日期格式化 日期格式为:yyyy-MM-dd * * @param date 日期 * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN * @return 返回yyyy-MM-dd格式日期 */ public static String format(Date date, String pattern) { if (date != null) { SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(date); } return null; }
date类型 -> string类型
public static String dateToStr(Date date) { if (date == null) return StringUtils.EMPTY; DateTime dateTime = new DateTime(date); return dateTime.toString(STANDARD_FORMAT); }or
public static String dateToStr(Date date, String formatPattern) { if (date == null) return StringUtils.EMPTY; DateTime dateTime = new DateTime(date); return dateTime.toString(formatPattern); }
string类型 -> date类型
public static Date strToDate(String timeStr) { DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT); DateTime dateTime = dateTimeFormatter.parseDateTime(timeStr); return dateTime.toDate(); }or
public static Date strToDate2(String timeStr) { DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATE_PATTERN); DateTime dateTime = dateTimeFormatter.parseDateTime(timeStr); return dateTime.toDate(); }
判断时间是否过期或者之前
public static boolean isTimeExpired(Date date) { if (null == date) return true; String timeStr = dateToStr(date); return isBeforeNow(timeStr); }or
private static boolean isBeforeNow(String timeStr) { DateTimeFormatter format = DateTimeFormat.forPattern(STANDARD_FORMAT); DateTime dateTime = DateTime.parse(timeStr, format); return dateTime.isBeforeNow(); } 判断是否为之前时间 /** * 判断timeStr是否在当前时刻之前 * * @param timeStr * @return */ private static boolean isBeforeNow(String timeStr) { DateTimeFormatter format = DateTimeFormat.forPattern(STANDARD_FORMAT); DateTime dateTime = DateTime.parse(timeStr, format); return dateTime.isBeforeNow(); }
日期加/减月份
/** * 日期加月份 * * @param date * @param months * @return */ public static Date plusMonths(Date date, Integer months) { return plusOrMinusMonths(date, months, 0); } /** * 日期减月份 * * @param date * @param months * @return */ public static Date minusMonths(Date date, Integer months) { return plusOrMinusMonths(date, months, 1); } /** * 加减月份 * * @param date * @param months * @param type 0:加月份 1:减月份 * @return */ private static Date plusOrMinusMonths(Date date, Integer months, Integer type) { if (null == date) return null; months = null == months ? 0 : months; DateTime dateTime = new DateTime(date); if (type == 0) { dateTime = dateTime.plusMonths(months); } else { dateTime = dateTime.minusMonths(months); } return dateTime.toDate(); }
日期加/减少天数
/** * 日期加天数 * * @param date * @param days * @return */ public static Date plusDays(Date date, Integer days) { return plusOrMinusDays(date, days, 0); } /** * 日期减天数 * * @param date * @param days * @return */ public static Date minusDays(Date date, Integer days) { return plusOrMinusDays(date, days, 1); } /** * 当日减天数 * * @param days * @return */ public static String minusDays(Integer days, String formatPattern) { Date date = new Date(); if (formatPattern == null) return StringUtils.EMPTY; return format(plusOrMinusDays(date, days, 1), formatPattern); } /** * 加减天数 * * @param date * @param days * @param type 0:加天数 1:减天数 * @return */ private static Date plusOrMinusDays(Date date, Integer days, Integer type) { if (null == date) return null; days = null == days ? 0 : days; DateTime dateTime = new DateTime(date); if (type == 0) { dateTime = dateTime.plusDays(days); } else { dateTime = dateTime.minusDays(days); } return dateTime.toDate(); }
日期加/减分钟
/** * 日期加分钟 * * @param date * @param minutes * @return */ public static Date plusMinutes(Date date, Integer minutes) { return plusOrMinusMinutes(date, minutes, 0); } /** * 日期减分钟 * * @param date * @param minutes * @return */ public static Date minusMinutes(Date date, Integer minutes) { return plusOrMinusMinutes(date, minutes, 1); } /** * 加减分钟 * * @param date * @param minutes * @param type 0:加分钟 1:减分钟 * @return */ private static Date plusOrMinusMinutes(Date date, Integer minutes, Integer type) { if (null == date) return null; minutes = null == minutes ? 0 : minutes; DateTime dateTime = new DateTime(date); if (type == 0) { dateTime = dateTime.plusMinutes(minutes); } else { dateTime = dateTime.minusMinutes(minutes); } return dateTime.toDate(); }
判断是否在某一时间段内
/** * 判断target是否在开始和结束时间之间 * * @param target * @param startTime * @param endTime * @return */ public static boolean isBetweenStartAndEndTime(Date target, Date startTime, Date endTime) { if (null == target || null == startTime || null == endTime) { return false; } DateTime dateTime = new DateTime(target); return dateTime.isAfter(startTime.getTime()) && dateTime.isBefore(endTime.getTime()); }
判断当前是为第几周
/** * 获得当前是周几 * @return */ public static Integer getWeekday(LocalDate currentDate) { if (currentDate == null) { currentDate = LocalDate.now(); } Integer weekday = currentDate.getDayOfWeek().getValue(); return weekday; }public static int compareDay( Date date1, Date date2) { if ((date1 == null) || (date2 == null)) { return 0; } long time1 = date1.getTime(); long time2 = date2.getTime(); long margin = time1 - time2; int ret = (int) Math.floor(margin / 86400000.0D); return ret; }
判断两个日期之间的间隔
public static int getDaysBetweenTwoDate(String firstString, String secondString) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date firstDate = null; Date secondDate = null; try { firstDate = df.parse(firstString); secondDate = df.parse(secondString); } catch (Exception e) { // 日期型字符串格式错误 System.out.println("日期型字符串格式错误"); } return (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000)); }
三 使用案例
//定义时间格式
String beginTime = DateTimeUtil.today("yyyy-MM-dd HH:mm:ss");
//7200分钟
Integer defBeginMinutes = 7200;
//获取当前时间前7200分钟的时间
beginTime=DateTimeUtil.dateToStr(DateTimeUtil.minusMinutes(DateTimeUtil.strToDate(beginTime), defBeginMinutes));