Reputation: 79
I'm trying to iterate through a List of Strings in Java. My List consists of:
["DataFileDownload/FD/722066/71493/2016/12/30/untitled-1.aux","DataFileDownload/FD/722066/71493/2016/12/31/untitled-2.aux","DataFileDownload/FD/722066/71493/2017/01/01/untitled-3.aux","DataFileDownload/FD/722066/71493/2016/01/02/untitled-4.aux"]
And I am reading the dates from the user as:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date fromDate = fmt.parse("2016-12-30");
Date toDate = fmt.parse("2017-01-02");
Calendar fromCal = Calendar.getInstance();
fromCal.setTime(fromDate);
String fromYear = String.valueOf(fromCal.get(Calendar.YEAR));
String fromMonth = String.valueOf(fromCal.get(Calendar.MONTH)+1);
String fromDay = String.valueOf(fromCal.get(Calendar.DAY_OF_MONTH));
Calendar toCal = Calendar.getInstance();
toCal.setTime(toDate);
String toYear = String.valueOf(toCal.get(Calendar.YEAR));
String toMonth = String.valueOf(toCal.get(Calendar.MONTH)+1);
String toDay = String.valueOf(toCal.get(Calendar.DAY_OF_MONTH));
If I wanted to extract the Strings from date: 30-dec-2016 to 01-Jan-2017, i.e.
"DataFileDownload/FD/722066/71493/2016/12/30/untitled-1.aux","DataFileDownload/FD/722066/71493/2016/12/31/untitled-2.aux","DataFileDownload/FD/722066/71493/2017/01/01/untitled-3.aux"
I'm using the below loop:-
for(String s : NameList){
if((s.equals(fromYear) && s.equals(fromMonth) && s.equals(fromDay)) == true){
while((s.equals(toYear) && s.equals(toMonth) && s.equals(toDay)) != true){
System.out.println(s);
}
System.out.println(s);
}
I get no result.
But when I try this:
for(String s : objectNameList){
if((s.contains(fromYear) && s.contains(fromMonth) && s.contains(fromDay))){
System.out.println(s);
}
}
I get only:
"DataFileDownload/FD/722066/71493/2016/12/30/untitled-1.aux"
Can anyone please let me know how can I improve my loop condition?
Thanks in advance!
Upvotes: 0
Views: 89
Reputation: 118
Convert the date values from String to Integer.
int fromYear=Integer.parseInt(String.valueOf(fromCal.get(Calendar.YEAR)));
int fromMonth=Integer.parseInt(String.valueOf(fromCal.get(Calendar.MONTH)+1));
int fromDay = Integer.parseInt(String.valueOf(fromCal.get(Calendar.DAY_OF_MONTH)));
int toYear = Integer.parseInt(String.valueOf(toCal.get(Calendar.YEAR)));
int toMonth = Integer.parseInt(String.valueOf(toCal.get(Calendar.MONTH)+1));
int toDay = Integer.parseInt(String.valueOf(toCal.get(Calendar.DAY_OF_MONTH)));
Then split the Sting List and get year, month, day from the string inside the for block
String[] f = s.split("/");
int year1 = Integer.parseInt(f[4]);
int month1 = Integer.parseInt(f[5]);
int day1 = Integer.parseInt(f[6]);
Then apply the logic to match date in between from date and to date
if((fromYear < year1) && (toYear > year1))
{
System.out.println(s);
}
else if((fromYear == year1))
{
if((fromMonth <= month1) && (fromDay >= day1))
{
System.out.println(s);
}
}
else if((toYear == year1))
{
if((toMonth >= month1) || (toDay == day1))
{
System.out.println(s);
}
}
Upvotes: 1
Reputation: 2616
Go string by string in your list and extract the date using regular expression, something like this:
private static String getDate(String str) {
Pattern regex = Pattern.compile("\\.*(?<date>([0-9]{4}\\/[0-9]{2}\\/[0-9]{2}))");
Matcher matcher = regex.matcher(str);
boolean success = matcher.find();
return (success ? matcher.group("date") : null);
}
// Example:
String dateStr = getDate("DataFileDownload/FD/722066/71493/2016/12/30/untitled-1.aux");
Save it as a Date
object:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
Date date = fmt.parse(dateStr);
Then, read the input dates from the user and save them as Date
objects.
Then, you can compare the user dates and the dates from the list using:
java.util.Date.before(Date when);
java.util.Date.after(Date when);
Much easier than parsing all the dates and playing with strings.
Upvotes: 2