文章目录
前言
统计图中周,月,年报表
一、本周日期集合
public static List<String> getWeekDateList() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayWeek == 1) {
dayWeek = 8;
}
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
Date mondayDate = cal.getTime();
cal.add(Calendar.DATE, 4 + cal.getFirstDayOfWeek());
Date sundayDate = cal.getTime();
List lDate = new ArrayList();
lDate.add(format.format(mondayDate));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(mondayDate);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(sundayDate);
//测试此日期是否在指定日期之后
while (sundayDate.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(format.format(calBegin.getTime()));
}
return lDate;
}
输出结果:
public static void main(String[] args) throws Exception {
System.out.println(getWeekDateList());
}
二、获取本月日期集合
/***
* 获取某年某月的所有日期
* @param year
* @param month
* @return
*/
public static List<String> getMonthFullDay(int year, int month) {
SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
List<String> fullDayList = new ArrayList<>(32);
// 获得当前日期对象
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH, month - 1);
// 当月1号
cal.set(Calendar.DAY_OF_MONTH, 1);
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int j = 1; j <= count; j++) {
fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH, 1);
}
return fullDayList;
}
输出结果
public static void main(String[] args) throws Exception {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
System.out.println(getMonthFullDay(year, month));
}
[2022-04-01, 2022-04-02, 2022-04-03, 2022-04-04, 2022-04-05, 2022-04-06, 2022-04-07, 2022-04-08, 2022-04-09, 2022-04-10, 2022-04-11, 2022-04-12, 2022-04-13, 2022-04-14, 2022-04-15, 2022-04-16, 2022-04-17, 2022-04-18, 2022-04-19, 2022-04-20, 2022-04-21, 2022-04-22, 2022-04-23, 2022-04-24, 2022-04-25, 2022-04-26, 2022-04-27, 2022-04-28, 2022-04-29, 2022-04-30]
三、获取本年月份
/**
* 获得12个月时间戳
*/
public static List<String> getMonthStr() throws Exception {
List<String> list = new ArrayList<String>();
Long sj = System.currentTimeMillis();//时间戳
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy");
String dateStr = dateformat.format(sj);
for (int i = 1; i <= 12; i++) {
List<Object> li = new ArrayList<Object>();
li.add(dateStr + "-" + i);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String mydate = dateStr + "-" + i;
Date datetime = sdf.parse(mydate);
list.add(sdf.format(datetime));
}
return list;
}
输出结果
public static void main(String[] args) throws Exception {
System.out.println(getMonthStr());
}
总结
package com.hvit.user.util;
import org.springframework.util.CollectionUtils;
import java.text.SimpleDateFormat;
import java.util.*;
public class GetDateKeyValueUtils {
private int inAmount = 10;//初始值
private int outAmount = 15;
public static void main(String[] args) throws Exception {
//System.out.println(getDateBy7(7));
List<Map<String, Object>> maps = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("date", "2022-04-19");
map.put("count", 22);
Map<String, Object> map1 = new HashMap<>();
map1.put("date", "2022-04-18");
map1.put("count", 18);
Map<String, Object> map2 = new HashMap<>();
map2.put("date", "2022-04-17");
map2.put("count", 17);
Map<String, Object> map3 = new HashMap<>();
map3.put("date", "2022-04-16");
map3.put("count", 16);
Map<String, Object> map4 = new HashMap<>();
map4.put("date", "2022-04-15");
map4.put("count", 15);
Map<String, Object> map5 = new HashMap<>();
map5.put("date", "2022-04-14");
map5.put("count", 14);
maps.add(map);
maps.add(map1);
maps.add(map2);
maps.add(map3);
maps.add(map4);
maps.add(map5);
//getDateKey("2", maps);
//System.out.println(getWeekDateList());
//Calendar now = Calendar.getInstance();
///int year = now.get(Calendar.YEAR);
//int month = now.get(Calendar.MONTH) + 1;
//System.out.println(getMonthFullDay(year, month));
System.out.println(getMonthStr());
}
/***
* 获取周,月,年 key-value
* @param type
* @param maps
* @return
*/
public static Map<String, Object> getDateKey(String type, List<Map<String, Object>> maps) throws Exception {
Map<String, Object> map = new LinkedHashMap<>();
if ("1".equals(type)) {
getWeekDateList().forEach(date -> {
map.put(date.toString(), 0);
});
if (!CollectionUtils.isEmpty(maps)) {
maps.forEach(value -> {
if (map.containsKey(value.get("date"))) {
map.put(value.get("date").toString(), value.get("count"));
}
});
}
} else if ("2".equals(type)) {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
getMonthFullDay(year, month).forEach(date -> {
map.put(date.toString(), 0);
});
if (!CollectionUtils.isEmpty(maps)) {
maps.forEach(value -> {
if (map.containsKey(value.get("date"))) {
map.put(value.get("date").toString(), value.get("count"));
}
});
}
} else if ("3".equals(type)) {
getMonthStr().forEach(date -> {
map.put(date.toString(), 0);
});
if (!CollectionUtils.isEmpty(maps)) {
maps.forEach(value -> {
if (map.containsKey(value.get("date"))) {
map.put(value.get("date").toString(), value.get("count"));
}
});
}
}
return map;
}
/**
* 获取过去或者未来 任意天内的日期数组
*
* @param intervals intervals天内
* @return 日期数组
*/
public static ArrayList getDateBy7(int intervals) {
ArrayList pastDaysList = new ArrayList<>();
for (int i = 0; i < intervals; i++) {
pastDaysList.add(getPastDate(i));
}
Collections.sort(pastDaysList);
return pastDaysList;
}
public static List<String> getWeekDateList() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayWeek == 1) {
dayWeek = 8;
}
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
Date mondayDate = cal.getTime();
cal.add(Calendar.DATE, 4 + cal.getFirstDayOfWeek());
Date sundayDate = cal.getTime();
List lDate = new ArrayList();
lDate.add(format.format(mondayDate));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(mondayDate);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(sundayDate);
//测试此日期是否在指定日期之后
while (sundayDate.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(format.format(calBegin.getTime()));
}
return lDate;
}
/**
* 获取过去第几天的日期
*
* @param past
* @return
*/
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
return result;
}
/**
* 获取未来 第 past 天的日期
*
* @param past
* @return
*/
public static String getFetureDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
return result;
}
/***
* 获取某年某月的所有日期
* @param year
* @param month
* @return
*/
public static List<String> getMonthFullDay(int year, int month) {
SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
List<String> fullDayList = new ArrayList<>(32);
// 获得当前日期对象
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH, month - 1);
// 当月1号
cal.set(Calendar.DAY_OF_MONTH, 1);
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int j = 1; j <= count; j++) {
fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH, 1);
}
return fullDayList;
}
/**
* 获得12个月时间戳
*/
public static List<String> getMonthStr() throws Exception {
List<String> list = new ArrayList<String>();
Long sj = System.currentTimeMillis();//时间戳
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy");
String dateStr = dateformat.format(sj);
for (int i = 1; i <= 12; i++) {
List<Object> li = new ArrayList<Object>();
li.add(dateStr + "-" + i);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String mydate = dateStr + "-" + i;
Date datetime = sdf.parse(mydate);
list.add(sdf.format(datetime));
}
return list;
}
}