按日,周,月,季,年统计;获取对应的时间段
1.周实体类:WeekEntity.java
package com.test.common.entity;
import java.time.LocalDate;
public class WeekEntity {
private String day;
/**
* 开始日期
**/
private LocalDate startTime;
/**
* 结束日期
**/
private LocalDate endTime;
/**
* 开始日期数字化
**/
private Integer startTimeNumber;
/**
* 结束日期数字化
**/
private Integer endTimeNumber;
/**
* XXXX年第XXX周
**/
private String yearAndWeek;
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public LocalDate getStartTime() {
return startTime;
}
public void setStartTime(LocalDate startTime) {
this.startTime = startTime;
}
public LocalDate getEndTime() {
return endTime;
}
public void setEndTime(LocalDate endTime) {
this.endTime = endTime;
}
public Integer getStartTimeNumber() {
return startTimeNumber;
}
public void setStartTimeNumber(Integer startTimeNumber) {
this.startTimeNumber = startTimeNumber;
}
public Integer getEndTimeNumber() {
return endTimeNumber;
}
public void setEndTimeNumber(Integer endTimeNumber) {
this.endTimeNumber = endTimeNumber;
}
public String getYearAndWeek() {
return yearAndWeek;
}
public void setYearAndWeek(String yearAndWeek) {
this.yearAndWeek = yearAndWeek;
}
@Override
public String toString() {
return "WeekEntity{" +
"day='" + day + '\'' +
", startTime=" + startTime +
", endTime=" + endTime +
", startTimeNumber=" + startTimeNumber +
", endTimeNumber=" + endTimeNumber +
", yearAndWeek='" + yearAndWeek + '\'' +
'}';
}
}
2.日期工具类,有各个时间段的日期数据
package com.test.common.utils;
import cn.hutool.core.date.DateUtil;
import com.test.common.entity.WeekEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.*;
import java.util.stream.Collectors;
/**
* 日期处理
*
* @author
* @email
* @date
*/
public class DateUtils extends PropertyEditorSupport {
/**
* 时间格式(yyyy-MM-dd)
*/
public final static String DATE_PATTERN = "yyyy-MM-dd";
/**
* 时间格式(yyyyMMdd)
*/
public final static String PATTERN = "yyyyMMdd";
/**
* 时间格式(yyyy-MM-dd HH:mm:ss)
*/
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 时间格式(yyyyMMddHHmmss)
*/
public final static String DATE_TIME_PATTERN_RULE = "yyyyMMddHHmmss";
/**
* 时间格式(yyyy-MM)
*/
public final static String DATE_YEAR_MONTH = "yyyy-MM";
/**
* 时间格式(yyyy-MM)
*/
public final static String DATE_YEAR = "yyyy";
/**
* 得到当前日期字符串 格式(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
*
* @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 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();
}
/**
* 得到当前时间的前N小时
*
* @param ihour
* @return
*/
public static String getBeforeByHourTime(int ihour) {
String returnstr = "";
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnstr = df.format(calendar.getTime());
return returnstr;
}
// 获取当前时间和6小时前的时间
public static Map<String, Object> dealTime(int hour) {
Calendar calendar = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
calendar2.add(Calendar.MINUTE, 1); //往前1分钟
String etime = simpleDateFormat2.format(calendar2.getTime());
int a = Calendar.HOUR_OF_DAY;
calendar.set(Calendar.HOUR_OF_DAY, calendar.get((Calendar.HOUR_OF_DAY)) - hour);
String stime = simpleDateFormat.format(calendar.getTime());
Map<String, Object> map = new HashMap<String, Object>();
map.put("stime", stime);
map.put("etime", etime);
return map;
}
/**
* lxl
*
* 返回两个时间段之间的所有日期
*
* @param beginDate
* @param endDate
*/
public static List<String> getDatesBetweenTwoDate(String beginDate, String endDate)
throws ParseException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> lDate = new ArrayList<String>();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
} catch (ParseException e) {
e.printStackTrace();
}
String dateString = sdf.format(date);
lDate.add(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
boolean bContinue = true;
while (bContinue) {
cal.add(Calendar.DAY_OF_MONTH, 1);
if (sdf.parse(endDate).after(cal.getTime())) {
lDate.add(sdf.format(cal.getTime()));
} else {
break;
}
}
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
} catch (ParseException e) {
e.printStackTrace();
}
String entrs = sdf.format(date);
if (!dateString.equals(entrs)) {
lDate.add(entrs);
}
return lDate;
}
//返回两个时间段之间的所有月包含最后一个月
public static List<String> getMonthBetweenTwoDateIncludeEndDate(String beginDate, String endDate)
throws ParseException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
List<String> lDate = new ArrayList<String>();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM").parse(beginDate);
} catch (ParseException e) {
e.printStackTrace();
}
String dateString = sdf.format(date);
lDate.add(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
boolean bContinue = true;
while (bContinue) {
cal.add(Calendar.MONTH, 1);
if (sdf.parse(endDate).after(cal.getTime())) {
lDate.add(sdf.format(cal.getTime()));
} else {
break;
}
}
try {
date = new SimpleDateFormat("yyyy-MM").parse(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
String entrs = sdf.format(date);
if (!dateString.equals(entrs)) {
lDate.add(entrs);
}
return lDate;
}
//返回两个时间段之间的所有日期包含最后一天
public static List<String> getDatesBetweenTwoDateIncludeEndDate(String beginDate, String endDate)
throws ParseException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> lDate = new ArrayList<String>();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
} catch (ParseException e) {
e.printStackTrace();
}
String dateString = sdf.format(date);
lDate.add(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
boolean bContinue = true;
while (bContinue) {
cal.add(Calendar.DAY_OF_MONTH, 1);
if (sdf.parse(endDate).after(cal.getTime())) {
lDate.add(sdf.format(cal.getTime()));
} else {
break;
}
}
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
String entrs = sdf.format(date);
if (!dateString.equals(entrs)) {
lDate.add(entrs);
}
return lDate;
}
/**
* lxl
*
* 获得两个时间段之内的所有日期小时:例如传参数:"2018-12-06 01"和"2018-12-06 23", 返回结果:[2018-12-06 01,
* 2018-12-06 02, 2018-12-06 03, 2018-12-06 04, ......, 2018-12-06 23]
*
* @param beginDate
* @param endDate
*/
public static List<String> getHoursBetweenTwoDate(String beginDate, String endDate)
throws ParseException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
List<String> lDate = new ArrayList<String>();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(beginDate);
} catch (ParseException e) {
e.printStackTrace();
}
String dateString = sdf.format(date);
lDate.add(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
boolean bContinue = true;
while (bContinue) {
cal.add(Calendar.HOUR, 1);
if (sdf.parse(endDate).after(cal.getTime())) {
lDate.add(sdf.format(cal.getTime()));
} else {
break;
}
}
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
String endstr = sdf.format(date);
lDate.add(endstr);
return lDate;
}
/**
* lxl
* 获得两个时间段内所有的分钟,例如:传参“2018-11-20 00:00”和“2018-11-22 23:59”, 将会返回["2018-11-20
* 00:01","2018-11-20 00:02","2018-11-20 00:03"......,"2018-11-22
* 23:58","2018-11-22 23:59"]
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getMinuteBetweenTwoDate(String beginDate, String endDate)
throws ParseException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
List<String> lDate = new ArrayList<String>();
lDate.add(beginDate);// 把开始时间加入集合
Calendar cal = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
cal.setTime(sdf.parse(beginDate));
boolean bContinue = true;
while (bContinue) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cal.add(Calendar.MINUTE, 1);
// 测试此日期是否在指定日期之后
if (sdf.parse(endDate).after(cal.getTime())) {
lDate.add(sdf.format(cal.getTime()));
} else {
break;
}
}
lDate.add(endDate);// 把结束时间加入集合
return lDate;
}
/**
* 获得某天所有的分钟
*
* @param day
* @return
* @throws ParseException
* @throws ParseException
*/
public static List<String> getMinutes(String day) throws ParseException, ParseException {
List<String> minuteList = getMinuteBetweenTwoDate(day + " 00:00", day + " 23:59");
return minuteList;
}
/**
* 获得i以内的随机整数
*
* @param i
* @return
*/
public static String getSecond(int i) {
long l = Math.round(Math.random() * i);
if (l < 10) {
return "0" + l;
} else {
if (l != i) {
return "" + l;
} else {
return l - 1 + "";
}
}
}
/**
* 获得i以内的随机数,保留两位小数
*
* @param i
* @return
*/
public static String getRandom(int i) {
double d = Math.random() * i;
return String.format("%.2f", d);
}
//获取两个时间之间的小时数
public static int getDayNum(String stime, String etime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = null;
Date date = null;
try {
now = df.parse(etime);
date = df.parse(stime);
} catch (ParseException e) {
e.printStackTrace();
}
long l = now.getTime() - date.getTime();
int day = (int) l / (24 * 60 * 60 * 1000);
int hour = (int) (l / (60 * 60 * 1000));
int min = (int) ((l / (60 * 1000)));
int s = (int) (l / 1000);
// System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
return hour;
}
//获取两个时间之间的小时数
public static int getHourNums(String stime, String etime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = null;
Date date = null;
try {
now = df.parse(etime);
date = df.parse(stime);
} catch (ParseException e) {
e.printStackTrace();
}
long l = now.getTime() - date.getTime();
int day = (int) l / (24 * 60 * 60 * 1000);
int hour = (int) (l / (60 * 60 * 1000));
int min = (int) ((l / (60 * 1000)));
int s = (int) (l / 1000);
// System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
return hour;
}
//获取两个时间之间的天数
public static int getDayNums(String stime, String etime) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = null;
Date date = null;
try {
now = df.parse(etime);
date = df.parse(stime);
} catch (ParseException e) {
e.printStackTrace();
}
long l = now.getTime() - date.getTime();
int day = (int) l / (24 * 60 * 60 * 1000);
int hour = (int) (l / (60 * 60 * 1000));
int min = (int) ((l / (60 * 1000)));
int s = (int) (l / 1000);
// System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
return day;
}
/**
* lxl
* 返回两个时间段之间的所有日期
* 动态设置时间格式
*
* @param beginDate
* @param endDate
*/
public static List<String> getDatesBetweenT(String beginDate, String endDate, String simpleFormat)
throws ParseException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(simpleFormat);
List<String> lDate = new ArrayList<String>();
Date date = null;
try {
date = new SimpleDateFormat(simpleFormat).parse(beginDate);
} catch (ParseException e) {
e.printStackTrace();
}
String dateString = sdf.format(date);
lDate.add(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
boolean bContinue = true;
while (bContinue) {
cal.add(Calendar.DAY_OF_MONTH, 1);
if (sdf.parse(endDate).after(cal.getTime())) {
lDate.add(sdf.format(cal.getTime()));
} else {
break;
}
}
try {
date = new SimpleDateFormat(simpleFormat).parse(beginDate);
} catch (ParseException e) {
e.printStackTrace();
}
String entrs = sdf.format(date);
if (!dateString.equals(entrs)) {
lDate.add(entrs);
}
return lDate;
}
/**
* @param minDate
* @param maxDate
* @return
* @throws ParseException lxl
* 获得两段时间内的所有月份
*/
public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(maxDate));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
return result;
}
/**
* @return
* @throws ParseException lxl
* 返回两个时期 共有多少天。
*/
public static int daysOfTwo_2(String stime, String etime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
/**
* 跨年不会出现问题
*/
Date fDate = sdf.parse(stime);
Date oDate = sdf.parse(etime);
Long a = ((oDate.getTime() - fDate.getTime()) / (1000 * 3600 * 24));
int days = a.intValue();
return days;
}
/**
* 获得当前系统时间
*/
public static String getNowDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date());
}
/**
* 获得月份的所有日期
*/
public static int getDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 获取某年第一天日期
*
* @param year 年份
* @return Date
*/
public static Date getCurrYearFirst(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}
/**
* 获取某年最后一天日期
*
* @param year 年份
* @return Date
*/
public static Date getCurrYearLast(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
Date currYearLast = calendar.getTime();
return currYearLast;
}
/**
* 获取指定日期分钟
*
* @param date
* @return
*/
public static int getRightMinute(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MINUTE);
}
/**
* 获取指定时间 加上/减去指定的分钟
*
* @return
*/
public static Date getRightDate(Date date, int minute) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MINUTE, minute);
return c.getTime();
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到日期字符串 默认格式(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;
}
/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* @Description: 获取一个日期的所在月的开始和结束时间"2016-02-05 00:00:00"->"2016-02-05 00:00:00" "2016-02-05 00:00:00"
* @Author: cjk
* @CreateDate: 2019/8/14
*/
public static Map<String,Object> getYearToMonthStartEnd(String dateStr){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
Calendar c1 = Calendar.getInstance();
try {
c.setTime(sdf.parse(dateStr));
c.add(Calendar.MONTH, 0);
//设置为1号,当前日期既为本月第一天
c.set(Calendar.DAY_OF_MONTH, 1);
c1.setTime(sdf.parse(dateStr));
c1.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
c1.set(Calendar.HOUR_OF_DAY, 23);
c1.set(Calendar.MINUTE, 59);
c1.set(Calendar.SECOND, 59);
} catch (ParseException e) {
e.printStackTrace();
}
Map<String,Object> map = new HashMap<>();
map.put("stime",DateUtils.formatDate(c.getTime(),"yyyy-MM-dd HH:mm:ss"));
map.put("etime",DateUtils.formatDate(c1.getTime(),"yyyy-MM-dd HH:mm:ss"));
return map;
}
/**
* 根据开始时间,结束时间 获取中间所有周
*
* @param startTime
* @param endTime
* @return
*/
public static List<WeekEntity> getWeekList(java.time.LocalDate startTime, java.time.LocalDate endTime) {
List<WeekEntity> resultList = new ArrayList<WeekEntity>();
java.time.format.DateTimeFormatter df = java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd");//判断是否同一周
WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 4);
if (startTime.get(weekFields.weekOfWeekBasedYear()) == endTime.get(weekFields.weekOfWeekBasedYear())) {
WeekEntity firstWeek = new WeekEntity();
firstWeek.setDay(startTime + "~" + endTime);
firstWeek.setStartTime(startTime);
firstWeek.setEndTime(endTime);
firstWeek.setStartTimeNumber(Integer.valueOf(df.format(startTime)));
firstWeek.setEndTimeNumber(Integer.valueOf(df.format(endTime)));
resultList.add(firstWeek);
return resultList;
}//开始周
TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
java.time.LocalDate startFirstWeek = startTime.with(FIRST_OF_WEEK); //开始周开始日期
TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
java.time.LocalDate endFirstWeek = startTime.with(LAST_OF_WEEK); //开始周结束日期
//结束周
TemporalAdjuster FIRST_OF_WEEK1 = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
java.time.LocalDate startLastWeek = endTime.with(FIRST_OF_WEEK1);
TemporalAdjuster LAST_OF_WEEK1 = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
java.time.LocalDate endLastWeek = endTime.with(LAST_OF_WEEK1);//将第一周添加
WeekEntity firstWeek = new WeekEntity();
firstWeek.setDay(startTime + "~" + endFirstWeek);
firstWeek.setStartTime(startTime);
firstWeek.setEndTime(endFirstWeek);
firstWeek.setStartTimeNumber(Integer.valueOf(df.format(startTime)));
firstWeek.setEndTimeNumber(Integer.valueOf(df.format(endFirstWeek)));
resultList.add(firstWeek);
while (true) {
startFirstWeek = startFirstWeek.plusDays(7);
if (startFirstWeek.with(LAST_OF_WEEK).equals(startLastWeek.with(LAST_OF_WEEK1))) {
break;
} else {
WeekEntity week = new WeekEntity();
week.setDay(startFirstWeek.with(FIRST_OF_WEEK) + "~" + startFirstWeek.with(LAST_OF_WEEK));
week.setStartTime(startFirstWeek.with(FIRST_OF_WEEK));
week.setEndTime(startFirstWeek.with(LAST_OF_WEEK));
week.setStartTimeNumber(Integer.valueOf(df.format(startFirstWeek.with(FIRST_OF_WEEK))));
week.setEndTimeNumber(Integer.valueOf(df.format(startFirstWeek.with(LAST_OF_WEEK))));
resultList.add(week);//System.out.println("日期="+startFirstWeek+"开始周="+startFirstWeek.with(FIRST_OF_WEEK)+"结束周="+startFirstWeek.with(LAST_OF_WEEK));
}
}
WeekEntity lastWeek = new WeekEntity();
lastWeek.setDay(startLastWeek + "~" + endTime);
lastWeek.setStartTime(startLastWeek);
lastWeek.setEndTime(endTime);
lastWeek.setStartTimeNumber(Integer.valueOf(df.format(startLastWeek)));
lastWeek.setEndTimeNumber(Integer.valueOf(df.format(endTime)));
resultList.add(lastWeek);
return resultList;
}
/**
* 字符串转LocalDate
* @param date
* @return
*/
public static java.time.LocalDate getLocalDateByDateStr(String date) {
Instant instant = DateUtil.parse(date).toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
java.time.LocalDate localDate = localDateTime.toLocalDate();
return localDate;
}
/**
* 根据日期获取所属第几周
* @param date
* @return
*/
public static int getWeekNum(String date) {
Instant instant = DateUtil.parse(date).toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
java.time.LocalDate localDate = localDateTime.toLocalDate();
// WeekFields.ISO = 一星期从周一开始算, 其它的请自己摸索.
WeekFields weekFields = WeekFields.ISO;
int weekNumber = localDate.get(weekFields.weekOfWeekBasedYear());
return weekNumber;
}
/**
* 根据日期获取所属第几周
* @param date
* @return
*/
public static String getWeekAndYear(Date date){
int year = DateUtil.year(date);
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
java.time.LocalDate localDate= localDateTime.toLocalDate();
// WeekFields.ISO = 一星期从周一开始算, 其它的请自己摸索.
WeekFields weekFields = WeekFields.ISO;
int weekNumber = localDate.get(weekFields.weekOfWeekBasedYear());
return year + "年第" + weekNumber + "周";
}
/**
* 获取时间段内所有季度
*
* @param startTime 开始时间
* @param endTime 结束时间
*/
public static List<String> getSeasonList(java.time.LocalDate startTime, java.time.LocalDate endTime) {
// 取当月第一天, 避免startTime的日期大于endTime计算不出来的情况
startTime = java.time.LocalDate.of(startTime.getYear(), startTime.getMonthValue(), 1);
endTime = java.time.LocalDate.of(endTime.getYear(), endTime.getMonthValue(), 1);
Set<String> set = new HashSet<>();
java.time.LocalDate mark = startTime;
while (true) {
if (mark.isBefore(endTime) || mark.isEqual(endTime)) {
String season = String.valueOf(mark.getYear()) + String.valueOf((mark.getMonthValue() + 2) / 3);
set.add(season);
// 加一个月
mark = mark.plusMonths(1);
} else {
break;
}
}
// set中是倒序, 重新排序
return set.stream().sorted().collect(Collectors.toList());
}
/**
* 获取两个日期之间的所有年
*
* @param startTime
* @param endTime
* @return:list
*/
public static List<String> getYearBetweenDate(String startTime, String endTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
// 声明保存日期集合
List<String> list = new ArrayList<>();
try {
// 转化成日期类型
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
//用Calendar 进行日期比较判断
Calendar calendar = Calendar.getInstance();
while (startDate.getTime() <= endDate.getTime()) {
// 把日期添加到集合
list.add(sdf.format(startDate));
// 设置日期
calendar.setTime(startDate);
//把年数增加 1
calendar.add(Calendar.YEAR, 1);
// 获取增加后的日期
startDate = calendar.getTime();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 根据日期获取季度
* @param dateStr
* @return
*/
public static String getYearAndQuarter(String dateStr){
Date date = DateUtil.parse(dateStr);
int year = DateUtil.year(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH)+1;
int quarter = (month-1)/3 + 1;
return year + "" + quarter;
// return year + "年" + quarter + "季度";
}
/**
* 根据日期获取季度
* @param dateStr
* @return
*/
public static String convertQuarter(String dateStr){
if(dateStr.length() == 5) {
return dateStr.substring(0,4) + "年" + dateStr.substring(4,5) + "季度";
}else{
return "";
}
}
/**
* 根据yyyy-MM-dd日期获取年份
* @param date
* @return
*/
public static String getYear(String date){
String[] yearArray = date.split("-");
return yearArray[0];
}
}
PropertyEditorSupport.java没有实际使用可去掉;