1)指定一个起始时间,指定需要获取数据的类型(年、月、日、小时),就可以获得从起始时间开始到当前时间的具体年、月、日、小时的数据。
2)工具类如下:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author dengtian
* @date 2021年06月22日11:11
*/
public class GetInitializationTime {
/**
* 获得每个月的天数
* 输入格式:2021-01
*
* @param year
* @param month
* @return
*/
public static int geMonthNum(int year, int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 根据年获得一年的月份
* 输入格式:2021
*
* @param year
* @return
* @throws ParseException
*/
public static List<String> getMonthsByYear(String year) {
List<String> result = new ArrayList<>();
//格式化初始时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//格式化结果时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
Date parse = null;
try {
parse = sdf.parse(year);
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
for (int i = 0; i <= 12; i++) {
calendar.set(Calendar.MONTH, i);
result.add(simpleDateFormat.format(calendar.getTime()));
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
/**
* 获得一个月每天的时间
* 输入格式:2021-01
*
* @param date
* @return
* @throws ParseException
*/
public static List<String> getDaysByMonth(String date) {
List<String> result = new ArrayList<>();
//格式化初始时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//格式化结果时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = null;
try {
parse = sdf.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
//根据月份获得一个月的最大天数
int maxDay = geMonthNum(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH));
for (int i = 1; i <= maxDay; i++) {
calendar.set(Calendar.DAY_OF_MONTH, i);
result.add(simpleDateFormat.format(calendar.getTime()));
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
/**
* 根据天获得一天的开始时间
* 输入格式:2020-01-01
*
* @param date
* @return
* @throws ParseException
*/
public static List<String> getStartHoursTimeByDay(String date) {
List<String> startTimeLists = new ArrayList<>();
//格式化初始时间
SimpleDateFormat stringToDate = new SimpleDateFormat("yyyy-MM-dd");
//格式化结果时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null;
try {
parse = stringToDate.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
for (int i = 0; i <= 23; i++) {
calendar.set(Calendar.HOUR_OF_DAY, i);
startTimeLists.add(simpleDateFormat.format(calendar.getTime()));
}
} catch (ParseException e) {
e.printStackTrace();
}
return startTimeLists;
}
/**
* 根据天获得一天的结束时间
* 输入格式:2020-01-01
*
* @param date
* @return
* @throws ParseException
*/
public static List<String> getEndHoursTimeByDay(String date) {
List<String> endTimeLists = new ArrayList<>();
//格式化初始时间
SimpleDateFormat stringToDate = new SimpleDateFormat("yyyy-MM-dd");
//格式化结果时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null;
try {
parse = stringToDate.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
for (int i = 0; i <= 23; i++) {
calendar.set(Calendar.HOUR_OF_DAY, i);
endTimeLists.add(simpleDateFormat.format(calendar.getTime()));
}
} catch (ParseException e) {
e.printStackTrace();
}
return endTimeLists;
}
/**
* 比较两个日期是否相等
* 输入可为:2021、2021-01、2021-01-01、2021-01-01 01:01:01
*
* @param date1 比较时间
* @param date2 若date2没传,则是默认和当前时间进行比较,若是传了,则是和传入的时间进行比较
* @return 返回-1,则date1的时间在前面;返回1,则date1的时间在后面,返回0则表示两个时间相等
*/
public static Integer compareToTime(String date1, String date2) {
SimpleDateFormat sdf = null;
if (date1.length() > 10) {
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else if (date1.length() > 7) {
sdf = new SimpleDateFormat("yyyy-MM-dd");
} else if (date1.length() > 4) {
sdf = new SimpleDateFormat("yyyy-MM");
} else {
sdf = new SimpleDateFormat("yyyy");
}
Date parse1 = null;
Date parse2 = null;
try {
parse1 = sdf.parse(date1);
if (date2 == null || "".equals(date2.trim())) {
Date date = new Date();
String format = sdf.format(date);
parse2 = sdf.parse(format);
} else {
parse2 = sdf.parse(date2);
}
} catch (ParseException e) {
e.printStackTrace();
}
return parse1.compareTo(parse2);
}
/**
* 获得当前时间
*
* @param date
* @return
*/
public static String getNowTime(Date date, String type) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String parse = sdf.format(date);
Calendar instance = Calendar.getInstance();
try {
instance.setTime(sdf.parse(parse));
} catch (ParseException e) {
e.printStackTrace();
}
if (type.equals("year")) {
return String.valueOf(instance.get(Calendar.YEAR));
} else if (type.equals("month")) {
int i = instance.get(Calendar.YEAR);
int i1 = instance.get(Calendar.MONTH) + 1;
String month = null;
if (i1 < 10) {
month = "0" + i1;
} else {
month = i1 + "";
}
return i + "-" + month;
} else if (type.equals("day")) {
int i = instance.get(Calendar.YEAR);
int i1 = instance.get(Calendar.MONTH) + 1;
String month = null;
if (i1 < 10) {
month = "0" + i1;
} else {
month = i1 + "";
}
String day = null;
int i2 = instance.get(Calendar.DAY_OF_MONTH);
if (i2 < 10) {
day = "0" + i2;
} else {
day = i2 + "";
}
return i + "-" + month + "-" + day;
} else {
return parse + " 00:00:00";
}
}
/**
* 传入时间,指定获得的时间类型
*
* @param startTime 2021-01-01
* @param type year,month,day,hour
* @return
*/
public static Map<String, Set<String>> getTimeList(String startTime, String type) {
Map<String, Set<String>> result = new HashMap<>();
//检查年的时候,注意传入的年的值为:2021
if (type.equals("year")) {
//当前 年的时间
String yearByDate = GetInitializationTime.getNowTime(new Date(), "year");
Set<String> years = getYears(startTime.substring(0, 4), yearByDate);
if (years != null && years.size() > 0) {
result.put("year", years);
}
return result;
} else if (type.equals("month")) { //检查年,获得起始年月到当前年月的 年月时间
//先截取出年
String substring = startTime.substring(0, 4);
String yearByDate = GetInitializationTime.getNowTime(new Date(), "year");
String nowMonth = GetInitializationTime.getNowTime(new Date(), "month");
//获得年的集合
Set<String> years = getYears(substring, yearByDate);
//开始时间
String start = startTime.substring(0, 7);
//获得月
Set<String> monthsByYear = getMonthsByYear(years, start, nowMonth);
result.put("month", monthsByYear);
return result;
} else if (type.equals("day")) {
//根据起始时间,获得从起始时间到当前时间的天的时间
String startDay = startTime.substring(0, 10);
String nowDay = GetInitializationTime.getNowTime(new Date(), "day");
//拿到年的集合
Set<String> years = getYears(startTime.substring(0, 4), nowDay.substring(0, 4));
//根据年的集合得到月的集合
Set<String> monthsByYear = getMonthsByYear(years, startTime.substring(0, 7), nowDay.substring(0, 7));
//获得天
Set<String> dayByMonth = getDaysByMonth(monthsByYear, startDay, nowDay);
result.put("day", dayByMonth);
return result;
} else {
Set<String> hourList = new HashSet<>();
String start = startTime;
String end = GetInitializationTime.getNowTime(new Date(), "hour");
//拿到年的集合
Set<String> years = getYears(start.substring(0, 4), end.substring(0, 4));
//根据年的集合得到月的集合
Set<String> monthsByYear = getMonthsByYear(years, start.substring(0, 7), end.substring(0, 7));
//获得天
Set<String> dayByMonth = getDaysByMonth(monthsByYear, start, end);
dayByMonth.forEach(s -> {
List<String> endDayTimeByDay = GetInitializationTime.getStartHoursTimeByDay(s);
hourList.addAll(endDayTimeByDay);
});
result.put("hour", hourList);
return result;
}
}
/**
* 传入起始的年月日和当前的年月日,得到从起始年月日到当前年月日的日集合
*
* @param monthsByYear
* @param startDay
* @param nowDay
* @return
*/
private static Set<String> getDaysByMonth(Set<String> monthsByYear, String startDay, String nowDay) {
Set<String> result = new HashSet<>();
//循环年月
monthsByYear.forEach(s -> {
Set<String> days = new HashSet<>();
List<String> dayByMonth = GetInitializationTime.getDaysByMonth(s);
//筛选在范围内的天
dayByMonth.forEach(s2 -> {
if (GetInitializationTime.compareToTime(s2, startDay) != -1 && GetInitializationTime.compareToTime(s2, nowDay) != 1) {
days.add(s2);
}
});
result.addAll(days);
});
return result;
}
/**
* 传入起始年月和当前年月,得到从起始年月到当前年月的月集合
*
* @param years
* @param start
* @param nowMonth
* @return
*/
private static Set<String> getMonthsByYear(Set<String> years, String start, String nowMonth) {
Set<String> result = new HashSet<>();
if (years != null && years.size() > 0) {
years.forEach(s -> {
List<String> monthByYear = new ArrayList<>();
//根据年,拿到月的集合
monthByYear = GetInitializationTime.getMonthsByYear(s);
//对month进行排序 保险
List<String> collect = monthByYear.stream().sorted().collect(Collectors.toList());
collect.forEach(s1 -> {
if (GetInitializationTime.compareToTime(s1, start) != -1 && GetInitializationTime.compareToTime(s1, nowMonth) != 1) {
result.add(s1);
}
});
});
}
return result;
}
/**
* 传入起始年和当前年,获得年的集合
*
* @param startTime
* @param nowTime
* @return
*/
private static Set<String> getYears(String startTime, String nowTime) {
Set<String> time = new HashSet<>();
if (GetInitializationTime.compareToTime(startTime, nowTime) == -1 || GetInitializationTime.compareToTime(startTime, nowTime) == 0) {
Integer tem = null;
tem = Integer.valueOf(startTime);
while (tem <= Integer.valueOf(nowTime)) {
time.add(String.valueOf(tem));
tem++;
}
time.add(nowTime);
} else if (GetInitializationTime.compareToTime(startTime, String.valueOf(nowTime)) == 0) {
time.add(String.valueOf(nowTime));
} else {
time.clear();
}
return time;
}
}
3)调用结果测试
/**
* @author dengtian
* @date 2021年06月25日12:02
*/
@SpringBootTest
public class TeseDemo04 {
@Test
void test1(){
System.out.println("获得当前年的时间:"+GetInitializationTime.getNowTime(new Date(), "year"));//获得当前年的时间:2021
System.out.println("获得当前月的时间:"+GetInitializationTime.getNowTime(new Date(), "month"));//获得当前月的时间:2021-06
System.out.println("获得当前天的时间:"+GetInitializationTime.getNowTime(new Date(), "day"));//获得当前天的时间:2021-06-26
//小时比较特殊,因业务需要,我直接初始化成当天的00:00:00,可按需进行修改
System.out.println("获得当前小时的时间:"+GetInitializationTime.getNowTime(new Date(), "hour"));//获得当前小时的时间:2021-06-26 00:00:00
}
@Test
void test2(){
System.out.println("获得2021年的每个月月份:"+GetInitializationTime.getMonthsByYear("2021"));
//[2021-01, 2021-02, 2021-03, 2021-04, 2021-05, 2021-06, 2021-07, 2021-08, 2021-09, 2021-10, 2021-11, 2021-12, 2022-01]
System.out.println("根据月份获得这个月的天数:"+GetInitializationTime.getDaysByMonth("2021-01"));
// [2021-01-01, 2021-01-02, 2021-01-03, 2021-01-04, 2021-01-05, 2021-01-06, 2021-01-07, 2021-01-08,
// 2021-01-09, 2021-01-10, 2021-01-11, 2021-01-12, 2021-01-13, 2021-01-14, 2021-01-15, 2021-01-16,
// 2021-01-17, 2021-01-18, 2021-01-19, 2021-01-20, 2021-01-21, 2021-01-22, 2021-01-23, 2021-01-24,
// 2021-01-25, 2021-01-26, 2021-01-27, 2021-01-28, 2021-01-29, 2021-01-30, 2021-01-31]
System.out.println("根据天获得一天的开始时刻:"+GetInitializationTime.getStartHoursTimeByDay("2021-01-01"));
// [2021-01-01 00:00:00, 2021-01-01 01:00:00, 2021-01-01 02:00:00, 2021-01-01 03:00:00, 2021-01-01 04:00:00,
// 2021-01-01 05:00:00, 2021-01-01 06:00:00, 2021-01-01 07:00:00, 2021-01-01 08:00:00, 2021-01-01 09:00:00,
// 2021-01-01 10:00:00, 2021-01-01 11:00:00, 2021-01-01 12:00:00, 2021-01-01 13:00:00, 2021-01-01 14:00:00,
// 2021-01-01 15:00:00, 2021-01-01 16:00:00, 2021-01-01 17:00:00, 2021-01-01 18:00:00, 2021-01-01 19:00:00,
// 2021-01-01 20:00:00, 2021-01-01 21:00:00, 2021-01-01 22:00:00, 2021-01-01 23:00:00]
System.out.println("根据天获得一天的结束时刻:"+GetInitializationTime.getEndHoursTimeByDay("2021-01-01"));
// [2021-01-01 00:59:59, 2021-01-01 01:59:59, 2021-01-01 02:59:59, 2021-01-01 03:59:59, 2021-01-01 04:59:59,
// 2021-01-01 05:59:59, 2021-01-01 06:59:59, 2021-01-01 07:59:59, 2021-01-01 08:59:59, 2021-01-01 09:59:59,
// 2021-01-01 10:59:59, 2021-01-01 11:59:59, 2021-01-01 12:59:59, 2021-01-01 13:59:59, 2021-01-01 14:59:59,
// 2021-01-01 15:59:59, 2021-01-01 16:59:59, 2021-01-01 17:59:59, 2021-01-01 18:59:59, 2021-01-01 19:59:59,
// 2021-01-01 20:59:59, 2021-01-01 21:59:59, 2021-01-01 22:59:59, 2021-01-01 23:59:59]
}
@Test
void test3(){
System.out.println("获得每个月的月总天数:"+GetInitializationTime.geMonthNum(2021, 01));//获得每个月的月总天数:31
}
//综合,因为作了去重,结果有点乱序,想要排序的可以利用java8的新特性stream流进行排序即可
@Test
void test4(){
System.out.println("获得起始时间到当前时间年的数据:"+GetInitializationTime.getTimeList("2018-03-05", "year"));
//获得起始时间到当前时间年的数据:{year=[2019, 2018, 2021, 2020]}
System.out.println("获得起始时间到当前时间月的数据:"+GetInitializationTime.getTimeList("2018-03-05", "month"));
// 获得起始时间到当前时间月的数据:{month=[2018-04, 2020-06, 2018-05, 2020-05, 2018-06, 2020-04, 2018-07, 2020-03, 2020-02, 2019-12,
// 2020-01, 2018-03, 2019-11, 2019-10, 2020-09, 2020-08, 2020-07, 2018-08, 2018-09, 2019-06, 2019-05, 2021-06, 2019-08, 2021-05,
// 2019-07, 2021-04, 2019-02, 2018-11, 2021-03, 2019-01, 2018-12, 2021-02, 2020-12, 2019-04, 2021-01, 2020-11, 2019-03, 2020-10,
// 2018-10, 2019-09]}
//小时和天的数据太多,就不展示,可自行运行查看结果
System.out.println("获得起始时间到当前时间日的数据:"+GetInitializationTime.getTimeList("2018-03-05", "day"));
System.out.println("获得起始时间到当前时间小时的数据:"+GetInitializationTime.getTimeList("2018-03-05", "hour"));
}
@Test
void test5(){
/**
* 若是,传入的第一个参数时间在第二个参数时间之前,则返回-1
* 若是,传入的第一个参数时间等于第二个参数的时间,则返回0
* 若是,传入的第一个参数时间在第二个参数时间之后,返回1
*/
System.out.println(GetInitializationTime.compareToTime("2021-01-01 02:00:00", "2021-01-01 01:00:00"));//1
}
}
按需进行方法调整,我只是记录业务中自己封装的工具类