/**
|
* Copyright (c) 2018 人人开源 All rights reserved.
|
* <p>
|
* https://www.renren.io
|
* <p>
|
* 版权所有,侵权必究!
|
*/
|
|
package com.iailab.common.utils;
|
|
import com.iailab.common.constant.BusinessConstant;
|
import org.apache.commons.lang3.StringUtils;
|
import org.joda.time.DateTime;
|
import org.joda.time.LocalDate;
|
import org.joda.time.format.DateTimeFormat;
|
import org.joda.time.format.DateTimeFormatter;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
/**
|
* 日期处理
|
*
|
* @author Mark sunlightcs@gmail.com
|
*/
|
public class DateUtils {
|
/** 时间格式(yyyy-MM-dd) */
|
public final static String DATE_PATTERN = "yyyy-MM-dd";
|
/** 时间格式(yyyy-MM-dd HH:mm:ss) */
|
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
|
public final static String DATE_TIME_PATTERN_MIN = "yyyy-MM-dd HH:mm";
|
|
public final static String TIME2_PATTERN = "mmss";
|
|
public final static String DATE_PATTERN_MON = "yyyy-MM";
|
|
public final static String DATE_PATTERN_YEAR = "yyyy-MM";
|
|
/**
|
* 日期格式化 日期格式为:yyyy-MM-dd
|
* @param date 日期
|
* @return 返回yyyy-MM-dd格式日期
|
*/
|
public static String format(Date date) {
|
return format(date, DATE_PATTERN);
|
}
|
|
/**
|
* 日期格式化 日期格式为:yyyy-MM-dd
|
* @param date 日期
|
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
|
* @return 返回yyyy-MM-dd格式日期
|
*/
|
public static String format(Date date, String pattern) {
|
if (date != null) {
|
SimpleDateFormat df = new SimpleDateFormat(pattern);
|
return df.format(date);
|
}
|
return null;
|
}
|
|
/**
|
* 日期解析
|
* @param date 日期
|
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
|
* @return 返回Date
|
*/
|
public static Date parse(String date, String pattern) {
|
try {
|
return new SimpleDateFormat(pattern).parse(date);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 字符串转换成日期
|
* @param strDate 日期字符串
|
* @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN
|
*/
|
public static Date stringToDate(String strDate, String pattern) {
|
if (StringUtils.isBlank(strDate)) {
|
return null;
|
}
|
|
DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
|
return fmt.parseLocalDateTime(strDate).toDate();
|
}
|
|
/**
|
* 根据周数,获取开始日期、结束日期
|
* @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周
|
* @return 返回date[0]开始日期、date[1]结束日期
|
*/
|
public static Date[] getWeekStartAndEnd(int week) {
|
DateTime dateTime = new DateTime();
|
LocalDate date = new LocalDate(dateTime.plusWeeks(week));
|
|
date = date.dayOfWeek().withMinimumValue();
|
Date beginDate = date.toDate();
|
Date endDate = date.plusDays(6).toDate();
|
return new Date[]{beginDate, endDate};
|
}
|
|
/**
|
* 对日期的【秒】进行加/减
|
*
|
* @param date 日期
|
* @param seconds 秒数,负数为减
|
* @return 加/减几秒后的日期
|
*/
|
public static Date addDateSeconds(Date date, int seconds) {
|
DateTime dateTime = new DateTime(date);
|
return dateTime.plusSeconds(seconds).toDate();
|
}
|
|
/**
|
* 对日期的【分钟】进行加/减
|
*
|
* @param date 日期
|
* @param minutes 分钟数,负数为减
|
* @return 加/减几分钟后的日期
|
*/
|
public static Date addDateMinutes(Date date, int minutes) {
|
DateTime dateTime = new DateTime(date);
|
return dateTime.plusMinutes(minutes).toDate();
|
}
|
|
/**
|
* 对日期的【小时】进行加/减
|
*
|
* @param date 日期
|
* @param hours 小时数,负数为减
|
* @return 加/减几小时后的日期
|
*/
|
public static Date addDateHours(Date date, int hours) {
|
DateTime dateTime = new DateTime(date);
|
return dateTime.plusHours(hours).toDate();
|
}
|
|
/**
|
* 对日期的【天】进行加/减
|
*
|
* @param date 日期
|
* @param days 天数,负数为减
|
* @return 加/减几天后的日期
|
*/
|
public static Date addDateDays(Date date, int days) {
|
DateTime dateTime = new DateTime(date);
|
return dateTime.plusDays(days).toDate();
|
}
|
|
/**
|
* 对日期的【周】进行加/减
|
*
|
* @param date 日期
|
* @param weeks 周数,负数为减
|
* @return 加/减几周后的日期
|
*/
|
public static Date addDateWeeks(Date date, int weeks) {
|
DateTime dateTime = new DateTime(date);
|
return dateTime.plusWeeks(weeks).toDate();
|
}
|
|
/**
|
* 对日期的【月】进行加/减
|
*
|
* @param date 日期
|
* @param months 月数,负数为减
|
* @return 加/减几月后的日期
|
*/
|
public static Date addDateMonths(Date date, int months) {
|
DateTime dateTime = new DateTime(date);
|
return dateTime.plusMonths(months).toDate();
|
}
|
|
/**
|
* 对日期的【年】进行加/减
|
*
|
* @param date 日期
|
* @param years 年数,负数为减
|
* @return 加/减几年后的日期
|
*/
|
public static Date addDateYears(Date date, int years) {
|
DateTime dateTime = new DateTime(date);
|
return dateTime.plusYears(years).toDate();
|
}
|
|
public static List<String> getDays(Date startDate, Date endDate) {
|
List<String> days = new ArrayList<String>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
|
while (calendar.getTime().compareTo(endDate) < 0) {
|
days.add(DateUtils.format(calendar.getTime(), "yyyy-MM-dd"));
|
calendar.add(6, 1);
|
}
|
return days;
|
}
|
|
public static Date getTime(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
return calendar.getTime();
|
}
|
|
public static List<String> getYearTime(Date startDate, Date endDate) {
|
List<String> days = new ArrayList<String>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
|
while (calendar.getTime().compareTo(endDate) <= 0) {
|
days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN_YEAR));
|
calendar.add(Calendar.YEAR, 1);
|
}
|
return days;
|
}
|
|
public static List<String> getMonTime(Date startDate, Date endDate) {
|
List<String> days = new ArrayList<String>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
|
while (calendar.getTime().compareTo(endDate) <= 0) {
|
days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN_MON));
|
calendar.add(Calendar.MONTH, 1);
|
}
|
return days;
|
}
|
|
public static List<String> getDayTime(Date startDate, Date endDate) {
|
List<String> days = new ArrayList<String>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
|
while (calendar.getTime().compareTo(endDate) <= 0) {
|
days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN));
|
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
}
|
return days;
|
}
|
|
public static List<String> getDayTime(Date startDate, Date endDate, int seconds) {
|
List<String> days = new ArrayList<String>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
while (calendar.getTime().compareTo(endDate) <= 0) {
|
days.add(DateUtils.format(calendar.getTime(), DATE_TIME_PATTERN));
|
calendar.add(Calendar.SECOND, seconds);
|
}
|
return days;
|
}
|
|
public static List<String> getClassTime(Date startDate, Date endDate) {
|
List<String> days = new ArrayList<String>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
while (calendar.getTime().compareTo(endDate) <= 0) {
|
days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN) + " 0点班");
|
days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN) + " 8点班");
|
days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN) + " 16点班");
|
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
}
|
return days;
|
}
|
|
public static Map<String, Date> getIntervalDate() {
|
Date tEndDate = getTime(addDateDays(new Date(), BusinessConstant.SCHEDULEDCLEANDAYS)); //当前时间前10天
|
Date tStartDate = getTime(addDateHours(tEndDate, -1));//当前时间前10天前一个小时
|
Map<String, Date> tMap = new HashMap<String, Date>();
|
tMap.put("startdate", tStartDate);
|
tMap.put("enddate", tEndDate);
|
return tMap;
|
|
}
|
|
public static Map<String, Date> getIntervalDate(int days) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.add(Calendar.DAY_OF_YEAR, days);
|
calendar.set(Calendar.MILLISECOND, 0);
|
calendar.set(Calendar.SECOND, 0);
|
Date tEndDate = calendar.getTime();
|
calendar.add(Calendar.HOUR_OF_DAY, -1);
|
Date tStartDate = calendar.getTime();
|
Map<String, Date> tMap = new HashMap<String, Date>(2);
|
tMap.put("startdate", tStartDate);
|
tMap.put("enddate", tEndDate);
|
return tMap;
|
|
}
|
|
public static boolean isNotBlank(Date date) {
|
if (date == null) {
|
return false;
|
}
|
return true;
|
}
|
|
}
|