All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.yes.tools.utils.YesDateUtils Maven / Gradle / Ivy
package org.yes.tools.utils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 时间分割工具类
*/
public class YesDateUtils {
private static SimpleDateFormat monthDateFormat = new SimpleDateFormat("MM");
/**
* 将时间段按星期分割
*
* @param start
* @param end
* @return
*/
public static final List splitToWeeks(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Date from = new Date(start.getTime() + 7L * 24 * 3600 * 1000);
Date weekEnd = cn.hutool.core.date.DateUtil.endOfWeek(end);
while (from.compareTo(weekEnd) <= 0) {
Date dt = cn.hutool.core.date.DateUtil.beginOfWeek(from);
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
CollectionUtil.getLast(result).setMonth(monthDateFormat.format(new Date(dt.getTime() - 24L * 3600 * 1000)));
result.add(Range.create(dt));
from.setTime(from.getTime() + 7L * 24 * 3600 * 1000);
}
CollectionUtil.getLast(result).end(end);
CollectionUtil.getLast(result).setMonth(monthDateFormat.format(end));
return result;
}
/**
* 将时间段按照月分割
*
* @param start
* @param end
* @return
*/
public static final List splitToMonths(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MONTH, 1);
Date monthEnd = cn.hutool.core.date.DateUtil.endOfMonth(end);
while (cal.getTimeInMillis() <= monthEnd.getTime()) {
Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
CollectionUtil.getLast(result).setMonth(monthDateFormat.format(new Date(dt.getTime() - 24L * 3600 * 1000)));
result.add(Range.create(dt));
cal.add(Calendar.MONTH, 1);
}
CollectionUtil.getLast(result).end(end);
CollectionUtil.getLast(result).setMonth(monthDateFormat.format(end));
return result;
}
/**
* 将时间段按照季度分割
*
* @param start
* @param end
* @return
*/
public static final List splitToQuarts(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MONTH, 3);
Date quartEnd = cn.hutool.core.date.DateUtil.endOfQuarter(end);
while (cal.getTimeInMillis() <= quartEnd.getTime()) {
Date dt = cn.hutool.core.date.DateUtil.beginOfQuarter(cal.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
System.out.println(cal.get(Calendar.MONTH));
if (cal.get(Calendar.MONTH) >= 1 && cal.get(Calendar.MONTH) <= 3) {
CollectionUtil.getLast(result).setMonth("4");
} else if (cal.get(Calendar.MONTH) >= 4 && cal.get(Calendar.MONTH) <= 6) {
CollectionUtil.getLast(result).setMonth("1");
} else if (cal.get(Calendar.MONTH) >= 7 && cal.get(Calendar.MONTH) <= 9) {
CollectionUtil.getLast(result).setMonth("2");
} else if (cal.get(Calendar.MONTH) >= 10 && cal.get(Calendar.MONTH) <= 12) {
CollectionUtil.getLast(result).setMonth("3");
}
result.add(Range.create(dt));
cal.add(Calendar.MONTH, 3);
}
CollectionUtil.getLast(result).end(end);
System.out.println(cal.get(Calendar.MONTH));
if (cal.get(Calendar.MONTH) >= 1 && cal.get(Calendar.MONTH) <= 3) {
CollectionUtil.getLast(result).setMonth("4");
} else if (cal.get(Calendar.MONTH) >= 4 && cal.get(Calendar.MONTH) <= 6) {
CollectionUtil.getLast(result).setMonth("1");
} else if (cal.get(Calendar.MONTH) >= 7 && cal.get(Calendar.MONTH) <= 9) {
CollectionUtil.getLast(result).setMonth("2");
} else if (cal.get(Calendar.MONTH) >= 10 && cal.get(Calendar.MONTH) <= 12) {
CollectionUtil.getLast(result).setMonth("3");
}
return result;
}
/**
* 将时间段按照半年分割
*
* @param start
* @param end
* @return
*/
public static final List splitToHalfOfYears(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MONTH, 6);
Calendar calOne = Calendar.getInstance();
calOne.setTime(end);
int mon = cal.get(Calendar.MONTH);
// Calendar tmp = Calendar.getInstance();
// if (mon < 6) {
// tmp.setTimeInMillis(cal.getTimeInMillis());
// tmp.set(Calendar.MONTH, 5);
// } else {
// tmp.setTimeInMillis(cal.getTimeInMillis());
// tmp.set(Calendar.MONTH, 11);
// }
Calendar halfYearEnd = cn.hutool.core.date.DateUtil.endOfMonth(calOne);
while (cal.getTimeInMillis() <= halfYearEnd.getTimeInMillis()) {
mon = cal.get(Calendar.MONTH);
Calendar cal1 = Calendar.getInstance();
if (mon < 6) {
cal1.setTimeInMillis(cal.getTimeInMillis());
cal1.set(Calendar.MONTH, 0);
} else {
cal1.setTimeInMillis(cal.getTimeInMillis());
cal1.set(Calendar.MONTH, 6);
}
Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal1.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
result.add(Range.create(dt));
cal.add(Calendar.MONTH, 6);
}
CollectionUtil.getLast(result).end(end);
return result;
}
/**
* 将时间段按照年分割
*
* @param start
* @param end
* @return
*/
public static final List splitToYears(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.YEAR, 1);
Date yearEnd = cn.hutool.core.date.DateUtil.endOfYear(end);
while (cal.getTimeInMillis() <= yearEnd.getTime()) {
Date dt = cn.hutool.core.date.DateUtil.beginOfYear(cal.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
result.add(Range.create(dt));
cal.add(Calendar.YEAR, 1);
}
CollectionUtil.getLast(result).end(end);
return result;
}
//日期区间
public static class Range {
Date start;
Date end;
String Month;
public Range() {
}
private Range(Date start) {
this.start = start;
}
public static Range create(Date start) {
return new Range(start);
}
public Range end(Date end) {
this.end = end;
return this;
}
public void setStart(Date start) {
this.start = start;
}
public void setEnd(Date end) {
this.end = end;
}
public Date getStart() {
return start;
}
public Date getEnd() {
return end;
}
public String getMonth() {
return Month;
}
public void setMonth(String month) {
Month = month;
}
@Override
public String toString() {
return "[" + cn.hutool.core.date.DateUtil.format(start, "yyyy-MM-dd") + "," + cn.hutool.core.date.DateUtil.format(end, "yyyy-MM-dd")
+ "]";
}
}
/**
* 根据年,月,第几周获取时间范围
*
* @param year
* @param month
* @param week
* @return
*/
public static Map getWeekDateLimits(String year, String month, String week) {
Map result = new HashMap();
SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.YEAR, Integer.parseInt(year));
c1.set(Calendar.MONTH, Integer.parseInt(month) - 1);
c1.set(Calendar.DAY_OF_WEEK, 2);
c1.set(Calendar.WEEK_OF_MONTH, Integer.parseInt(week));
Date time1 = c1.getTime();
String begin = dft.format(time1);
c1.set(Calendar.DATE, c1.get(Calendar.DATE) + 6);
Date time2 = c1.getTime();
String end = dft.format(time2);
result.put("begin", begin);
result.put("end", end);
return result;
}
/**
* 取得月度天数
*
* @param date
* @return
*/
public static int getDayOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 取得季度天数
*
* @param date
* @return
*/
public static int getDayOfSeason(Date date) {
int day = 0;
Date[] seasonDates = getSeasonDate(date);
for (Date date2 : seasonDates) {
day += getDayOfMonth(date2);
}
return day;
}
/**
* 取得季度月
*
* @param date
* @return
*/
public static Date[] getSeasonDate(Date date) {
Date[] season = new Date[3];
Calendar c = Calendar.getInstance();
c.setTime(date);
int nSeason = getSeason(date);
if (nSeason == 1) {// 第一季度
c.set(Calendar.MONTH, Calendar.JANUARY);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.FEBRUARY);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.MARCH);
season[2] = c.getTime();
} else if (nSeason == 2) {// 第二季度
c.set(Calendar.MONTH, Calendar.APRIL);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.MAY);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.JUNE);
season[2] = c.getTime();
} else if (nSeason == 3) {// 第三季度
c.set(Calendar.MONTH, Calendar.JULY);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.AUGUST);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.SEPTEMBER);
season[2] = c.getTime();
} else if (nSeason == 4) {// 第四季度
c.set(Calendar.MONTH, Calendar.OCTOBER);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.NOVEMBER);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.DECEMBER);
season[2] = c.getTime();
}
return season;
}
/**
* 1 第一季度 2 第二季度 3 第三季度 4 第四季度
*
* @param date
* @return
*/
public static int getSeason(Date date) {
int season = 0;
Calendar c = Calendar.getInstance();
c.setTime(date);
int month = c.get(Calendar.MONTH);
switch (month) {
case Calendar.JANUARY:
case Calendar.FEBRUARY:
case Calendar.MARCH:
season = 1;
break;
case Calendar.APRIL:
case Calendar.MAY:
case Calendar.JUNE:
season = 2;
break;
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.SEPTEMBER:
season = 3;
break;
case Calendar.OCTOBER:
case Calendar.NOVEMBER:
case Calendar.DECEMBER:
season = 4;
break;
default:
break;
}
return season;
}
public static String getDay(Date date) {
return formatDate(date, "yyyy-MM-dd");
}
public static String formatDate(Date date, String pattern) {
String formatDate = null;
if (pattern != null && !pattern.equals("")) {
formatDate = DateFormatUtils.format(date, pattern);
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
public static int betweenDay(Date date1, Date date2) {
int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
return days;
}
/**
* 判断日期是否在同一个月
*
* @param date1
* @param date2
* @return
*/
public static boolean isMonth(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
int year1 = calendar1.get(Calendar.YEAR);
int year2 = calendar2.get(Calendar.YEAR);
int month1 = calendar1.get(Calendar.MONTH);
int month2 = calendar2.get(Calendar.MONTH);
System.out.println(year1 + " " + month1);
System.out.println(year2 + " " + month2);
return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);
}
/**
* 判断日期是否在同一个季度
*
* @param date1
* @param date2
* @return
*/
public static boolean isSeason(Date date1, Date date2) {
if (getSeason(date1) == getSeason(date2)) {
return true;
}
return false;
}
/**
* 判断日期是否在同一个半年
*
* @param date1
* @param date2
* @return
*/
public static boolean isHalfOfYears(Date date1, Date date2) {
int month1 = DateUtil.month(date1);
int month2 = DateUtil.month(date2);
if ((month1 > 6 && month2 > 6) || (month1 <= 6 && month2 <= 6)) {
return true;
}
return false;
}
/**
* 判断日期是否在同一年
*
* @param date1
* @param date2
* @return
*/
public static boolean isYears(Date date1, Date date2) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
int year1 = calendar.get(Calendar.YEAR);
calendar.setTime(date2);
int year2 = calendar.get(Calendar.YEAR);
if (year1 == year2) {
return true;
}
return false;
}
}