UtilTools.java
3.9 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
package com.xkl.tools;
import org.joda.time.DateTime;
import java.sql.Timestamp;
import java.text.ParseException;
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;
}
/**
* 由String转为long型时间,13位
* @param timestamp
* @return
*/
public static long getLongTime(String timestamp){
long time = 0;
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
time = format.parse(timestamp).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
/**
* 获取当前时间long型,10位
* @return
*/
public static long getNow10Second(){
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();
}
public static void main(String[] args) {
System.out.println(getLongTime("2016-10-11 22:22:22"));
}
}