一、maven依赖

        <!--joda time-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10</version>
</dependency>

二、JodaTimeUtil

package com.sso.util;

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.util.Date; public class JodaTimeUtil { private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss"; /**
* date类型 -> string类型
*
* @param date
* @return
*/
public static String dateToStr(Date date) {
if (date == null) return StringUtils.EMPTY;
DateTime dateTime = new DateTime(date);
return dateTime.toString(STANDARD_FORMAT);
} /**
* date类型 -> string类型
*
* @param date
* @param formatPattern
* @return
*/
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类型
*
* @param timeStr
* @return
*/
public static Date strToDate(String timeStr) {
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
DateTime dateTime = dateTimeFormatter.parseDateTime(timeStr);
return dateTime.toDate();
} /**
* 判断date日期是否过期(与当前时刻比较)
*
* @param date
* @return
*/
public static boolean isTimeExpired(Date date) {
if (null == date) return true;
String timeStr = dateToStr(date);
return isBeforeNow(timeStr);
} /**
* 判断date日期是否过期(与当前时刻比较)
*
* @param timeStr
* @return
*/
public static boolean isTimeExpired(String timeStr) {
if (StringUtils.isBlank(timeStr)) return true;
return isBeforeNow(timeStr);
} /**
* 判断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 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 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();
} /**
* 日期加月份
*
* @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();
} /**
* 判断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());
} }

最新文章

  1. Exploratory Undersampling for Class-Imbalance Learning
  2. C#在Dictionary中使用枚举作为键
  3. JavaScript的Ajax请求示例
  4. eclipse根据父类打开子类快捷键
  5. 初学web开发——怎么解决无法找到路径的问题
  6. C语言 后缀自增的优先级详解
  7. mongoDB安装学习
  8. Windows server 共享文件夹权限设置
  9. 1654 方程的解 - Wikioi
  10. jQuery 遍历祖先
  11. ModelAndView解析
  12. 有关于/home出现100%被占用 与 vnc的关系
  13. ES6-模块化
  14. DIV+CSS特殊符号的处理方法
  15. 字符串API
  16. iOS NSInteger 的输出 %d %ld %zd %ld (long)
  17. MongoDB安装之window版本的安装
  18. Git:八、Git自定义:忽略特殊文件&amp;配置别名
  19. iOS开发基础-序列帧动画之Tom猫
  20. linux 添加环境变量(php为例)

热门文章

  1. [leetcode]88. Merge Sorted Array归并有序数组
  2. 字符串转码中文乱码问题的进一步理解 UTF-8 GBK转码
  3. ArrayList 初探
  4. 学习blinker
  5. [C#]GetFloat提示&quot;指定的转换无效&quot;
  6. MFC中List控件动态填充数据(LVN_GETDISPINFO)
  7. 安装php7.2并且整合nginx且分开部署
  8. pop
  9. Beta冲刺 (1/7)
  10. [算法专题] LinkedList