1.获取两月间的所有开始结束日期
/*
* 获取两月间的所有周
* @param beaginTime 开始年月份
* @param endTime 结束年月份
* @return 两月间的所有周区间
*/
List<OmInspectWeekDayVO> getWeeksDayByTwoMonth(String beaginTime, String endTime) throws ParseException {
List<OmInspectWeekDayVO> result = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(beaginTime);
Date date1 = sdf.parse(endTime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
List<String> bothMonth = getMonthBetween(beaginTime, endTime);
bothMonth.forEach(t -> {
getWeeksDay(YearMonth.parse(t)).entrySet().forEach(t1 -> {
if (t1.getValue().getWeekEnd().getDayOfMonth() - t1.getValue().getWeekStart().getDayOfMonth() < 6) {
} else {
result.add(t1.getValue());
}
});
});
List<OmInspectWeekDayVO> result1 = new ArrayList<>();
int startMonths = 0;
for (int i = 0; i < result.size(); i++) {
if (startMonths == 0) {
startMonths = result.get(i).getWeekStart().getMonthValue();
}
if (startMonths == result.get(i).getWeekStart().getMonthValue()) {
result1.add(result.get(i));
}
if (startMonths != result.get(i).getWeekStart().getMonthValue()) {
if (i < result.size() - 1) {
if (result.get(i + 1).getWeekStart().getDayOfMonth() != 1) {
OmInspectWeekDayVO temp = new OmInspectWeekDayVO();
temp.setWeekStart(result.get(i - 1).getWeekEnd().minus(-1, ChronoUnit.DAYS));
temp.setWeekEnd(result.get(i).getWeekStart().minus(1, ChronoUnit.DAYS));
result1.add(temp);
startMonths = result.get(i).getWeekStart().getMonthValue();
} else {
startMonths = result.get(i).getWeekStart().getMonthValue();
}
}
result1.add(result.get(i));
}
if (i == result.size() - 1) {
OmInspectWeekDayVO temp = new OmInspectWeekDayVO();
List<String> nextWeek = getNextWeekDateList(sdf.parse(String.valueOf(result.get(i).getWeekEnd())));
temp.setWeekStart(LocalDate.parse(nextWeek.get(0)));
temp.setWeekEnd(LocalDate.parse(nextWeek.get(nextWeek.size() - 1)));
result1.add(temp);
}
}
@SuppressWarnings("unused")
int startMonth = 0;
int countWeek = 0;
String[] num_lower = {"一", "二", "三", "四", "五", "六"};
for (int i = 0; i < result1.size(); i++) {
if (startMonths == 0) {
startMonths = result1.get(i).getWeekStart().getMonthValue();
}
if (i == result1.size() - 1) {
String xAxis = result1.get(i).getWeekStart().getMonthValue() + "月第" + num_lower[countWeek] + "周";
result1.get(i).setXAxisName(xAxis);
countWeek = 0;
}
if (i < result1.size() - 1) {
if (result1.get(i + 1).getWeekStart().getMonthValue() != result1.get(i).getWeekStart().getMonthValue()) {
startMonth = 0;
String xAxis = result1.get(i).getWeekStart().getMonthValue() + "月第" + num_lower[countWeek] + "周";
result1.get(i).setXAxisName(xAxis);
countWeek = 0;
} else {
result1.get(i).getWeekStart().getMonthValue();
String xAxis = result1.get(i).getWeekStart().getMonthValue() + "月第" + num_lower[countWeek] + "周";
result1.get(i).setXAxisName(xAxis);
countWeek = countWeek + 1;
}
}
}
;
return result1;
}
/**
*
* @author zhh
*/
@Data
@EqualsAndHashCode(callSuper=false)
@ApiModel(value = "实体类", description = "实体类")
public class OmInspectWeekDayVO {
@ApiModelProperty(value = "一周的开始时间", name = "start", dataType = "Long", example = "", position = 1)
private LocalDate weekStart;
@ApiModelProperty(value = "一周的结束时间", name = "start", dataType = "Long", example = "", position = 1)
private LocalDate weekEnd;
@ApiModelProperty(value = "图表x轴名称", name = "xAxisName", dataType = "Long", example = "", position = 1)
private String xAxisName;
public OmInspectWeekDayVO() {
}
public OmInspectWeekDayVO(List<LocalDate> localDates) {
this.weekStart = localDates.get(0);
this.weekEnd = localDates.get(localDates.size()-1);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List<String> getNextWeekDateList(){
Calendar cal1 = Calendar.getInstance();
Calendar cal2 =Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal1.setTime(new Date());
cal2.setTime(new Date());
// 获得当前日期是一个星期的第几天
int dayWeek = cal1.get(Calendar.DAY_OF_WEEK);
if(dayWeek == 1){
cal1.add(Calendar.DAY_OF_MONTH, 1);
cal2.add(Calendar.DAY_OF_MONTH, 7);
} else {
cal1.add(Calendar.DAY_OF_MONTH, 1-dayWeek+8);
cal2.add(Calendar.DAY_OF_MONTH, 1-dayWeek+14);
}
Calendar cStart = Calendar.getInstance();
cStart.setTime(cal1.getTime());
List dateList = new ArrayList();
//别忘了,把起始日期加上
dateList.add(sdf.format(cal1.getTime()));
// 此日期是否在指定日期之后
while (cal2.getTime().after(cStart.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cStart.add(Calendar.DAY_OF_MONTH, 1);
dateList.add(sdf.format(cStart.getTime()));
}
return dateList;
}
输入:
输出:
2.获取日期所在周的所有日期
/**
* 获取日期所在周的所有日期
* @param date
* @return
*/
@Override
public List<String> getWeekDays(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
list.add(sdf.format(cal.getTime()));
for (int i = 1; i < 7; i++) {
cal.add(Calendar.DAY_OF_MONTH, 1);
list.add(sdf.format(cal.getTime()));
}
return list;
}
输入:
输出:
3.获取两个月间的所有月份集合
/**
* 获取两月间的月份
*
* @param minDate 开始月份
* @param maxDate 结束月份
*/
@Override
public 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;
}
输入:
输出:
4.获取某日下个星期的日期
/**
* 获取某日下个星期的日期
*
* @param time 某日
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public List<String> getNextWeekDateList(Date time) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal1.setTime(time);
cal2.setTime(time);
// 获得当前日期是一个星期的第几天
int dayWeek = cal1.get(Calendar.DAY_OF_WEEK);
if (dayWeek == 1) {
cal1.add(Calendar.DAY_OF_MONTH, 1);
cal2.add(Calendar.DAY_OF_MONTH, 7);
} else {
cal1.add(Calendar.DAY_OF_MONTH, 1 - dayWeek + 8);
cal2.add(Calendar.DAY_OF_MONTH, 1 - dayWeek + 14);
}
Calendar cStart = Calendar.getInstance();
cStart.setTime(cal1.getTime());
List dateList = new ArrayList();
//别忘了,把起始日期加上
dateList.add(sdf.format(cal1.getTime()));
// 此日期是否在指定日期之后
while (cal2.getTime().after(cStart.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cStart.add(Calendar.DAY_OF_MONTH, 1);
dateList.add(sdf.format(cStart.getTime()));
}
return dateList;
}
第一行输入,第二行输出
1、Calendar根据日期获取年份和周
直接上代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalendarTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
getYearAndWeek(cal.getTime());
}
/**
* 获取日期所在的年和周
* @param date
* @return
*/
public static WeekYear getYearAndWeek(Date date) {
Calendar calendar = Calendar.getInstance();
// 设置一周的第一天为周一
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
int year = calendar.get(Calendar.YEAR);
// 处理跨年
calendar.add(Calendar.DAY_OF_MONTH, -7);
int yearBefore = calendar.get(Calendar.YEAR);
int weekBefore = calendar.get(Calendar.WEEK_OF_YEAR);
if (week < weekBefore) {
yearBefore += 1;
year = yearBefore;
}
System.out.println(year + "年第" + week + "周");
return new WeekYear(year, week);
}
}
class WeekYear {
private Integer year;
private Integer week;
public WeekYear(Integer year, Integer week) {
this.year = year;
this.week = week;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Integer getWeek() {
return week;
}
public void setWeek(Integer week) {
this.week = week;
}
@Override
public String toString() {
return "WeekYear{" +
"year=" + year +
", week=" + week +
'}';
}
}
运行结果:
2020年第50周
如果想获取过去2周和未来四周的年份和日期,代码如下:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
List<WeekYear> wys = new ArrayList<>(8);
cal.add(Calendar.DATE, -14);
wys.add(getYearAndWeek(cal.getTime()));
for (int i = 0; i < 6; i++) {
cal.add(Calendar.DATE, +7);
wys.add(getYearAndWeek(cal.getTime()));
}
}
运行结果如下:
2020年第48周
2020年第49周
2020年第50周
2020年第51周
2020年第52周
2021年第1周
2021年第2周
这里我们设置一周的第一天为周一,上面的结果可以看出第三周时跨年了,测试时会发现2020-12-28 Calendar返回的是周是1,但是2020年,所以这里需要特殊处理下,应该是2021年第一周。
2、当前周的所有日期
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalendarTest {
/**
* 获取日期所在周的所有日期
* @param date
* @return
*/
public static List<String> getWeekDays(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
list.add(sdf.format(cal.getTime()));
for (int i = 1; i < 7; i++) {
cal.add(Calendar.DAY_OF_MONTH, 1);
list.add(sdf.format(cal.getTime()));
}
return list;
}
public static void main(String[] args) throws ParseException {
System.out.println(getWeekDays(new Date()));
}
}
输出结果:
[2020-12-07, 2020-12-08, 2020-12-09, 2020-12-10, 2020-12-11, 2020-12-12, 2020-12-13]
再验证获取2020-12-28所在周的所有日期
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse("2020-12-28"));
System.out.println(getWeekDays(cal.getTime()));
}
输出结果:
[2020-12-28, 2020-12-29, 2020-12-30, 2020-12-31, 2021-01-01, 2021-01-02, 2021-01-03]
————————————————