• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fetch JSONObjects from JSONArray that have date property between the start date and end date?  RSS feed

 
Hasnain Ahmad
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have JSONArray that have 57 JSONObjects, each object has 8 key/value pair properties. In which there is a “publishdate” property. I want to fetch each objects that has “publishdate” within a month. I am using Joda-Time library for date computation.
Here is my code.

Here is my image that shows JSONArray of JSONObjects.

Which data structure is best for above scenario ArrayList or any other?
 
Arun Chidambaram
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would rather prefer to add dates in Arraylist and use Collection.sort(list) to sort the dates and split the list based on variable month.

1.Store all dates in a Arraylist
2.Use Collection.sort(list) to sort the dates by either asc or desc depending on the requirement
3.Split the list by navigating through the entries and check if month retrieved is different to previous entry, if so populate in new Arraylist and repeat for rest of the entries.

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a case for a Stream.Use filter before sorted to reduce the performance load.

I am afraid I have got to go now, so I don't have time to describe the whole process.

Please search for Stream Arrays filter in my posts on this forum and on the Java in General forum, and you will find something similar with fuller explanations.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a different Collector available where you use the groupingBy method, and that will return you a Map.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!