1

I am newbie to spring working on a spring web app. In the app there's one API which should return the date which I saved earlier using another api. The date is consumed on iOS. It's all working fine but the amount of data returned as part of the date is huge and I am wondering if it's possible to turn it off. Here's the sample of the data:

"availability": {
"offset": {
"totalSeconds": -25200
"id": "-07:00"
"rules": {
"fixedOffset": true
"transitions": [0]
"transitionRules": [0]
}-
}-
"zone": {
"id": "America/Los_Angeles"
"rules": {
"fixedOffset": false
"transitions": [127]
0:  {
"offsetBefore": {
"totalSeconds": -28378
"id": "-07:52:58"
"rules": {
"fixedOffset": true
"transitions": [0]
"transitionRules": [0]
}-
}-
"offsetAfter": {
"totalSeconds": -28800
"id": "-08:00"
"rules": {
"fixedOffset": true
"transitions": [0]
"transitionRules": [0]
}-
}-
"duration": {
"seconds": -422
"negative": true
"zero": false
"units": [2]
0:  "SECONDS"
1:  "NANOS"
-
"nano": 0
}-
"gap": false
"overlap": true
"dateTimeAfter": {
"year": 1883
"month": "NOVEMBER"
"dayOfMonth": 18
"dayOfWeek": "SUNDAY"
"dayOfYear": 322
"monthValue": 11
"hour": 12
"minute": 0
"second": 0
"nano": 0
"chronology": {
"id": "ISO"
"calendarType": "iso8601"
}-
}-
"dateTimeBefore": {
"year": 1883
"month": "NOVEMBER"
"dayOfMonth": 18
"dayOfWeek": "SUNDAY"
"dayOfYear": 322
"monthValue": 11
"hour": 12
"minute": 7
"second": 2
"nano": 0
"chronology": {
"id": "ISO"
"calendarType": "iso8601"
}-
}-
"instant": {
"nano": 0
"epochSecond": -2717640000
}-
}-
1:  {
"offsetBefore": {
"totalSeconds": -28800
"id": "-08:00"
"rules": {
"fixedOffset": true
"transitions": [0]
"transitionRules": [0]
}-
}-

I don't want all this data, because my query is taking long time to return. Am I doing something wrong or missing some flag which tells what to return. The date is stored as ZonedDateTime originally, or shall I store the date in some other format?

puru020
  • 808
  • 7
  • 20

1 Answers1

3

You didn't show the POJO you are serializing. So I bet you are using some Date abstraction like Joda Time's or Java8's LocalDateTime. You may want to change this POJO to contain only simple Long timestamp or just Date, which should be serialized into amount of milliseconds from epoch 1970.

For mentioned conversion take a look at these SO answers:

Converting between java.time.LocalDateTime and java.util.Date

How to get milliseconds from LocalDateTime in Java 8

Community
  • 1
  • 1
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • Thanks, I am also thinking of storing the date in epoch relative to UTC instead of actual date in some format. Thanks for the suggestion. – puru020 Mar 14 '16 at 18:19
  • @puru020 Using count-from-epoch is unintuitive, masks the meaning of the values, and is therefore prone to error. I strongly recommend considering the use of standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formats for serializing date-time values into a string representation. These formats are sensible and intuitive. The ISO 8601 standard is used by default in both the java.time and Joda-Time frameworks for parsing/generating textual representations of date-time values. Also, big tip: Use UTC. For example: `2016-03-13T04:50:31Z` where the `Z` is `Zulu` meaning UTC. – Basil Bourque Mar 14 '16 at 22:54
  • Thanks Basil. I was actually using this format earlier "yyyy-MM-dd HH:mm:ss Z" with ZonedDateTime in my rest api. The data was being saved correctly in mongodb like "ISODate("2016-03-14T01:45:28.731Z")". But when querying for value I was getting the response above which is huge data and was trying to avoid it. Do you know if there's way to tell spring to return just the date? – puru020 Mar 14 '16 at 23:37