1

I am using GsonBuilder to parse the string to SleepSessionMessage class type but getting an unparseable date exception on "changeTimeStamp" field. I have checked my JSON string and it is perfectly fine and checked the Epoch time too, not sure what is wrong:

public class SleepSessionMessage extends SleepSession implements SiqMessage {

private static transient Gson gson = new GsonBuilder().create();

public static SleepSessionMessage fromJson(String jsonString) {
SleepSessionMessage sleepSessionMessage = null;
try {
  sleepSessionMessage = new .fromJson(jsonString, SleepSessionMessage.class);
} catch (Exception e) {
  logger.error("Failed in fromJson@SleepSessionMessage", e);
}

   return sleepSessionMessage;
}

and below is the exception thrown:

java.text.ParseException: Unparseable date: "1538773089927"

Here is the JSON string I am using:

{
  "type": null,
  "changeTimeStamp": 1538773089927,
  "bamUserId": 1538773089927,
  "calcVersion": null,
  "endDate": 1538773089927,
  "duration": null,
  "restfulTime": null,
  "sleepQuotient": null,
  "minHeartRate": null,
  "maxHeartRate": null,
  "avgHeartRate": null,
  "minRespirationRate": null,
  "maxRespirationRate": null,
  "avgRespirationRate": null,
  "minMotion": null,
  "maxMotion": null,
  "avgMotion": null,
  "percentMotion": null,
  "sleepStartDate": null,
  "sleepEndDate": null,
  "sleepDuration": null,
  "fallAsleepPeriod": null,
  "wakeUpPeriod": null,
  "sessionStatus": null,
  "motionNoiseThreshold": null,
  "noMotionPressure": null,
  "minNormalizedMotion": null,
  "maxNormalizedMotion": null,
  "avgNormalizedMotion": null,
  "sleepDebt": null,
  "inAndOut": null,
  "percentSnoring": null,
  "percentGoodHR": null,
  "fiveMinWithNoHRCount": null,
  "fifteenMinWithNoHRCount": null,
  "sleepNumber": null,
  "timeToFallSleep": null,
  "startDate": 1538773089927,
  "segStartDate": null,
  "fallAsleepDate": null,
  "hiddenChangeDate": null,
  "ossExists": null,
  "ossMinDuration": null,
  "ossMaxDuration": null,
  "ossAvgDuration": null,
  "ossMinRestfulTime": null,
  "ossMaxRestfulTime": null,
  "ossAvgRestfulTime": null,
  "ossMinSleepQuotient": null,
  "ossMaxSleepQuotient": null,
  "ossAvgSleepQuotient": null,
  "ossMinHeartRate": null,
  "ossMaxHeartRate": null,
  "ossAvgHeartRate": null,
  "ossMinRespirationRate": null,
  "ossMaxRespirationRate": null,
  "ossAvgRespirationRate": null,
  "ossMinMotion": null,
  "ossMaxMotion": null,
  "ossAvgMotion": null,
  "ossMinPercentMotion": null,
  "ossMaxPercentMotion": null,
  "ossAvgPercentMotion": null,
  "ossMinSleepDuration": null,
  "ossMaxSleepDuration": null,
  "ossAvgSleepDuration": null,
  "ossMinFallAsleepPeriod": null,
  "ossMaxFallAsleepPeriod": null,
  "ossAvgFallAsleepPeriod": null,
  "ossMinWakeupPeriod": null,
  "ossMaxWakeupPeriod": null,
  "ossAvgWakeupPeriod": null,
  "ossMinNormalizedMotion": null,
  "ossMaxNormalizedMotion": null,
  "ossAvgNormalizedMotion": null,
  "ossSleepDebtLastWeek": null,
  "ossSleepDebtLastMonth": null,
  "ossMinSleepDebt": null,
  "ossMaxSleepDebt": null,
  "ossAvgSleepDebt": null,
  "ossAvgStartTime": null,
  "ossAvgEndTime": null,
  "ossMinInAndOut": null,
  "ossMaxInAndOut": null,
  "ossAvgInAndOut": null,
  "ossMinPercentSnoring": null,
  "ossMaxPercentSnoring": null,
  "ossAvgPercentSnoring": null,
  "isSleepSegment": null,
  "sleepSegments": null,
  "messageType": "START_MESSAGE",
  "sessionUUID": null,
  "topic": "topic.rollup.sleepsession",
  "sentTime": 0,
  "entriesCount": 0,
  "key": null,
  "sleepSession": true,
  "hidden": null
}
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
db07
  • 107
  • 1
  • 6
  • 1
    That's not a _date_ that needs to be parsed, it is an epoch timestamp. If you have it as a String, just turn it into a Long (parseLong) and then create a Date or a LocalDate from that long value. See [this question](https://stackoverflow.com/q/44883432/17300) for timestamp to LocalDateTime – Stephen P Oct 09 '18 at 19:08
  • You are passing a `long` to a method expecting a `String`. – The Head Rush Oct 09 '18 at 19:08
  • 1
    Possible duplicate of [Gson dateformat to parse/output unix-timestamps](https://stackoverflow.com/questions/41348055/gson-dateformat-to-parse-output-unix-timestamps) – pirho Oct 09 '18 at 19:08

0 Answers0