• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 76425
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76425
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a different Collector available where you use the groupingBy method, and that will return you a Map.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic