package com.sf.wop.common.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DateUtil {
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss SSS";
public static final String ORA_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_TIME_FORMAT_YMD = "yyyyMMdd";
public static final String DATE_TIME_FORMAT_MMDDHHmm = "MMddHHmm";
private static Logger logger = LoggerFactory.getLogger(DateUtil.class);
private static final String DATE_FORMAT = "yyyy-MM-dd";
/**
* 判断当前日期是星期几 1-7
*
* @param pTime 修要判断的时间
* @return dayForWeek 判断结果
* @Exception 发生异常
*/
public static int dayOfWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
/**
* @param dateBgn 开始日期
* @param dateEnd 结束日期
* @return 日期间隔天数
* @throws ParseException
*/
public static int getIntervalDays(String dateBgn, String dateEnd) throws ParseException{
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(format.parse(dateEnd));
int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
aCalendar.setTime(format.parse(dateBgn));
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
return day2 - day1;
}
/**
* @param dateBgn 开始日期
* @param dateEnd 结束日期
* @return 日期间隔分钟
* @throws ParseException
*/
public static long getIntervalMin(String dateBgn, String dateEnd, String format) throws ParseException{
SimpleDateFormat DateFormat = new SimpleDateFormat(format);
Date end = DateFormat.parse(dateEnd);
Date begin = DateFormat.parse(dateBgn);
return (end.getTime() - begin.getTime())/1000/90;
}
/**
* 把日期往后增加n月. 整数往后推,负数往前移动
*
* @param dateStr
* @param interval
* @return
* @throws ParseException
*/
public static String calcMonth(String dateStr, int interval) throws ParseException{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Calendar calendar = new GregorianCalendar();
calendar.setTime(format.parse(dateStr));
calendar.add(Calendar.MONTH, interval);
return format.format(calendar.getTime());
}
/**
* 把日期往后增加n天. 整数往后推,负数往前移动
*
* @param dateStr
* @param interval
* @return
* @throws ParseException
*/
public static String calcDate(String dateStr, int interval) throws ParseException{
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
Calendar calendar = new GregorianCalendar();
calendar.setTime(format.parse(dateStr));
calendar.add(Calendar.DATE, interval);
return format.format(calendar.getTime());
}
/**
* 把日期往后增加n天. 整数往后推,负数往前移动
*
* @param dateStr
* @param interval
* @return
* @throws ParseException
*/
public static Date calcDate(String dataStr, String format, int interval, int intervalType) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Calendar calendar = new GregorianCalendar();
calendar.setTime(dateFormat.parse(dataStr));
calendar.add(intervalType, interval);
return calendar.getTime();
}
/**
* 把日期往后增加n天. 整数往后推,负数往前移动
*
* @param dateStr
* @param interval
* @return
* @throws ParseException
*/
public static Date calcDate(Date date, int interval, int intervalType) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(intervalType, interval);
return calendar.getTime();
}
/**
* 把日期往后增加n天. 整数往后推,负数往前移动
*
* @param dateStr
* @param interval
* @return
* @throws ParseException
*/
public static Date calcDate(String dateStr, int interval, int intervalType) throws ParseException{
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
Calendar calendar = new GregorianCalendar();
calendar.setTime(format.parse(dateStr));
calendar.add(intervalType, interval);
return calendar.getTime();
}
public static int getBetweenDays(String t1, String t2)
throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int betweenDays = 0;
Date d1 = format.parse(t1);
Date d2 = format.parse(t2);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
// 保证第二个时间一定大于第一个时间
if (c1.after(c2)) {
c1 = c2;
c2.setTime(d1);
}
int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
for (int i = 0; i < betweenYears; i++) {
c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
}
return betweenDays;
}
/**
* 把日期往后增加n天. 整数往后推,负数往前移动
*
* @param dateStr
* @param interval
* @return
* @throws ParseException
*/
public static String calcDate(Date date, int interval) throws ParseException{
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, interval);
return format.format(calendar.getTime());
}
public static List<String> getDaysExcluded(String dateBgn, String dateEnd, String weekSelected){
List<String> days = new ArrayList<>();
String[] weekInfo = weekSelected.split(",");
if(weekInfo == null || weekInfo.length == 0){
logger.warn(String.format("at least one day in week should be selectd: %s", weekSelected));
return days;
}
Map<String, String> dayOfWeekMap = new HashMap<String, String>();
for(String index : weekInfo){
dayOfWeekMap.put(index, index);
}
Map<String, String> usDayOfweekMap = new HashMap<String, String>();
for(int i = 1; i <= 7; i++){
if(dayOfWeekMap.containsKey(""+i)){
continue;
}
usDayOfweekMap.put(""+i, ""+i);
}
//没有排除任何一天
if(usDayOfweekMap.size() == 0){
return days;
}
int interval = 0;
try {
interval = getIntervalDays(dateBgn, dateEnd);
} catch (ParseException e) {
logger.warn(String.format("invalid date format: %s or %s", dateBgn,dateEnd));
return days;
}
if(interval < 0){
logger.warn(String.format("end day shoule be equal or greater than begin day: %s or %s", dateBgn,dateEnd));
return days;
}
int dayIndex = 0;
try {
dayIndex = dayOfWeek(dateBgn);
} catch (Exception e) {
logger.warn(String.format("invalid date format: %s", dateBgn));
return days;
}
for(int i=0;i<= interval;i++){
//当前为非选择的星期序号
if(usDayOfweekMap.containsKey(""+dayIndex)){
try {
days.add(calcDate(dateBgn, i));
} catch (ParseException e) {
}
}
//下一个日期的星期序号
dayIndex++;
if(dayIndex>7){
dayIndex -= 7;
}
}
return days;
}
public static String format(Date date) {
String result = DateFormatUtils.format(date, DATE_TIME_FORMAT);
return result;
}
public static String format(Date date, String format) {
String result = DateFormatUtils.format(date, format);
return result;
}
public static Date parseDate(String dateStr) {
DateFormat dateFormat = new SimpleDateFormat(ORA_DATE_TIME_FORMAT);
try {
return dateFormat.parse(dateStr);
} catch (ParseException e) {
logger.error("DateUtil parseDate error", e);
}
return null;
}
public static Date parseDate(String dateStr, String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
try {
return dateFormat.parse(dateStr);
} catch (ParseException e) {
logger.error("DateUtil parseDate error", e);
}
return null;
}
public static List<String> getTimesBetween(String beginDateStr, String endDateStr, String inputFormat,
String outFormat, int interval, int intervalType){
List<String> rtn = new ArrayList<String>();
Date beginDate = parseDate(beginDateStr, inputFormat);
Date endDate = parseDate(endDateStr, inputFormat);
if(beginDate.after(endDate)){
return rtn;
}
//加入起始日期
rtn.add(format(beginDate, outFormat));
Date curDate = calcDate(beginDate, interval, intervalType);
while(!curDate.after(endDate)){
//加入起始日期
rtn.add(format(curDate, outFormat));
curDate = calcDate(curDate, interval, intervalType);
}
return rtn;
}
public static void main(String[] args) {
// String s = "2016-05-33 15:25:25";
// System.out.println(parseDate(s));
// String cur = "2016-02-21";
// String end = "2016-03-08";
//
// try {
// System.out.println(dayOfWeek(cur));
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// List<String> days = getDaysExcluded(cur, end, "1,3,5");
// for(String day : days){
// System.out.println(day);
// }
//
// try {
// System.out.println(getBetweenDays("2016-01-17","2016-01-19"));
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
long stt = System.currentTimeMillis();
List<String> result = getTimesBetween("201606302050","201607010400","yyyyMMddHHmm","ddHH",1,Calendar.HOUR);
for(String str : result){
System.out.println(str);
}
//System.out.println((System.currentTimeMillis() - stt));
}
}