Skip to content

Commit 113375a

Browse files
committed
* Fixed LSM not storing time with millisecond precision.
1 parent eb08cb0 commit 113375a

File tree

1 file changed

+48
-42
lines changed
  • modules/lsm-light/lsm-light.server/src/main/java/org/openiot/lsm/utils

1 file changed

+48
-42
lines changed

modules/lsm-light/lsm-light.server/src/main/java/org/openiot/lsm/utils/DateUtil.java

+48-42
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
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-
*/
212

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+
*/
2222
import java.text.ParseException;
2323
import java.text.SimpleDateFormat;
2424
import java.util.Calendar;
2525
import java.util.Date;
26+
import java.util.logging.Level;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
2630
/**
27-
*
31+
*
2832
* @author Hoan Nguyen Mau Quoc
29-
*
33+
*
3034
*/
3135
public class DateUtil {
32-
36+
3337
/**
3438
* @param time the format should be like:"yyyyMMddHHmmss"
3539
* @return the Date
@@ -47,7 +51,7 @@ public static Date fullFormatDigits2Date(String time){
4751
}
4852
return date;
4953
}
50-
54+
5155
/**
5256
* parse the time to string under the format.
5357
* @param time
@@ -64,19 +68,19 @@ public static String date2FormatString(Date time, String format){
6468
}
6569
return result;
6670
}
67-
71+
6872
public static Date RFC822Section5Format_to_Date(String time){
6973
String format = "EEE, dd MMM yyyy hh:mm a";
7074
Date date = string2Date(time,format);
7175
return date;
7276
}
73-
77+
7478
public static Date RFC822WUnderGroundFormat_to_date(String time){
7579
String format = "EEE, dd MMM yyyy hh:mm:ss";
7680
Date date = string2Date(time,format);
7781
return date;
7882
}
79-
83+
8084
public static boolean isRFC822WUnderGroundFormat(String time){
8185
String regex = "EEE, dd MMM yyyy hh:mm:ss";
8286
SimpleDateFormat sdf = new SimpleDateFormat(regex);
@@ -87,15 +91,15 @@ public static boolean isRFC822WUnderGroundFormat(String time){
8791
}
8892
return true;
8993
}
90-
94+
9195
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");
9397
}
94-
98+
9599
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");
97101
}
98-
102+
99103
public static String getYearMonthDay(Date date){
100104
Calendar calendar = Calendar.getInstance();
101105
calendar.setTime(date);
@@ -106,7 +110,7 @@ public static String getYearMonthDay(Date date){
106110
String day_str = (day < 10 ?("0" + day) : "" + day);
107111
return year + "-" + month_str + "-" + day_str;
108112
}
109-
113+
110114
public static Date string2Date(String time, String regex){
111115
Date date = null;
112116
SimpleDateFormat sdf = new SimpleDateFormat(regex);
@@ -119,7 +123,8 @@ public static Date string2Date(String time, String regex){
119123
}
120124

121125
/**
122-
* time1 < time2 + days
126+
* time1 &lt; time2 + days.
127+
*
123128
* @param time1
124129
* @param time2
125130
* @param days
@@ -142,31 +147,32 @@ public static boolean isBefore_day(Date time1, Date time2, int days){
142147
}
143148
return result;
144149
}
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);
148153
}
149-
154+
150155
/**
151-
* time1 < time2 - 7_days
156+
* time1 &lt; time2 - 7_days.
157+
*
152158
* @param time1
153159
* @param time2
154160
* @return
155161
*/
156162
public static boolean isBeforeOneWeek(Date time1, Date time2){
157163
return isBefore_day(time1,time2,-7);
158164
}
159-
165+
160166
public static void main(String[] args) {
161167
String time = "2012-03-21T11:13:36.089+07:00";
162168
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());
164170
System.out.println(DateUtil.date2StandardString(new Date()));
165171
// DateTimeFormatter parser = ISODateTimeFormat.dateTime();
166172
// DateTime dt = parser.parseDateTime(time);
167-
//
173+
//
168174
// DateTimeFormatter formatter = DateTimeFormat.mediumDateTime();
169175
// System.out.println(dt.toString());
170176
}
171-
177+
172178
}

0 commit comments

Comments
 (0)