UtilTools.java
3.24 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
package com.xkl.tools;
import org.joda.time.DateTime;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* Created by win7 on 2016/11/20.
*/
public class UtilTools {
/**
* support time format yyyy-MM-dd'T'HH:mm:ss.SSS
*
* @param timestamp
* @return
* @throws Exception
*/
public static int getTime(String timestamp) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.CHINA);
Date time = format.parse(timestamp);
SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMdd");
return Integer.parseInt(dayFormat.format(time));
}
public static Timestamp getTimestamp(String timestamp) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.CHINA);
Date time = format.parse(timestamp);
return new Timestamp(time.getTime());
}
public static DateTime getDateTime(String timestamp) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
Date time = format.parse(timestamp);
return new DateTime(time.getTime());
}
public static int getDay(Timestamp timestamp) {
SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMdd");
return Integer.parseInt(dayFormat.format(new Date(timestamp.getTime())));
}
public static int getHour(Timestamp timestamp) {
SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMddHH");
return Integer.parseInt(dayFormat.format(new Date(timestamp.getTime())));
}
public static long get13Second(){
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
long a=c.getTimeInMillis();
return a;
}
public static long get10Second(){
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
long a=c.getTimeInMillis()/1000;
return a;
}
public static long _long(String value) {
if (value == null || "null".equals(value)) {
return 0;
}
try {
return Long.parseLong(value.toString());
} catch (Exception e) {
return 0;
}
}
public static long _long(Object value) {
if (value == null || "null".equals(value)) {
return 0;
}
try {
return Long.parseLong(value.toString());
} catch (Exception e) {
return 0;
}
}
public static int _int(String value) {
if (value == null || "null".equals(value)) {
return 0;
}
try {
return Integer.parseInt(value.toString());
} catch (Exception e) {
return 0;
}
}
public static Double _double(String value) {
if (value == null || "null".equals(value)) {
return 0.0;
}
try {
return Double.parseDouble(value.toString());
} catch (Exception e) {
return 0.0;
}
}
public static String _string(Object object) {
if (object == null || "null".equals(object)) {
return "";
}
return object.toString();
}
}