import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.time.DateFormatUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* 日期工具类
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
/**
* 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
*/
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
/**
* 日期型字符串转化为日期 格式
* { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
* "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null){
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取过去的月份
* @param date
* @return
*/
public static long pastMonth(Date date) {
Calendar bef = Calendar.getInstance();
Calendar aft = Calendar.getInstance();
bef.setTime(new Date());
aft.setTime(date);
int surplus = aft.get(Calendar.DATE) - bef.get(Calendar.DATE);
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
surplus = surplus <= 0 ? 1 : 0;
return (Math.abs(month + result) + surplus);
}
/**
* 获取过去的天数
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(24*60*60*1000);
}
/**
* 获取过去的小时
* @param date
* @return
*/
public static long pastHour(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(60*60*1000);
}
/**
* 获取过去的分钟
* @param date
* @return
*/
public static long pastMinutes(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(60*1000);
}
/**
* 还剩多少秒
* @param date
* @return
*/
public static long remainSecond(Date date) {
long t = date.getTime() - new Date().getTime();
return t/(1000);
}
/**
* 还剩多少分钟
* @param date
* @return
*/
public static long remainMinutes(Date date) {
long t = date.getTime() - new Date().getTime();
return t/(60*1000);
}
/**
* 转换为时间(天,时:分:秒.毫秒)
* @param timeMillis
* @return
*/
public static String formatDateTime(long timeMillis){
long day = timeMillis/(24*60*60*1000);
long hour = (timeMillis/(60*60*1000)-day*24);
long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
}
/**
* 获取两个日期之间的天数
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
/**
* 获取两个日期之间的小时
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoHours(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60);
}
/**
* 获取两个日期之间的分钟
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoMinutes(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60);
}
public static Date getPoorTimeTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
long diff = afterTime - beforeTime;//这样得到的差值是微秒级别
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
long second = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000*60))/(1000);
String poorTime = "00:"+minutes+":"+second;
Date poor = parseDate("00:00:00");
try {
poor = parseDate(poorTime, "HH:mm:ss");
}catch(Exception e) {
}
return poor;
}
/**
* 计算时间
* @param before
* @param after
* @return
*/
public static String getDistanceOfTwoString(Date before, Date after) {
String t = "";
long beforeTime = before.getTime();
long afterTime = after.getTime();
long diff = afterTime - beforeTime;//这样得到的差值是微秒级别
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
long second = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000*60))/(1000);
long microsecond = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000*60)-second*1000);
if(days > 0) {
t += days + "天";
}
if(hours > 0) {
t += hours + "小时";
}
if(minutes > 0) {
t += minutes + "分";
}
if(second > 0) {
t += second + "秒";
}
if(microsecond > 0) {
t += microsecond + "微秒";
}
return t;
}
/**
* 获取N天后的时间
* @return
*/
public static Date getDayAfterN(Integer day) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(day == 0){
return calendar.getTime();
}
calendar.add(Calendar.DATE, day);
return calendar.getTime();
}
/**
* 根据固定时间获取N天后的时间
* @return
*/
public static Date getFixedDayAfterN(Date date, Integer day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(day == 0){
return calendar.getTime();
}
calendar.add(Calendar.DATE, day);
return calendar.getTime();
}
/**
* 获取N分钟后的时间
* @return
*/
public static Date getMinuteAfterN(Date date, Integer minute) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(minute == 0){
return calendar.getTime();
}
calendar.add(Calendar.MINUTE, minute);
return calendar.getTime();
}
/**
* 获取N秒后的时间
* @return
*/
public static Date getSecondAfterN(Date date, Integer second) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(second == 0){
return calendar.getTime();
}
calendar.add(Calendar.SECOND, second);
return calendar.getTime();
}
/**
* 获取N天后的时间 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDayAfter(int day) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, day);
return DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd");
}
/**
/**
* 判断时间是否在时间段内
* @param nowTime
* @param beginTime
* @param endTime
* @return
*/
public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
//设置当前时间
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
//设置开始时间
Calendar begin = Calendar.getInstance();
begin.setTime(beginTime);
//设置结束时间
Calendar end = Calendar.getInstance();
end.setTime(endTime);
//处于开始时间之后,和结束时间之前的判断
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
/**
* 获取某年某月 所有日期
* @return
*/
public static List<Date> getMonthFullDay(int year, int month){
List<Date> fullDayList = Lists.newArrayList();
// 获得当前日期对象
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH, month-1 );
// 当月1号
cal.set(Calendar.DAY_OF_MONTH,1);
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int j = 1; j <= count ; j++) {
fullDayList.add(cal.getTime());
cal.add(Calendar.DAY_OF_MONTH,1);
}
return fullDayList;
}
/**
* 获取指定年月的第一天
* @param year
* @param month
* @return
*/
public static String getFirstDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最小天数
int firstDay = cal.getMinimum(Calendar.DATE);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH,firstDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
/**
* 获取指定年月的最后一天
* @param year
* @param month
* @return
*/
public static String getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DATE);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
}