how to return a list of objects from arraylist when passed two dates in java? [duplicate]
how to return a list of objects from arraylist when passed two dates in java? [duplicate]
This question already has an answer here:
Am using following code to return photo objects from arraylist between start and end date (dates are in YYYY-MM-DD string format). if no such objects, it should return empty list.
How can i modify to get the result? There are two parts of code. Here is the first part:
public class Photo {
private String title;
private string date;
public String getTitle()
return title;
public String getDate()
return date;
Here is the second part:
import java.util.ArrayList;
public class Album {
private String albumtitle;
private ArrayList<Photo> photos;
public ArrayList<Photo> datedPhotos(String date1, String date2)
String startdate = date1;
String enddate = date2;
ArrayList<Photo> photolist = new ArrayList<>();
for (Photo pho : photos)
if (pho.getDate().compareTo(enddate)< 0 && pho.getDate().compareTo(startdate)>0)
photolist.add(pho);
return photolist;
`
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You are comparing startdate and enddate while you wanted to filter by (startData < photoDate) && (photoDate < endDate)
– msayag
Sep 15 '18 at 17:22
What is the phpto’s date is the same as the start date? The same as the end date? May start and end date be the same date if I want photos from that date only?
– Ole V.V.
Sep 15 '18 at 18:57
@OleV.V. - Sorry for the confusion. i want to input two dates, start date and end date and the output are the objects between these two dates. The start date and end date can be same and it will return that day's object only i guess
– Jim Ye
Sep 16 '18 at 2:22
@JBNizet - hi JB, in instruction it shows the dates are in ISO format (YYYY-MM-DD) so the simple String comparisons can be used. So one photo has date and title, the aim is to get the date within range and return title. i know how to compare date but stuck to return the title of photos.
– Jim Ye
Sep 16 '18 at 2:40
3 Answers
3
You need to convert your String into dates, so that the dates can be compared. You can use the SimpleDateFormat to achieve the same. After conversion you need to check whether the date of each photo is after the startDate and before the endDate.
String
SimpleDateFormat
startDate
endDate
public ArrayList<Photo> datedPhotos(String date1, String date2)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
ArrayList<Photo> photolist = new ArrayList<>();
Date startDate, endDate;
try
startDate = sdf.parse(date1);
endDate = sdf.parse(date2);
List<Photo> photos = new ArrayList<>();
for (Photo pho : photos)
if (startDate.before(pho.getDate()) && endDate.after(pho.getDate()))
photolist.add(pho);
catch (ParseException e)
// Handle Exception accordingly
return photolist;
Please don’t teach the young ones to use the long outdated and notoriously troublesome
SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter.– Ole V.V.
Sep 15 '18 at 18:58
SimpleDateFormat
java.time
DateTimeFormatter
@Nicholas k - is there any way to avoid "throws ParseExcept" to solve the issue? Thanks.
– Jim Ye
Sep 16 '18 at 2:43
@JimYe : I've edited my code to handle the exception within the
try-catch.– Nicholas K
Sep 16 '18 at 5:59
try-catch
@NicholasK - I have tried your code. Do I need to import any method to achieve it? As the date has been input as "YYYY-MM-DD" format, so no need to set it to simple date format again. Do you have a solution for it?
– Jim Ye
Sep 16 '18 at 10:03
@S.Overflow If you insist on using the outdated and poorly designed
Date class, I find startDate.before(pho.getDate()) clearer to read than startDate.compareTo(pho.getDate()) < 0. I see no use of the (correct) fact that Date implements Comparable here.– Ole V.V.
Sep 17 '18 at 9:40
Date
startDate.before(pho.getDate())
startDate.compareTo(pho.getDate()) < 0
Date
Comparable
remove return photolist; in if statement because you return arraylist with 1 element that is first found
return photolist;
arraylist
also you are comparing startDate and endDate that method got. I think you are trying to do something like this for with you need getter
startDate
endDate
getter
if(startdate.compareTo(pho.getStartDate())< 0)
I imagine your photo object has a property called 'date'. So I think you would want something like this to add photos to your ArrayList
import java.util.ArrayList;
public class Album {
private String albumtitle;
private ArrayList<Photo> photos;
public ArrayList<Photo> datedPhotos(String date1, String date2, ArrayList(Photo) photos)
String startdate = date1;
String enddate = date2;
//edit - you actually do not need to cycle through the arraylist.....
if (photos.date.compareTo(enddate)< 0 && photos.date.compareTo(startdate) > 0)
photolist.add(pho);
return photolist;
`
And then some other place in your code you would have:
Alblum ab = new Album();
ArrayList<photo> myPhotos;
Photo newPhoto = new Photo();
newPhoto.image = "mypicture.jpg";
newPhoto.date = new Date('........');
startdate = new Date('.........');
enddate = new Date('.........);
myPhotos = ab.datedPhotos(startdate, enddate, newPhoto);
Now you have an ArrayList of type photo with one photo in it provided the date fell within the defined parameters.
hi, the aim is to input two dates, start date and end date and return all the title objects between these two dates. Could you edit your code a bit? Thanks
– Jim Ye
Sep 16 '18 at 2:57
Hey Jim - So what are you trying to do? Get the photos between those two dates or add a photo if it exists between those two dates. Also where is the ArrayList of all the photos stored? If it is in the class Album then I think you need two methods right? One to store photos and one to search for photos that exist within certain dates. I think you have everything already have everything here to solve your problem, you just need to move a little slow. Create a private arraylist in your class. Add a method to add Photos into that array list. Add a method to search for photos within a date range.
– S. Overflow
Sep 16 '18 at 3:30
@S. Overflow: The
compareTo(Date anotherDate) takes Date as another parameter, you cannot pass in a String, your code wouldn't even compile.– Nicholas K
Sep 17 '18 at 3:55
compareTo(Date anotherDate)
Date
String
You need to compare the date of the photo with the start date and with the end date. You're only checking that the start date is before the end date. that will always be true (hopefully). Yo should also use the correct type: LocalDate, instead of using Strings to store dates.
– JB Nizet
Sep 15 '18 at 17:22