引入依赖
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9.9</version> </dependency>
使用 joda-time 线程安全
工具类:
import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.joda.time.Days; import org.joda.time.Minutes; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 日期帮助类,用于对日期以及字符串的相关操作 */ public class DateUtil { /** * compare two days * if first bigger than second return > 0 else return < 0 */ public static int compareDates(Date first, Date second) { if (first.getTime() > second.getTime()) { return 1; } else if (first.getTime() < second.getTime()) { return -1; } else { return 0; } } /** * 功能描述:返回分 * * @param date 日期 * @return 返回分钟 */ public static int getMinute(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MINUTE); } /** * 获取小时 * * @param date * @return */ public static int getHour(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.HOUR_OF_DAY); } /** * 字符串转换成日期 * "yyyy-MM-dd HH:mm:ss" * * @param str * @return date */ public static Date stringToDate(String str, String dateFormat) { Date date = null; try { if (StringUtils.isNotEmpty(str)) { DateTimeFormatter format = DateTimeFormat.forPattern(dateFormat); DateTime dateTime = DateTime.parse(str, format); date = dateTime.toDate(); } } catch (Exception e) { } return date; } public static Date stringToDate(String str) { Date date = null; try { if (StringUtils.isNotEmpty(str)) { DateTime dateTime = DateTime.parse(str); date = dateTime.toDate(); } } catch (Exception e) { } return date; } /** * 日期转换成字符串 * * @param date * @param strFormat * @return */ public static String dateToString(Date date, String strFormat) { DateTime dateTime = new DateTime(date); return dateTime.toString(strFormat); } /** * 获取 * * @param date * @param n * @return */ public static Date addDays(Date date, int n) { DateTime dateTime = new DateTime(date); return dateTime.plusDays(n).toDate(); //把日期往后增加n天.正数往后推,负数往前移动 } public static Date addYears(Date date, int var1) { DateTime dateTime = new DateTime(date); return dateTime.plusYears(var1).toDate(); //把日期往后增加n天.正数往后推,负数往前移动 } /** * 计算相差分钟 * * @param var0 * @param var1 * @return */ public static int minutesBetween(Date var0, Date var1) { if (var0 != null && var1 != null) return Minutes.minutesBetween(new DateTime(var0), new DateTime(var1)).getMinutes(); return 0; } /** * 计算相差天数 * * @param var0 * @param var1 * @return */ public static int daysBetween(Date var0, Date var1) { return Days.daysBetween(new DateTime(var0), new DateTime(var1)).getDays(); } /** * 获取星期 * * @param var1 * @return */ public static String getDayOfWeek(Date var1) { DateTime dateTime = new DateTime(var1); return dateTime.dayOfWeek().getAsString(); } /* * 生成日期对象 */ public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) { XMLGregorianCalendar gc = null; try { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); } catch (Exception e) { } return gc; } public static Calendar dateToCalendar(Timestamp timestamp) { Calendar calendar = Calendar.getInstance(); calendar.setTime(timestamp); return calendar; } }