根据任意某天,获取所在月份的每一天及星期
import java.util.*;
import java.util.stream.Collectors;
/**
* @author : 秦淮
* create at: 2021/1/13 10:27
* @description: 获取月份的每一天及星期
*/
public class GetMonthWeek {
public static void main(String[] args) {
Date date = new Date();
Map<Date, String> map = getAllDateOfMonth(date);
for (Date d : map.keySet()) {
System.out.println("日期:" + d + ",星期:" + map.get(d));
}
}
/**
* 获取当月所有日期, 星期
*
* @param date Date
* @return Map<Date, String> key:日期 value:星期
*/
public static Map<Date, String> getAllDateOfMonth(Date date) {
Map<Date, String> map = new HashMap<>(32);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
int month = cal.get(Calendar.MONTH);
String[] weeks = {"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
while (cal.get(Calendar.MONTH) == month) {
int number = cal.get(Calendar.DAY_OF_WEEK);
map.put(cal.getTime(), weeks[number]);
cal.add(Calendar.DATE, 1);
}
// 按照日期排序
Map<Date, String> sortMap = map.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oleValue, newValue) -> oleValue, LinkedHashMap::new));
return sortMap;
}
}
获取当前日期所在一周日期, 星期
https://blog.csdn.net/weixin_42919267/article/details/112608772