import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class DateUtil {
private int settlementDay =26;
public int getSettlementDay() {
return settlementDay;
}
private Date getDate(){
return new Date();
}
public Date getCurrDate0H() {
return DateUtils.setSeconds(DateUtils.setMinutes(DateUtils.setHours(this.getDate(), 0), 0), 0);
}
public Date getLastCurrDate0H() {
return DateUtils.addDays(this.getCurrDate0H(), -1);
}
public Date getCurrDate0H(Date date) {
return DateUtils.setSeconds(DateUtils.setMinutes(DateUtils.setHours(date, 0), 0), 0);
}
public Date getCurrDate24H() {
return DateUtils.addDays(DateUtils.setSeconds(DateUtils.setMinutes(DateUtils.setHours(getDate(), 0), 0), 0),1);
}
public Date getCurrDate24H(Date date) {
return DateUtils.addDays(DateUtils.setSeconds(DateUtils.setMinutes(DateUtils.setHours(date, 0), 0), 0),1);
}
/**
* 将日期转换为y/m/d格式的字符串
* @param date
* @return
*/
public String formatDate(Date date){
String date0 = this.format(date,"yyyyMMdd");
String year = date0.substring(0,4);
int month = Integer.valueOf(date0.substring(4,6));
int day = Integer.valueOf(date0.substring(6,8));
return year+"/"+month+"/"+day;
}
/**
* @param pattern 日期格式
* @param date
* @return
*/
public String format(Date date,String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
/**
* 获取当前日期 String 类型
* @return
*/
public String getCurrDateString() {
return this.format(getCurrDate0H(), "YYYYMMdd");
}
/**
* 获取前一天日期 String 类型
* @return
*/
public String getLastCurrDateString() {
return this.format(getLastCurrDate0H(), "YYYYMMdd");
}
/**
* 获取两个时间之间的日期数
* @param beginDate
* @param endDate
* @return
*/
public int getBetweenDate(Date beginDate, Date endDate){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
String end=this.format(this.getCurrDate0H(endDate), "yyyyMMdd");
String begin = this.format(this.getCurrDate0H(beginDate), "yyyyMMdd");
Long c;
try {
c = sf.parse(end).getTime()-sf.parse(begin).getTime();
Long d = c/1000/60/60/24;//天
return d.intValue();
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
/**
* 获取两个时间之间的月数
* @param beginDate(yyyyMM)
* @param endDate(yyyyMM)
* @return
*/
public int getBetweenMonth(Date beginDate, Date endDate){
return ((this.getYear(endDate) - this.getYear(beginDate))*12)+this.getMonthOfYear(endDate)-this.getMonthOfYear(beginDate);
}
/**
* 获取年
*
* @param date
* @return
*/
public int getYear(Date date) {
DateTime jodaDate = new DateTime(date);
return jodaDate.getYear();
}
/**
* 获取月
*
* @param date
* @return
*/
public int getMonthOfYear(Date date) {
DateTime jodaDate = new DateTime(date);
return jodaDate.getMonthOfYear();
}
/**
* 获取日
*
* @param date
* @return
*/
public int getDayOfMonth(Date date) {
DateTime jodaDate = new DateTime(date);
return jodaDate.getDayOfMonth();
}
/**
* 获取月末日期
*
* @param date
* @return
*/
public Date getLastDayOfMonth(Date date) {
DateTime d = new DateTime(date.getTime());
return d.dayOfMonth().withMaximumValue().toDate();
}
/**
* 获取两个时间之间的潢月月数
* @param beginDate(yyyyMM)
* @param endDate(yyyyMM)
* @return
*/
public int getFullBetweenMonth(Date beginDate, Date endDate){
return this.monthsBetween(beginDate, endDate);
}
/**
* 获取两个时间之间的月数
* @param beginDate(yyyyMMdd)
* @param endDate(yyyyMMdd)
* @return
*/
public int getBetweenMonthByDate(Date beginDate, Date endDate){
int month = ((this.getYear(endDate) - this.getYear(beginDate))*12)+this.getMonthOfYear(endDate)-this.getMonthOfYear(beginDate);
if(this.getDayOfMonth(endDate) - this.getDayOfMonth(beginDate)<0){
if(this.checkLastDayOfMonth(endDate)){
return month;
}else{
return month-1;
}
}
return month;
}
/**
* 判断日期是否为当月最后一天
* @param reportDate
* @return
*/
public boolean checkLastDayOfMonth(Date reportDate){
return this.getLastDayOfMonth(reportDate).compareTo(reportDate)==0?true:false;
}
/**
* 获取两个时间之间的年数(1年起步)
* @param beginDate
* @param endDate
* @return
*/
public String getOrderYears(Date validateDate){
Date newDate = new Date();
int year = this.getYear(newDate) - this.getYear(validateDate) + 1;
int month = this.getMonthOfYear(newDate) - this.getMonthOfYear(validateDate);
int day = this.getDayOfMonth(newDate) - this.getDayOfMonth(validateDate);
if(month>0){
return String.valueOf(year>0?year:1);
}else if(month < 0){
return String.valueOf((year>1?year:2) - 1);
}else{
if(day>=0 || ((this.getDayOfMonth(this.getLastDayOfMonth(newDate)) == this.getDayOfMonth(newDate)) && (this.getDayOfMonth(this.getLastDayOfMonth(validateDate)) == this.getDayOfMonth(validateDate)))){
return String.valueOf(year>0?year:1);
}else{
return String.valueOf((year>1?year:2) - 1);
}
}
}
/**
* 获取两个时间之间的年数(标准年数)
* @param beginDate
* @param endDate
* @return
*/
public Long getOrderYears(Date validateDate, Date reportDate){
//Date newDate = new Date();
int year = this.getYear(reportDate) - this.getYear(validateDate);
int month = this.getMonthOfYear(reportDate) - this.getMonthOfYear(validateDate);
int day = this.getDayOfMonth(reportDate) - this.getDayOfMonth(validateDate);
if(month>0){
return Long.valueOf(year);
}else if(month < 0){
return Long.valueOf(year - 1);
}else{
if(day>=0 || ((this.getDayOfMonth(this.getLastDayOfMonth(reportDate)) == this.getDayOfMonth(reportDate)) && (this.getDayOfMonth(this.getLastDayOfMonth(validateDate)) == this.getDayOfMonth(validateDate)))){
return Long.valueOf(year);
}else{
return Long.valueOf(year - 1);
}
}
}
public boolean checkLastDayOfFeb(Date reportDate){
if(this.getMonthOfYear(reportDate)==2 && this.getDayOfMonth(this.getLastDayOfMonth(reportDate))==this.getDayOfMonth(reportDate)){
return true;
}
return false;
}
/**
* 返回上个结算日
* @return
*/
public Date getBeforeSettlementDay(){
if(this.getDayOfMonth(getCurrDate0H())>=getSettlementDay()){
return DateUtils.setDays(getCurrDate0H(), getSettlementDay());
}else{
return DateUtils.setDays(DateUtils.addMonths(getCurrDate0H(), -1), getSettlementDay());
}
}
/**
* 根据指定日期获取对应当月结算起始日期(含)
* @param reportDate
* @return
*/
public Date getBeforeSettlementDay(Date reportDate){
return DateUtils.setDays(this.getCurrDate0H(reportDate), 1);
}
public Date getThisSettlementDay(Date date) {
return this.getLastDayOfMonth(date);
}
/**
* 返回下个结算日
* @param date
* @return
*/
public Date getNextSettlementDay(Date date) {
return this.getFirstDayOfMonth(DateUtils.addMonths(date, 1));
}
/**
* 获取月初
* @param date
* @return
*/
public Date getFirstDayOfMonth(Date date) {
DateTime d = new DateTime(date.getTime());
return d.dayOfMonth().withMinimumValue().toDate();
}
/**
* 返回上个月结算日
* @return
*/
public Date getBeforeMonthSettlementDay(){
return DateUtils.setDays(DateUtils.addMonths(getCurrDate0H(), -1), getSettlementDay());
}
/**
* 返回上上个月结算日
* @return
*/
public Date getLastBeforeMonthSettlementDay(){
return DateUtils.setDays(DateUtils.addMonths(getCurrDate0H(), -2), getSettlementDay());
}
/**
* 返回当前日期绝对值是否大于等于结算日绝对值
* @param
*/
public Boolean compareWithSettlementDay(){
if(this.getDayOfMonth(getCurrDate0H())>=getSettlementDay()){
return true;
}else{
return false;
}
}
/**
* 返回当前日是否大于今年结算日 小于明年一月一日
* @param
*/
public Boolean compareWithYearSettlementDay(){
return this.containsBetween(getCurrDate0H(), getNewYearSettlementDay(), getAfterYearFirstDay());
}
/**
* 判断now是否在区间[from, to]中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsBetween(Date now, Date from, Date to) {
DateTime d1 = new DateTime(from);
DateTime d2 = new DateTime(to);
DateTime d3 = new DateTime(now);
return new Interval(d1, d2.plusDays(1)).contains(d3);
}
/**
* 返回当前月的1号0点
* @return
*/
public Date getMonthDayFirst(){
return DateUtils.setDays(getCurrDate0H(),1);
}
/**
* 获取去年12月SettlementDay日
* @param args
*/
public Date getLastYearSettlementDay(){
return DateUtils.setDays(DateUtils.setMonths(DateUtils.addYears(getCurrDate0H(), -1), 11), getSettlementDay());
}
/**
* 获取今年12月SettlementDay日
* @param args
*/
public Date getNewYearSettlementDay(){
return DateUtils.setDays(DateUtils.setMonths(DateUtils.addYears(getCurrDate0H(), 0), 11), getSettlementDay());
}
/**
* 获取今年的1月1日
* @param args
*/
public Date getYearFirstDay(){
return DateUtils.setDays(DateUtils.setMonths(getCurrDate0H(), 0), 1);
}
/**
* 获取明年的1月1日
* @param args
*/
public Date getAfterYearFirstDay(){
// return KC.date.setDays(KC.date.addYears(KC.date.setMonths(getCurrDate0H(), 1), 1),1);
return DateUtils.setDays(DateUtils.setMonths(DateUtils.addYears(getCurrDate0H(), 1), 0),1);
}
/**
* 指定日期加上天数后的日期
* @param num 为增加的天数
* @param newDate 创建时间
* @return
* @throws ParseException
*/
public String plusDay(int num,String newDate) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currdate;
try {
currdate = format.parse(newDate);
//System.out.println("现在的日期是:" + currdate);
Calendar ca = Calendar.getInstance();
ca.setTime(currdate);
ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
currdate = ca.getTime();
String enddate = format.format(currdate);
//System.out.println("增加天数以后的日期:" + enddate);
return enddate;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 指定日期加上天数后的日期
* @param num 为增加的天数
* @param newDate 目标日期
* @return
* @throws ParseException
*/
public Date plusDay(int num,Date newDate) {
Calendar ca = Calendar.getInstance();
ca.setTime(newDate);
ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
return ca.getTime();
}
/**
* 指定日期加上天数后的日期
* @param num 为增加的天数
* @param newDate 目标日期
* @return
* @throws ParseException
*/
public Date plusStringDay(int num,String newDate) {
java.util.Date date2 = new Date(Long.valueOf(newDate));
Calendar ca = Calendar.getInstance();
ca.setTime(date2);
ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
return ca.getTime();
}
/**
* 指定日期的次月10号
* @param newDate 目标日期
* @return
* @throws ParseException
*/
public Date getNextMonth10(String newDate) {
// SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date2 = new Date(Long.valueOf(newDate));
// return sdf.format(date2);
Calendar ca = Calendar.getInstance();
ca.setTime(date2);
ca.add(Calendar.MONTH, 1);
ca.set(Calendar.DATE, 10);
return ca.getTime();
}
/**
* 将毫秒转换为天时分秒
* 毫秒到秒=》1000
* 秒=》分=》60
* 分=》时=》60
* 时=》天=》24
* @param args
* @throws ParseException
*/
public String changeTimeType(Long mss){
String msg = "";
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
long ms =mss%1000;
msg = msg + (days>0 ? (days+"天"):"");
msg = msg + (hours>=0 ? (hours+"小时"):"");
msg = msg + (minutes>=0 ? (minutes+"分"):"");
msg = msg + (seconds>=0 ? (seconds+"秒"):"");
msg = msg + (ms>=0 ? (ms+"毫秒"):"");
return msg;
}
public Date getThisYearValDate(Date valDate) {
if(valDate != null) {
while (valDate.compareTo(new Date())>0) {
valDate = DateUtils.addYears(valDate, -1);
}
}
return valDate;
}
/**
* 是否月初
*
* @param date
* @return
*/
public static boolean isFirstDayOfMonth(Date date) {
return DateUtils.toCalendar(date).get(Calendar.DATE) == 1;
}
/**
* 是否月末
*
* @param date
* @return
*/
public boolean isLastDayOfMonth(Date date) {
Date nextDay = DateUtils.addDays(date, 1);
return DateUtils.toCalendar(nextDay).get(Calendar.DATE) == 1;
}
/**
* 是否季度初
*
* @param date
* @return
*/
public static boolean isFirstDayOfQuarter(Date date) {
Calendar cal = DateUtils.toCalendar(date);
return (cal.get(Calendar.DATE) == 1) && (cal.get(Calendar.MONTH) % 3 == 0);
}
/**
* 季度第一天
*
* @param date
* @return
*/
public static Date getFirstDayOfQuarter(Date date) {
Calendar cal = DateUtils.toCalendar(date);
int mon = cal.get(Calendar.MONTH);
// 第1季度
if (mon >= Calendar.JANUARY && mon <= Calendar.MARCH) {
cal.set(Calendar.MONTH, Calendar.JANUARY);
}
// 第2季度
if (mon >= Calendar.APRIL && mon <= Calendar.JUNE) {
cal.set(Calendar.MONTH, Calendar.APRIL);
}
// 第3季度
if (mon >= Calendar.JULY && mon <= Calendar.SEPTEMBER) {
cal.set(Calendar.MONTH, Calendar.JULY);
}
// 第4季度
if (mon >= Calendar.OCTOBER && mon <= Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.OCTOBER);
}
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
/**
* 季度最后一天
*
* @param date
* @return
*/
public Date getLastDayOfQuarter(Date date) {
Calendar cal = DateUtils.toCalendar(date);
int mon = cal.get(Calendar.MONTH);
// 第1季度
if (mon >= Calendar.JANUARY && mon <= Calendar.MARCH) {
cal.set(Calendar.MONTH, Calendar.MARCH);
cal.set(Calendar.DAY_OF_MONTH, 31);
}
// 第2季度
if (mon >= Calendar.APRIL && mon <= Calendar.JUNE) {
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DAY_OF_MONTH, 30);
}
// 第3季度
if (mon >= Calendar.JULY && mon <= Calendar.SEPTEMBER) {
cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
cal.set(Calendar.DAY_OF_MONTH, 30);
}
// 第4季度
if (mon >= Calendar.OCTOBER && mon <= Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DAY_OF_MONTH, 31);
}
return cal.getTime();
}
/**
* 是否年初
*
* @param date
* @return
*/
public boolean isFirstDayOfYear(Date date) {
Calendar cal = DateUtils.toCalendar(date);
return cal.get(Calendar.MONTH) == Calendar.JANUARY && cal.get(Calendar.DATE) == 1;
}
/**
* 年初第一天
*
* @param date
* @return
*/
public Date getFirstDayOfYear(Date date) {
Calendar cal = DateUtils.toCalendar(date);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
/**
* 获取日期间隔天数 </br>
* 只比较日期, 忽略时间 如果为空取new Date()
*
* @param from
* @param to
* @return
*/
public int daysBetween(Date from, Date to) {
LocalDate d1 = new LocalDate(from);
LocalDate d2 = new LocalDate(to);
return Days.daysBetween(d1, d2).getDays();
}
/**
* 获取日期间隔月数 </br>
* 只比较日期, 忽略时间 如果为空取new Date()
*
* @param from
* @param to
* @return
*/
public int monthsBetween(Date from, Date to) {
DateTime d1 = new DateTime(from.getTime());
DateTime d2 = new DateTime(to.getTime());
return Months.monthsBetween(d1, d2).getMonths();
}
/**
* 判断now是否在区间[from, to)中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsRightOpen(Date now, Date from, Date to) {
DateTime d1 = new DateTime(from);
DateTime d2 = new DateTime(to);
DateTime d3 = new DateTime(now);
return new Interval(d1, d2).contains(d3);
}
/**
* 判断now是否在区间(from, to]中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsLeftOpen(Date now, Date from, Date to) {
DateTime d1 = new DateTime(from);
DateTime d2 = new DateTime(to);
DateTime d3 = new DateTime(now);
return new Interval(d1.plusDays(1), d2.plusDays(1)).contains(d3);
}
/**
* 判断now是否在区间(from, to)中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsOpen(Date now, Date from, Date to) {
DateTime d1 = new DateTime(from);
DateTime d2 = new DateTime(to);
DateTime d3 = new DateTime(now);
return new Interval(d1.plusDays(1), d2).contains(d3);
}
/**
* 判断now是否在区间[from, to]中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsDateBetween(Date now, Date from, Date to) {
return this.containsBetween(DateUtils.truncate(now, Calendar.DATE),
DateUtils.truncate(from, Calendar.DATE), DateUtils.truncate(to, Calendar.DATE));
}
/**
* 判断now是否在区间[from, to)中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsDateRightOpen(Date now, Date from, Date to) {
return this.containsRightOpen(DateUtils.truncate(now, Calendar.DATE),
DateUtils.truncate(from, Calendar.DATE), DateUtils.truncate(to, Calendar.DATE));
}
/**
* 判断now是否在区间(from, to]中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsDateLeftOpen(Date now, Date from, Date to) {
return this.containsLeftOpen(DateUtils.truncate(now, Calendar.DATE),
DateUtils.truncate(from, Calendar.DATE), DateUtils.truncate(to, Calendar.DATE));
}
/**
* 判断now是否在区间(from, to)中
*
* @param now
* @param from
* @param to
* @return
*/
public boolean containsDateOpen(Date now, Date from, Date to) {
return this.containsOpen(DateUtils.truncate(now, Calendar.DATE), DateUtils.truncate(from, Calendar.DATE),
DateUtils.truncate(to, Calendar.DATE));
}
/**
* null安全 日期格式化,格式yyyyMMdd
*
* @param date
* @return
*/
public String safeFormat(Date date, String pattern) {
if (date == null) {
return "";
}
if (StringUtils.isBlank(pattern)) {
pattern = "yyyyMMdd";
}
return this.format(date, pattern);
}
/**
* null安全 日期格式化,格式yyyyMMdd
*
* @param date
* @return
*/
public String safeFormat(Date date) {
if (date == null) {
return "";
}
return this.format(date, "yyyyMMdd");
}
/**
* 字符串转日期
*
* @param dateStr
* @param pattern
* @return
*/
public Date parse(String dateStr, String pattern) {
DateTimeFormatter format = DateTimeFormat.forPattern(pattern);
DateTime dateTime = DateTime.parse(dateStr, format);
return dateTime.toDate();
}
/**
* 字符串转日期,格式yyyyMMdd
*
* @param dateStr
* @return
*/
public Date parse(String dateStr) {
DateTimeFormatter format = DateTimeFormat.forPattern("yyyyMMdd");
DateTime dateTime = DateTime.parse(dateStr, format);
return dateTime.toDate();
}
/**
* 日期拼接, date1日期+date2时间
*
* @param date1
* @param date2
* @return
*/
public Date joinTime(Date date1, Date date2) {
DateTime date = new DateTime(date1);
DateTime time = new DateTime(date2);
DateTime dateTime = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(),
time.getHourOfDay(), time.getMinuteOfHour(), time.getSecondOfMinute(), time.getMillisOfSecond());
return dateTime.toDate();
}
/**
* 判断2个日期是否同一天次
*
* @param date1
* @param date2
* @return
*/
public boolean isSameDayOfMonth(Date date1, Date date2) {
DateTime jd1 = new DateTime(date1);
DateTime jd2 = new DateTime(date2);
return jd1.getDayOfMonth() == jd2.getDayOfMonth();
}
/**
* 返回日期中最大一天(最新一天)
*
* @param dates
* @return
*/
public Date maxDate(Date... dates) {
if (dates == null || dates.length == 0) {
return null;
}
Date[] sortedDates = sortAsc(dates);
return sortedDates[sortedDates.length - 1];
}
/**
* 返回日期中最大一天(只考虑日期)
*
* @param dates
* @return
*/
public Date trunMaxDate(Date... dates) {
if (dates == null || dates.length == 0) {
return null;
}
Date[] sortedDates = this.trunSortAsc(dates);
return sortedDates[sortedDates.length - 1];
}
/**
* 返回日期排序
*
* @param dates
* @return
*/
public Date[] sortAsc(Date... dates) {
Arrays.sort(dates, new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
return o1.compareTo(o2);
}
});
return dates;
}
/**
* 返回日期排序 只考虑日期
*
* @param dates
* @return
*/
public Date[] trunSortAsc(Date... dates) {
Arrays.sort(dates, new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
return this.truncatedDateCompareTo(o1, o2);
}
private int truncatedDateCompareTo(Date o1, Date o2) {
return DateUtils.truncatedCompareTo(o1, o2, Calendar.DATE);
}
});
return dates;
}
/**
* 日期比较,忽略时分秒
*
* @param from
* @param to
* @return
*/
public int truncatedDateCompareTo(Date from, Date to) {
return DateUtils.truncatedCompareTo(from, to, Calendar.DATE);
}
}