DateUtils.java
1.72 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
package com.sincere.common.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author chen
* @version 1.0
* @date 2019/10/14 0014 14:16
*/
public class DateUtils {
public static String format = "yyyy";
public static String format1 = "yyyy-MM-dd";
public static String format2 = "yyyy-MM-dd HH:mm:ss";
public static long getDate(){
return System.currentTimeMillis();
}
public static String date2String(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
public static Date string2Date(String date, String format) {
try{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(date);
}catch (Exception e){
}
return new Date();
}
public static Date getToday(){
return new Date();
}
public static String getToday(String format) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
public static int getDateDifference(Date date1, Date date2, String timeType) {
int between = (int) (date1.getTime() - date2.getTime());
int difference = 0;
switch (timeType) {
case "day":
difference = between / (24 * 60 * 60 * 1000);
break;
case "hour":
difference = between / (60 * 60 * 1000);
break;
case "min":
difference = between / (60 * 1000);
break;
case "s":
difference = between / 1000;
}
return difference;
}
}