/**
• 查询一段时间内的折线图
•
• @param typeId 查询类型(1:今日,2:7天,3:30天,4:全部,5:其他自定义)
• @param userId
• @param startTime
• @param endTime
• @return
*/
@Override
public Map<String, Object> getAddTrendLineChart(Integer typeId, Long userId, String startTime, String endTime) throws Exception {
//如果是查询今日的
if (typeId.equals(ReasonEnum.REASON1.getCode())) {
Date dayBegin = NewDateUtil.getDayBegin();
return getTodayChart(dayBegin, userId);
} else if (typeId.equals(ReasonEnum.REASON2.getCode())) {
//查询最近7天的
return getWeekChart(userId, 6);
} else if (typeId.equals(ReasonEnum.REASON3.getCode())) {
//查询最近30天的
return getWeekChart(userId, 29);
} else if (typeId.equals(ReasonEnum.REASON4.getCode())) {
//查询全部的
String endTime2 = NewDateUtil.dateToString(new Date(System.currentTimeMillis()), “yyyy-MM-dd HH:mm:ss”);
String startTime2 = NewDateUtil.dateToString(new Date(1546272000000L), “yyyy-MM-dd HH:mm:ss”);
return getCustomizeChart(userId, startTime2, endTime2);
} else if (typeId.equals(ReasonEnum.REASON5.getCode())) {
//其他自定义
if (StringUtil.isNotNull(startTime) && StringUtil.isNotNull(endTime) && startTime.equals(endTime)) {
Date dayBegin = NewDateUtil.stringToDate(startTime, “yyyy-MM-dd HH:mm:ss”);
return getTodayChart(dayBegin, userId);
} else {
return getCustomizeChart(userId, startTime, endTime);
}
}
throw new RrkException(“typeId查询类型必填,且只能为(1:今日,2:7天,3:30天,4:全部,5:其他自定义)”);
}
/**
• 其他自定义时间内的
•
• @param userId
• @param startTime
• @param endTime
• @return
• @throws Exception
*/
private Map<String, Object> getCustomizeChart(Long userId, String startTime, String endTime) throws Exception {
Map<String, Object> map = new HashMap<>(4);
Date frontDay = NewDateUtil.stringToDate(startTime, “yyyy-MM-dd HH:mm:ss”);
Date endDay = NewDateUtil.stringToDate(endTime, “yyyy-MM-dd HH:mm:ss”);
// System.out.println(“frontDay=” + frontDay);
// System.out.println(“endDay=” + endDay);
List timeList2 = NewDateUtil.findDates(frontDay, endDay);
List dateList = new ArrayList<>(12);
List fansCount = new ArrayList<>(12);
for (int i = 0; i < timeList2.size() - 1; i++) {
List fansList = getFansCount(userId, NewDateUtil.getDayStartTime2(timeList2.get(i)), NewDateUtil.getDayEndTime2(timeList2.get(i)));
fansCount.add(fansList.size());
String formatDate = DateUtils.formatDate(timeList2.get(i), “yyyy-MM-dd HH:mm:ss”);
dateList.add(formatDate);
// System.out.println(timeList2.get(i));
// System.out.println(“frontDay=” + NewDateUtil.getDayStartTime2(timeList2.get(i)));
// System.out.println(“endDay=” + NewDateUtil.getDayEndTime2(timeList2.get(i)));
}
map.put(“dateList”, dateList);
map.put(“fansCount”, fansCount);
return map;
}
/**
• 查询最近7天的
•
• @param userId
• @return
*/
private Map<String, Object> getWeekChart(Long userId, int count) {
Map<String, Object> map = new HashMap<>(4);
Date frontDay = NewDateUtil.getFrontDay(new Date(), count);
// System.out.println(“frontDay=” + frontDay);
List timeList2 = NewDateUtil.findDates(frontDay, new Date());
List dateList = new ArrayList<>(12);
List fansCount = new ArrayList<>(12);
for (int i = 0; i < timeList2.size() - 1; i++) {
Date dayStartTime = NewDateUtil.getDayStartTime2(timeList2.get(i));
Date dayEndTime = NewDateUtil.getDayEndTime2(timeList2.get(i));
List fansList = getFansCount(userId, dayStartTime, dayEndTime);
fansCount.add(fansList.size());
String formatDate = DateUtils.formatDate(timeList2.get(i), “yyyy-MM-dd HH:mm:ss”);
dateList.add(formatDate);
// System.out.println(timeList2.get(i));
}
map.put(“dateList”, dateList);
map.put(“fansCount”, fansCount);
return map;
}
/**
• 查询今日的
•
• @param userId
• @return
*/
private Map<String, Object> getTodayChart(Date dateBegin, Long userId) throws Exception {
Map<String, Object> map = new HashMap<>(4);
//获取今天内的各时间点
List todayHours = NewDateUtil.getTodayHours(dateBegin, 4);
List dateList = new ArrayList<>(12);
List fansCount = new ArrayList<>(12);
for (int i = 0; i < todayHours.size() - 1; i++) {
List fansList = getFansCount(userId, todayHours.get(i), todayHours.get(i + 1));
fansCount.add(fansList.size());
String formatDate = DateUtils.formatDate(todayHours.get(i + 1), “yyyy-MM-dd HH:mm:ss”);
dateList.add(formatDate);
}
map.put(“dateList”, dateList);
map.put(“fansCount”, fansCount);
return map;
}
用到的日期工具类
//获取某个日期的开始时间
public static Date getDayStartTime2(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
//获取某个日期的结束时间
public static Date getDayEndTime2(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
//JAVA获取某段时间内的所有日期
public static List findDates(Date dStart, Date dEnd) {
Calendar cStart = Calendar.getInstance();
cStart.setTime(dStart);
List dateList = new ArrayList<>();
//别忘了,把起始日期加上
dateList.add(dStart);
// 此日期是否在指定日期之后
while (dEnd.after(cStart.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cStart.add(Calendar.DAY_OF_MONTH, 1);
dateList.add(cStart.getTime());
}
return dateList;
}
// string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH时mm分ss秒,
// strTime的时间格式必须要与formatType的时间格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
/**
• 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:“yyyy-MM-dd” “HH:mm:ss” “E”
*/
public static String formatDate(Date date, String pattern) {
String formatDate = null;
if (date != null){
// if (StringUtils.isNotBlank(pattern)) {
// formatDate = DateFormatUtils.format(date, pattern);
// } else {
// formatDate = DateFormatUtils.format(date, “yyyy-MM-dd”);
// }
if (StringUtils.isBlank(pattern)) {
pattern = “yyyy-MM-dd”;
}
formatDate = FastDateFormat.getInstance(pattern).format(date);
}
return formatDate;
}
java查找集合对象中日期属性是否有间隔 java根据时间段查询数据
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章