时间工具类(慢慢完善)

根据开始时间和结束时间,生成期间每一天的日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class DateTimeUtil {
/**
* @Description: 根据开始时间和结束时间,生成期间每一天的日期
*/
public static String[] dateArray(String startTime, String endTime) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
int total = DateTimeUtil.caculateTotalTime(startTime, endTime);
if (total < 0) {
throw new Exception("错误:时间相差天数为负数");
}
String[] dataArray = new String[total];
for (int i = 0; i < total; i++) {
Calendar c = Calendar.getInstance();
c.setTime(formatter.parse(startTime));
c.add(Calendar.DAY_OF_MONTH, i);
dataArray[i] = formatter.format(c.getTime());
}
return dataArray;

}

/**
* @Description: 根据开始时间和结束时间,获取相差天数
*/
public static int caculateTotalTime(String startTime, String endTime) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(startTime);
long ts = date.getTime();
Date date1 = formatter.parse(endTime);
long ts1 = date1.getTime();
long ts2 = ts1 - ts;
int totalTime = 0;
totalTime = (int) (ts2 / (24 * 3600 * 1000) + 1);
return totalTime;
}
}
赏个🍗吧
0%