Skip to content

Commit fd3e649

Browse files
committed
Added date functionality.
1 parent 8114b97 commit fd3e649

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎JSONObject.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
3131
import java.lang.reflect.Method;
3232
import java.lang.reflect.Modifier;
3333
import java.util.Collection;
34+
import java.util.Date;
3435
import java.util.Enumeration;
3536
import java.util.HashMap;
3637
import java.util.Iterator;
@@ -581,6 +582,24 @@ public JSONObject getJSONObject(String key) throws JSONException {
581582
+ "] is not a JSONObject.");
582583
}
583584

585+
/**
586+
* Get the Date value associated with a key.
587+
*
588+
* @param key
589+
* A key string.
590+
* @return A JSONObject which is the value.
591+
* @throws JSONException
592+
* if the key is not found or if the value is not a JSONObject.
593+
*/
594+
public Date getDate(String key) throws JSONException {
595+
Object object = this.get(key);
596+
if (object instanceof Date) {
597+
return (Date) object;
598+
}
599+
throw new JSONException("JSONObject[" + quote(key)
600+
+ "] is not a Date.");
601+
}
602+
584603
/**
585604
* Get the long value associated with a key.
586605
*

‎JSONTokener.java‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.InputStreamReader;
77
import java.io.Reader;
88
import java.io.StringReader;
9+
import java.util.GregorianCalendar;
910

1011
/*
1112
Copyright (c) 2002 JSON.org
@@ -358,7 +359,7 @@ public Object nextValue() throws JSONException {
358359
switch (c) {
359360
case '"':
360361
case '\'':
361-
return this.nextString(c);
362+
return this.checkDates(this.nextString(c));
362363
case '{':
363364
this.back();
364365
return new JSONObject(this);
@@ -390,6 +391,34 @@ public Object nextValue() throws JSONException {
390391
return JSONObject.stringToValue(string);
391392
}
392393

394+
/**
395+
* Checks a string for dates.
396+
*
397+
* @param s string to check
398+
* @return passed through string or a Date object
399+
*/
400+
private Object checkDates(String s) {
401+
if (s.length() == 10 || s.length() == 20) {
402+
if (s.charAt(4) == '-' && s.charAt(7) == '-') {
403+
int year = Integer.valueOf(s.substring(0, 4));
404+
int month = Integer.valueOf(s.substring(5, 7));
405+
int day = Integer.valueOf(s.substring(8, 10));
406+
407+
if (s.length() == 20 && s.charAt(10) == 'T' && s.charAt(13) == ':' && s.charAt(16) == ':' && s.charAt(19) == 'Z') {
408+
int hour = Integer.valueOf(s.substring(11, 13));
409+
int mins = Integer.valueOf(s.substring(14, 16));
410+
int secs = Integer.valueOf(s.substring(17, 19));
411+
412+
return new GregorianCalendar(year, month, day, hour, mins, secs).getTime();
413+
}
414+
415+
return new GregorianCalendar(year, month, day).getTime();
416+
}
417+
}
418+
419+
return s;
420+
}
421+
393422

394423
/**
395424
* Skip characters until the next character is the requested character.

0 commit comments

Comments
 (0)