1
1
package org .openiot .lsm .utils ;
2
- /**
3
- * Copyright (c) 2011-2014, OpenIoT
4
- *
5
- * This file is part of OpenIoT.
6
- *
7
- * OpenIoT is free software: you can redistribute it and/or modify
8
- * it under the terms of the GNU Lesser General Public License as published by
9
- * the Free Software Foundation, version 3 of the License.
10
- *
11
- * OpenIoT is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- * GNU Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public License
17
- * along with OpenIoT. If not, see <http://www.gnu.org/licenses/>.
18
- *
19
- * Contact: OpenIoT mailto: info@openiot.eu
20
- */
21
2
3
+ /**
4
+ * Copyright (c) 2011-2014, OpenIoT
5
+ *
6
+ * This file is part of OpenIoT.
7
+ *
8
+ * OpenIoT is free software: you can redistribute it and/or modify it under the
9
+ * terms of the GNU Lesser General Public License as published by the Free
10
+ * Software Foundation, version 3 of the License.
11
+ *
12
+ * OpenIoT is distributed in the hope that it will be useful, but WITHOUT ANY
13
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
+ * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15
+ * details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with OpenIoT. If not, see <http://www.gnu.org/licenses/>.
19
+ *
20
+ * Contact: OpenIoT mailto: info@openiot.eu
21
+ */
22
22
import java .text .ParseException ;
23
23
import java .text .SimpleDateFormat ;
24
24
import java .util .Calendar ;
25
25
import java .util .Date ;
26
+ import java .util .logging .Level ;
27
+ import org .slf4j .Logger ;
28
+ import org .slf4j .LoggerFactory ;
29
+
26
30
/**
27
- *
31
+ *
28
32
* @author Hoan Nguyen Mau Quoc
29
- *
33
+ *
30
34
*/
31
35
public class DateUtil {
32
-
36
+
33
37
/**
34
38
* @param time the format should be like:"yyyyMMddHHmmss"
35
39
* @return the Date
@@ -47,7 +51,7 @@ public static Date fullFormatDigits2Date(String time){
47
51
}
48
52
return date ;
49
53
}
50
-
54
+
51
55
/**
52
56
* parse the time to string under the format.
53
57
* @param time
@@ -64,19 +68,19 @@ public static String date2FormatString(Date time, String format){
64
68
}
65
69
return result ;
66
70
}
67
-
71
+
68
72
public static Date RFC822Section5Format_to_Date (String time ){
69
73
String format = "EEE, dd MMM yyyy hh:mm a" ;
70
74
Date date = string2Date (time ,format );
71
75
return date ;
72
76
}
73
-
77
+
74
78
public static Date RFC822WUnderGroundFormat_to_date (String time ){
75
79
String format = "EEE, dd MMM yyyy hh:mm:ss" ;
76
80
Date date = string2Date (time ,format );
77
81
return date ;
78
82
}
79
-
83
+
80
84
public static boolean isRFC822WUnderGroundFormat (String time ){
81
85
String regex = "EEE, dd MMM yyyy hh:mm:ss" ;
82
86
SimpleDateFormat sdf = new SimpleDateFormat (regex );
@@ -87,15 +91,15 @@ public static boolean isRFC822WUnderGroundFormat(String time){
87
91
}
88
92
return true ;
89
93
}
90
-
94
+
91
95
public static String date2StandardString (Date time ){
92
- return date2FormatString (time ,"yyyy-MM-dd'T'HH:mm:ss" );
96
+ return date2FormatString (time ,"yyyy-MM-dd'T'HH:mm:ss.SSS " );
93
97
}
94
-
98
+
95
99
public static Date standardString2Date (String time ){
96
- return string2Date (time ,"yyyy-MM-dd'T'HH:mm:ss" );
100
+ return string2Date (time ,"yyyy-MM-dd'T'HH:mm:ss.SSS " );
97
101
}
98
-
102
+
99
103
public static String getYearMonthDay (Date date ){
100
104
Calendar calendar = Calendar .getInstance ();
101
105
calendar .setTime (date );
@@ -106,7 +110,7 @@ public static String getYearMonthDay(Date date){
106
110
String day_str = (day < 10 ?("0" + day ) : "" + day );
107
111
return year + "-" + month_str + "-" + day_str ;
108
112
}
109
-
113
+
110
114
public static Date string2Date (String time , String regex ){
111
115
Date date = null ;
112
116
SimpleDateFormat sdf = new SimpleDateFormat (regex );
@@ -119,7 +123,8 @@ public static Date string2Date(String time, String regex){
119
123
}
120
124
121
125
/**
122
- * time1 < time2 + days
126
+ * time1 < time2 + days.
127
+ *
123
128
* @param time1
124
129
* @param time2
125
130
* @param days
@@ -142,31 +147,32 @@ public static boolean isBefore_day(Date time1, Date time2, int days){
142
147
}
143
148
return result ;
144
149
}
145
-
146
- public static boolean isBefore (Date time1 , Date time2 ){
147
- return isBefore_day ( time1 , time2 , 0 );
150
+
151
+ public static boolean isBefore (Date time1 , Date time2 ) {
152
+ return time1 . before ( time2 );
148
153
}
149
-
154
+
150
155
/**
151
- * time1 < time2 - 7_days
156
+ * time1 < time2 - 7_days.
157
+ *
152
158
* @param time1
153
159
* @param time2
154
160
* @return
155
161
*/
156
162
public static boolean isBeforeOneWeek (Date time1 , Date time2 ){
157
163
return isBefore_day (time1 ,time2 ,-7 );
158
164
}
159
-
165
+
160
166
public static void main (String [] args ) {
161
167
String time = "2012-03-21T11:13:36.089+07:00" ;
162
168
Date date = DateUtil .string2Date (time ,"yyyy-MM-dd'T'HH:mm:ss.SSS" );
163
- System .out .println (date .toString ());
169
+ System .out .println (date .toString ());
164
170
System .out .println (DateUtil .date2StandardString (new Date ()));
165
171
// DateTimeFormatter parser = ISODateTimeFormat.dateTime();
166
172
// DateTime dt = parser.parseDateTime(time);
167
- //
173
+ //
168
174
// DateTimeFormatter formatter = DateTimeFormat.mediumDateTime();
169
175
// System.out.println(dt.toString());
170
176
}
171
-
177
+
172
178
}
0 commit comments