Skip to content

Commit 8669ad9

Browse files
committed
add US DateTime suport in default parssing
1 parent 9d45504 commit 8669ad9

File tree

6 files changed

+30
-26
lines changed

6 files changed

+30
-26
lines changed

.gitignore

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
2-
/json-smart/target
3-
/json-smart/.settings
4-
/json-smart/.classpath
5-
/json-smart/.project
6-
/parent/.project
7-
/parent/.settings
8-
/json-smart/src/test/java/asm
9-
/asm/.classpath
10-
/asm/.project
11-
/asm/.settings
12-
/asm/targetjson-smart-backport/.classpath
13-
json-smart-backport/.project
14-
json-smart-backport/.settings
15-
asm/target/
16-
json-smart-backport/target/
17-
json-smart/target/
18-
**/.project
1+
**/.classpath
2+
**/.idea/
3+
**/.project
4+
**/.settings/
5+
**/*.iml
196
**/bin
20-
*.classpath
21-
*.project
22-
*.prefs
7+
**/target

accessors-smart/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>net.minidev</groupId>
55
<artifactId>accessors-smart</artifactId>
6-
<version>1.1</version>
6+
<version>1.2</version>
77
<name>ASM based accessors helper used by json-smart</name>
88
<description>Java reflect give poor performance on getter setter an constructor calls, accessors-smart use ASM to speed up those calls.
99
</description>

accessors-smart/src/main/java/net/minidev/asm/ConvertDate.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.HashSet;
99
import java.util.Locale;
1010
import java.util.StringTokenizer;
11+
import java.util.TimeZone;
1112
import java.util.TreeMap;
1213

1314
public class ConvertDate {
@@ -40,6 +41,7 @@ private static Integer parseMonth(String s1) {
4041
static {
4142
voidData.add("CET");
4243
voidData.add("MEZ");
44+
voidData.add("PST");
4345
voidData.add("Uhr");
4446
voidData.add("h");
4547
voidData.add("pm");
@@ -118,6 +120,8 @@ public static Date convertToDate(Object obj) {
118120
return null;
119121
if (obj instanceof Date)
120122
return (Date) obj;
123+
if (obj instanceof Number)
124+
return new Date(((Number)obj).longValue());
121125
if (obj instanceof String) {
122126
StringTokenizer st = new StringTokenizer((String) obj, " -/:,.+");
123127
String s1 = "";
@@ -204,8 +208,12 @@ private static Date getMMDDYYYY(StringTokenizer st, String s1) {
204208
return null;
205209
s1 = st.nextToken();
206210
}
207-
cal.set(Calendar.YEAR, getYear(s1));
208-
211+
if (s1.length() == 4)
212+
cal.set(Calendar.YEAR, getYear(s1));
213+
else if (s1.length() == 2) {
214+
return addHour2(st, cal, s1);
215+
216+
}
209217
// /if (st.hasMoreTokens())
210218
// return null;
211219
// s1 = st.nextToken();
@@ -236,6 +244,10 @@ private static Date addHour(StringTokenizer st, Calendar cal, String s1) {
236244
return cal.getTime();
237245
s1 = st.nextToken();
238246
}
247+
return addHour2(st, cal, s1);
248+
}
249+
250+
private static Date addHour2(StringTokenizer st, Calendar cal, String s1) {
239251
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s1));
240252

241253
if (!st.hasMoreTokens())
@@ -273,13 +285,19 @@ private static Date addHour(StringTokenizer st, Calendar cal, String s1) {
273285
s1 = trySkip(st, s1, cal);
274286
// if (s1.equalsIgnoreCase("pm"))
275287
// cal.add(Calendar.HOUR_OF_DAY, 12);
288+
289+
if (s1.length() == 4 && Character.isDigit(s1.charAt(0)))
290+
cal.set(Calendar.YEAR, getYear(s1));
291+
276292
return cal.getTime();
277293
}
278294

279295
private static String trySkip(StringTokenizer st, String s1, Calendar cal) {
280296
while (voidData.contains(s1)) {
281297
if (s1.equalsIgnoreCase("pm"))
282298
cal.add(Calendar.HOUR_OF_DAY, 12);
299+
if (s1.equalsIgnoreCase("PST"))
300+
cal.setTimeZone(TimeZone.getTimeZone("PST"));
283301
if (!st.hasMoreTokens())
284302
return null;
285303
s1 = st.nextToken();

accessors-smart/src/test/java/net/minidev/asm/TestDateConvert.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public void testDateFR() throws Exception {
1717
tests.add("23 janvier 2012 13:42:12");
1818
tests.add("lundi 23 janvier 2012 13:42:12");
1919
tests.add("2012-01-23 13:42:12");
20+
tests.add("Thu Jan 23 13:42:12 PST 2012");
2021
//
2122
for (String testDate : tests) {
2223
Date parsed = null;

json-smart/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>net.minidev</groupId>
4949
<artifactId>accessors-smart</artifactId>
50-
<version>1.1</version>
50+
<version>1.2</version>
5151
</dependency>
5252
</dependencies>
5353
<reporting>

json-smart/src/test/java/net/minidev/json/test/TestNavi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testNaviWriteArray() {
4444
JSONNavi<JSONAwareEx> nav = JSONNavi.newInstance();
4545
nav.set("type", "bundle").at("data").array().at(0).set("type", "object").set("name", "obj1").up().at(1).set("type", "object").set("name", "obj2").root();
4646
String s2 = nav.toString();
47-
assertEquals(expected, nav.toString());
47+
assertEquals(expected, s2);
4848

4949
nav = JSONNavi.newInstance();
5050
nav.set("type", "bundle").at("data").array().atNext().set("type", "object").set("name", "obj1").up().atNext().set("type", "object").set("name", "obj2").root();

0 commit comments

Comments
 (0)