/**
*
* 获取 年-月-日
* 年/周
* 年-月
* 年
* @param type 1 天 2 周 3 月 4 年
* @param dateStart 开始时间戳
* @param dateEnd 结束时间戳
* */
public static List<String> getTimeGroupString(Integer type, Long dateStart, Long dateEnd) {
List<String> dateList = new ArrayList<String>();
//逐日打印日期
LocalDate startDate = Instant.ofEpochMilli(dateStart).atZone(ZoneOffset.ofHours(8)).toLocalDate();
LocalDate endDate = Instant.ofEpochMilli(dateEnd).atZone(ZoneOffset.ofHours(8)).toLocalDate();
/*
几种方法用来判断日期,isBefore()、isAfter()、isEqual()
*/
if (type ==1 || type ==2 || type == 3 || type == 4){
getDateString(type, dateList, startDate);
while (startDate.isBefore(endDate)) {
if (type == 1) {
startDate = startDate.plusDays(1);
}
if (type == 2) {
startDate = startDate.plusDays(1);
}
if (type == 3) {
startDate = startDate.plusMonths(1);
}
if (type == 4) {
startDate = startDate.plusYears(1);
}
getDateString(type, dateList, startDate);
}
}
return dateList;
}
private static void getDateString(Integer type, List<String> dateList, LocalDate startDate) {
//天
if (type == 1) {
dateList.add(startDate.toString());
}
//年周
if (type == 2) {
// 第一个参数:一周的第一天,不能为空
// 第二个参数:第一周的最小天数,从1到7
WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
int weekOfYear = startDate.get(weekFields.weekOfYear());
String data = startDate.getYear() + "/" + weekOfYear;
if (!dateList.contains(data)){
dateList.add(data);
}
}
//年月
if (type == 3) {
String data = startDate.getYear() + "-" + startDate.getMonthValue();
dateList.add(data);
}
//年
if (type == 4) {
dateList.add(String.valueOf(startDate.getYear()));
}
}