DateTimeUtils.java
1.6 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.sunyo.wlpt.dispatch.utils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author 子诚
* Description:
* 时间:2020/5/6 21:16
*/
public class DateTimeUtils {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 获取年月日,date类型
* @param s
* @return
*/
public static Date getDate(String s){
Date time = null;
if(s == null){
return time;
}
try {
if (s.length()>0){
time = simpleDateFormat.parse(s);
}
}catch (Exception e){
e.printStackTrace();
}
return time;
}
/**
* 获取年月日,String类型
* @param date
* @return
*/
public static String getDateString(Date date){
return simpleDateFormat.format(date);
}
/**
* 获取当前年月日时分秒,String类型
* @param date
* @return
*/
public static String getDateTimeString(Date date){
return simpleDateTimeFormat.format(date);
}
public static List getSeverDay(){
List<Date> dates = new ArrayList<>();
Date date = new Date();
dates.add(date);
for (int i=1;i<7;i++) {
long time = date.getTime() - i*24 * 3600 * 1000L;
Date preTime = new Date(time);
dates.add(preTime);
}
return dates;
}
}