-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTimeUtil.java
More file actions
166 lines (149 loc) · 5.08 KB
/
SimpleTimeUtil.java
File metadata and controls
166 lines (149 loc) · 5.08 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleTimeUtil {
private static SimpleDateFormat sdf;
private static SimpleDateFormat sdf2;
/**
*
* @param mss
* @return
*/
public static String formatDuring(long mss) {
if (mss <= 0) {
return "00:00:00";
}
StringBuffer sb = new StringBuffer();
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
sb.append(addZero(days, false)).append(addZero(hours, true)).append(addZero(minutes, true)).append(addZero(seconds, true));
String str = sb.toString();
str = str.substring(0, str.length() - 1);
return str;
}
private static String addZero(long time, boolean b) {
String timeStr;
if (time < 1 && !b) {
timeStr = "";
} else if (time >= 1 && time < 10 || time <= 0) {
if (!b) {
timeStr = time + "天";
} else {
timeStr = "0" + time + ":";
}
} else {
timeStr = time + ":";
}
return timeStr;
}
/**
*
* @param begin
* 时间段的开始
* @param end
* 时间段的结束
* @return 输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
* @author fy.zhang
*/
public static String formatDuring(Date begin, Date end) {
return formatDuring(end.getTime() - begin.getTime());
}
public static Date dateToLong(String formatDate, String date) throws ParseException {
sdf = new SimpleDateFormat(formatDate);
Date dt = sdf.parse(date);
return dt;
}
public static Date dateToLong(String date) throws ParseException {
sdf2 = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
Date dt = sdf2.parse(date);
return dt;
}
public static String longToDate(String time) {
Date date = new Date(Long.parseLong(time));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
time = formatter.format(date);
return time;
}
public static Long formatDuring(String start, String end) throws ParseException {
Date date1 = dateToLong(start);
Date date2 = dateToLong(end);
return date2.getTime() - date1.getTime();
}
/**
* string类型转换为long类型 strTime要转换的String类型的时间 formatType时间格式
* strTime的时间格式和formatType的时间格式必须相同
*/
public static long stringToLong(String strTime, String formatType) throws ParseException {
Date date = stringToDate(strTime, formatType); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
/**
* string类型转换为long类型 strTime要转换的String类型的时间 formatType时间格式
* strTime的时间格式和formatType的时间格式必须相同
*/
public static long stringToLong(String strTime) throws ParseException {
Date date = stringToDate(strTime, "yyyy-MM-dd HH:mm:ss"); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
/**
* date类型转换为String类型 formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
* data Date类型的时间
*/
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
/**
* long类型转换为String类型 currentTime要转换的long类型的时间 formatType要转换的string类型的时间格式
*/
public static String longToString(long currentTime, String formatType) throws ParseException {
Date date = longToDate(currentTime, formatType); // long类型转成Date类型
String strTime = dateToString(date, formatType); // date类型转成String
return strTime;
}
/**
* long类型转换为String类型 currentTime要转换的long类型的时间 formatType要转换的string类型的时间格式
*/
public static String longToString(long currentTime) throws ParseException {
Date date = longToDate(currentTime, "yyyy-MM-dd HH:mm:ss"); // long类型转成Date类型
String strTime = dateToString(date, "yyyy-MM-dd HH:mm:ss"); // date类型转成String
return strTime;
}
/**
* string类型转换为date类型 strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd
* HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒, strTime的时间格式必须要与formatType的时间格式相同
*/
public static Date stringToDate(String strTime, String formatType) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
/**
* long转换为Date类型 currentTime要转换的long类型的时间 formatType要转换的时间格式yyyy-MM-dd
* HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
*/
public static Date longToDate(long currentTime, String formatType) throws ParseException {
Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
return date;
}
/**
* date类型转换为long类型 date要转换的date类型的时间
*/
public static long dateToLong(Date date) {
return date.getTime();
}
}