package com.conb.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
/**
*
* @description 时间补零工具类
* @author haixia.shi
* @date 2017年7月27日
*
*/
@Repository
public class SupplementUtils {
/**
* 单个数据按日期补零(格式:2017-01-01)
*
* @param startTime
* 开始时间
* @param endTime
* 结束时间
* @param concent
* 数据名称
* @return
*/
public static List<Map<String, Object>> supplementByDay(String startTime, String endTime, String concent) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date start = null;
Date end = null;
try {
start = simpleDateFormat.parse(startTime);
end = simpleDateFormat.parse(endTime);
} catch (ParseException e) {
return null;
}
Integer days = compare(start, end);
List<Map<String, Object>> reusltList = new ArrayList<>();
for (int i = 0; i < days + 1; i++) {
Map<String, Object> map1 = new HashMap<String, Object>();
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.DAY_OF_MONTH, 1);
String time = simpleDateFormat.format(start);
map1.put("createTime", time);
map1.put(concent, 0);
reusltList.add(map1);
start = cal.getTime();
}
return reusltList;
}
public static int compare(Date one, Date two) {
Long num1 = one.getTime();
Long num2 = two.getTime();
return (int) ((num2 - num1) / (24 * 3600 * 1000));
}
/**
* 单个数据按小时补零(格式:23:00)
*
* @param startTime
* 开始时间
* @param endTime
* 结束时间
* @param concent
* 数据名称
* @return
*/
public static List<Map<String, Object>> supplementByHour(String startTime, String endTime, String concent) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = null;
Integer hours = 23;
Calendar calender = Calendar.getInstance();
try {
Date date = new Date();
String curDate = sdf.format(date);
start = sdf.parse(startTime);
if (curDate.equals(startTime)) {
hours = calender.get(Calendar.HOUR_OF_DAY);
}
} catch (ParseException e) {
return null;
}
List<Map<String, Object>> reusltList = new ArrayList<>();
for (int j = 0; j < hours + 1; j++) {
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.HOUR_OF_DAY, 1);
Map<String, Object> map1 = new HashMap<String, Object>();
String time = simpleDateFormat.format(start);
map1.put("createTime", time);
map1.put(concent, 0);
reusltList.add(map1);
start = cal.getTime();
}
return reusltList;
}
/**
* 单个数据按小时补零(格式:23:59)
*
* @param startTime
* 开始时间
* @param endTime
* 结束时间
* @param concent
* 数据名称
* @return
*/
public static List<Map<String, Object>> supplementByHourFull(String startTime, String endTime, String concent) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = null;
Integer hours = 23;
Calendar calender = Calendar.getInstance();
try {
Date date = new Date();
String curDate = sdf.format(date);
start = sdf.parse(startTime);
if (curDate.equals(startTime)) {
hours = calender.get(Calendar.HOUR_OF_DAY);
}
} catch (ParseException e) {
return null;
}
Integer second = 59;
List<Map<String, Object>> reusltList = new ArrayList<>();
for (int j = 0; j < hours + 1; j++) {
Calendar cal = Calendar.getInstance();
cal.setTime(start);
for (int i = 0; i < second + 1; i++) {
Map<String, Object> map1 = new HashMap<String, Object>();
cal.add(Calendar.MINUTE, 1);
String time = simpleDateFormat.format(start);
map1.put("createTime", time);
map1.put(concent, 0);
reusltList.add(map1);
start = cal.getTime();
}
cal.add(Calendar.HOUR_OF_DAY, 1);
}
return reusltList;
}
public static int compareHour(Date one, Date two) {
Long num1 = one.getTime();
Long num2 = two.getTime();
return (int) ((num2 - num1) / (3600 * 1000));
}
}
日期时间补零工具类
最新推荐文章于 2024-07-31 04:25:06 发布