Java
Chilkat2 Java
JSON Date Parsing
See more JSON Examples
Demonstrates how to parse date/time strings from JSON.Note: This example uses the DtOf and DateOf methods introduced in Chilkat v9.5.0.73
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.DtObj;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
JsonObject json = new JsonObject();
json.setEmitCompact(false);
// First, let's create JSON containing some date/time strings.
json.updateString("test.timestamp", "2018-01-30T20:35:00Z");
json.updateString("test.rfc822", "Tue, 24 Apr 2018 08:47:03 -0500");
json.updateString("test.dateStrings[0]", "2018-01-30T20:35:00Z");
json.updateString("test.dateStrings[1]", "Tue, 24 Apr 2018 08:47:03 -0500");
json.updateNumber("test.StartLoggingTime", "1446834998.695");
json.updateNumber("test.Expiration", "1442877512.0");
json.updateInt("test.StartTime", 1518867432);
System.out.println(json.emit());
// We've built the following JSON:
// {
// "test": {
// "timestamp": "2018-01-30T20:35:00Z",
// "rfc822": "Tue, 24 Apr 2018 08:47:03 -0500",
// "dateStrings": [
// "2018-01-30T20:35:00Z",
// "Tue, 24 Apr 2018 08:47:03 -0500"
// ],
// "StartLoggingTime": 1446834998.695,
// "Expiration": 1442877512.0,
// "StartTime": 1518867432
// }
// }
// Use the DateOf and DtOf methods to load Chilkat date/time objects with the date/time values.
// The CkDateTime object is primarily for loading a date/time from numerous formats, and then getting
// the date/time in various formats. Thus, it's primarly for date/time format conversion.
// The DtObj object holds a date/time where the individual components (day, month, year, hour, minutes, etc.) are
// immediately accessible as integers.
DateTime dateTime = new DateTime();
DtObj dt = new DtObj();
boolean getAsLocal = false;
// Load the date/time at test.timestamp into the dateTime object.
json.dateOf("test.timestamp", dateTime);
System.out.println(dateTime.getAsTimestamp(getAsLocal));
System.out.println(dateTime.getAsUnixTime(false));
System.out.println(dateTime.getAsRfc822(getAsLocal));
json.dateOf("test.rfc822", dateTime);
System.out.println(dateTime.getAsTimestamp(getAsLocal));
json.setI(0);
json.dateOf("test.dateStrings[i]", dateTime);
System.out.println(dateTime.getAsTimestamp(getAsLocal));
json.setI(1);
json.dateOf("test.dateStrings[i]", dateTime);
System.out.println(dateTime.getAsTimestamp(getAsLocal));
json.dateOf("test.StartLoggingTime", dateTime);
System.out.println(dateTime.getAsTimestamp(getAsLocal));
json.dateOf("test.Expiration", dateTime);
System.out.println(dateTime.getAsTimestamp(getAsLocal));
json.dateOf("test.StartTime", dateTime);
System.out.println(dateTime.getAsTimestamp(getAsLocal));
// Output so far:
// 2018-01-30T20:35:00Z
// 1517344500
// Tue, 30 Jan 2018 20:35:00 GMT
// 2018-04-24T13:47:03Z
// 2018-01-30T20:35:00Z
// 2018-04-24T13:47:03Z
// 2015-11-07T00:36:38Z
// 2015-09-22T04:18:32Z
// 2018-02-17T17:37:12Z
// Now load the date/time strings into the dt object:
json.dtOf("test.timestamp", getAsLocal, dt);
System.out.println("month=" + dt.getMonth() + ", day=" + dt.getDay() + ", year=" + dt.getYear() + ", hour=" + dt.getHour() + ", minute=" + dt.getMinute());
json.dtOf("test.rfc822", getAsLocal, dt);
System.out.println("month=" + dt.getMonth() + ", day=" + dt.getDay() + ", year=" + dt.getYear() + ", hour=" + dt.getHour() + ", minute=" + dt.getMinute());
json.setI(0);
json.dtOf("test.dateStrings[i]", getAsLocal, dt);
System.out.println("month=" + dt.getMonth() + ", day=" + dt.getDay() + ", year=" + dt.getYear() + ", hour=" + dt.getHour() + ", minute=" + dt.getMinute());
json.setI(1);
json.dtOf("test.dateStrings[i]", getAsLocal, dt);
System.out.println("month=" + dt.getMonth() + ", day=" + dt.getDay() + ", year=" + dt.getYear() + ", hour=" + dt.getHour() + ", minute=" + dt.getMinute());
json.dtOf("test.StartLoggingTime", getAsLocal, dt);
System.out.println("month=" + dt.getMonth() + ", day=" + dt.getDay() + ", year=" + dt.getYear() + ", hour=" + dt.getHour() + ", minute=" + dt.getMinute());
json.dtOf("test.Expiration", getAsLocal, dt);
System.out.println("month=" + dt.getMonth() + ", day=" + dt.getDay() + ", year=" + dt.getYear() + ", hour=" + dt.getHour() + ", minute=" + dt.getMinute());
json.dtOf("test.StartTime", getAsLocal, dt);
System.out.println("month=" + dt.getMonth() + ", day=" + dt.getDay() + ", year=" + dt.getYear() + ", hour=" + dt.getHour() + ", minute=" + dt.getMinute());
// Output:
// month=1, day=30, year=2018, hour=20, minute=35
// month=4, day=24, year=2018, hour=13, minute=47
// month=1, day=30, year=2018, hour=20, minute=35
// month=4, day=24, year=2018, hour=13, minute=47
// month=11, day=6, year=2015, hour=18, minute=36
// month=9, day=21, year=2015, hour=23, minute=18
// month=2, day=17, year=2018, hour=11, minute=37
}
}