jdk8之前使用jdk7的时间操作,并没有像jdk8新特性时间操作方便,也没有第三方开源框架joda-time方便,之前写过关于joda-time的总结,如果你想对joda-time
有更多的了解:不妨看一下这里:☞ joda-time快速入门学习
/**
* 时间工具类
* @author
* @version Id: DateUtil.java, v 0.1 2015/11/26 9:52 Exp $$
*/
public class DateUtil {
/** 锁对象 */
private static final Object lockObj = new Object();
/** 存放不同的日期模板格式的sdf的Map */
private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
/** 日期时间格式 **/
public static final String fullPattern = "yyyyMMddHHmmss";
public static final String settlePattern = "yyyy-MM-dd HH:mm:ss";
public static final String datePattern = "yyyyMMdd";
public static final String shotPattern = "yyyy-MM-dd";
public static final String obliqueShotPattern = "yyyy/MM/dd";
public static final String timePattern = "HHmmss";
public static final String timesPattern = "yyyy/MM/ddHH:mm:ss";
public static final String shortDatePattern = "yyMMdd";
public static final String fullPatterns = "yyyyMMddHHmmssSS";
public static final String partPattern = "yyMMddHHmmss";
public static final String ticketPattern = "yyyy.MM.dd HH:mm:ss";
public static final String hour_of_minute = "HHmm";
public static final String timeColPattern = "HH:mm:ss";
public static final String dateFullPattern = "yyyyMMdd HH:mm:ss";
public static final String year_of_minute = "yyyyMMddHHmm";
public static final String year_of_hour = "yyyyMMddHH";
public static final String yearDate = "yyyy-MM-dd HH:mm";
/***
* 获取当前时间
* @return 当前时间
*/
public static String getCurrent() {
return format(new Date(), DateUtil.settlePattern);
}
/**
* 使用线程容器来获取SimpleDateFormat
* @param date 日期
* @param pattern 格式
* @return 指定格式的日期字符串
*/
public static String format(Date date, String pattern) {
if (date == null) {
return "";
}
return getSdf(pattern).format(date);
}
/**
* 返回一个ThreadLocal的sdf,每个线程只会new一次sdf
* @param pattern 时间格式
* @return 指定格式的时间
*/
private static SimpleDateFormat getSdf(final String pattern) {
ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);
// 此处的双重判断和同步是为了防止sdfMap这个单例被多次put重复的sdf
if (tl == null) {
synchronized (lockObj) {
tl = sdfMap.get(pattern);
if (tl == null) {
// 使用ThreadLocal替代原来直接new SimpleDateFormat
tl = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(pattern);
}
};
sdfMap.put(pattern, tl);
}
}
}
return tl.get();
}
/**
* 日期转换
* @param dateStr 字符串的日期
* @param pattern 格式
* @return 转换后的日期
* @throws ParseException
*/
public static Date parse(String dateStr, String pattern) throws ParseException {
return getSdf(pattern).parse(dateStr);
}
/**
* 通过开始时间,经过的天数计算到达的日期
* @param startDate
* @param days
* @return
*/
public static String calEndDate(Date startDate, int days) {
Calendar c1 = Calendar.getInstance();
c1.setTime(startDate);
c1.set(Calendar.DATE, c1.get(Calendar.DATE) + days);
Date endDate = c1.getTime();
SimpleDateFormat sdf = new SimpleDateFormat(shotPattern);
String arriveDate = sdf.format(endDate);
return arriveDate;
}
/**
* 通过订单号获取订单的生存日期
* @param bpbpOrderNo
* @return
*/
public static String getOrderCreateDate(String bpbpOrderNo) throws ParseException {
/**销账表需要分区,bpbpOrderNo的生存规则yyyymmdd+seq 需要截取前面的日期*/
String orderCreateDate = bpbpOrderNo.substring(0, 8);
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
Date date = sdf.parse(orderCreateDate);
SimpleDateFormat sdf1 = new SimpleDateFormat(shotPattern);
String createDate = sdf1.format(date);
return createDate;
}
/**
* 将字符传格式时间转换为日期
* @param createDate
* @return
*/
public static Date createDateConverter(String createDate) throws ParseException {
SimpleDateFormat sdf1 = new SimpleDateFormat(shotPattern);
Date orderCreateDate = sdf1.parse(createDate);
return orderCreateDate;
}
/**
* 从销账订单字符串获取创建日期
* @param bpbpOrderNo
* @return
*/
public static Date createDateConverter(String bpbpOrderNo,String format) throws ParseException {
SimpleDateFormat sdf1 = new SimpleDateFormat(format);
return sdf1.parse(bpbpOrderNo.substring(0, 4) +"-"+ bpbpOrderNo.substring(4, 6) +"-"+ bpbpOrderNo.substring(6, 8));
}
/**
* 计算前几分钟的时间
* @param minute 分钟数
* @return 时间
*/
public static String getCurrentAfter(int minute) {
Calendar targetTime = Calendar.getInstance();
targetTime.setTime(new Date());
targetTime.add(Calendar.MINUTE, minute);
return format(targetTime.getTime(), DateUtil.fullPattern);
}
/***
* 时间格式转换
* @param date 时间字符串
* @param originPattern 原时间格式
* @param targetPattern 新的时间格式
* @return 转换后的日期字符串
* @throws ParseException
*/
public static String convert(String date, String originPattern,
String targetPattern) throws ParseException {
Date originDate = parse(date, originPattern);
return format(originDate, targetPattern);
}
/**
* 源日期和(目标日期加上毫秒数)比较大小, 大则返回false ,小返回true
* @param src 源日期
* @param target 目的日期
* @param second 秒数
* @return 成功,失败
*/
public static boolean compareDateForSecond(Date src, Date target, int second) {
Calendar targetTime = Calendar.getInstance();
targetTime.setTime(target);
targetTime.add(Calendar.SECOND, second);
Calendar srcTime = Calendar.getInstance();
srcTime.setTime(src);
return srcTime.compareTo(targetTime) <= 0;
}
/**
* 比较目标时间是否在两个时间时间范围内
* @param beginDate 开始时间
* @param endDate 结束时间
* @param compareDate 目标时间
* @return true 在时间范围内 false 不在时间范围内
*/
public static boolean compareDateBetweenTwoDates(Date beginDate,Date endDate,Date compareDate){
boolean result1 = compareDateForSecond(beginDate,compareDate,0);
boolean result2 = compareDateForSecond(compareDate,endDate,0);
if(result1 && result2){
return true;
}
return false;
}
/**
* 将日志转为指定格式的字符串
* @param date
* @return
*/
public static String format(Date date) {
return format(date, fullPattern);
}
/**
* 将日志转为指定格式的字符串
* @param date
* @return
*/
public static String formatString(Date date) {
return format(date, shotPattern);
}
/**
* 返回日期的起始时间
* @param date
* @return
*/
public static Date getDateStart(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND,0);
return c.getTime();
}
/**
* 返回日期的结束时间
* @param date
* @return
*/
public static Date getDateEnd(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND,999);
return c.getTime();
}
}
/**
* 获取开始时间到中间时间的所有时间段
*
* @param start 开始时间
* @param end 结束时间
* @param calendarType 时间类型
* @return
*/
public static Date[] getDateArrays(Date start, Date end, int calendarType) {
ArrayList<Date> ret = new ArrayList();
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
Date tmpDate = calendar.getTime();
long endTime = end.getTime();
while (tmpDate.before(end) || tmpDate.getTime() == endTime) {
ret.add(calendar.getTime());
calendar.add(calendarType, 1);
tmpDate = calendar.getTime();
}
Date[] dates = new Date[ret.size()];
return (Date[]) ret.toArray(dates);
}