迭代从一个字符串开始的字符串列表,直到Java中的“另一个”字符串

时间:2017-06-08 04:21:40

标签: java string

我正在尝试迭代Java中的字符串列表。 我的清单包括:

["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"]

我正在阅读用户的日期:

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));

如果我想从日期中提取字符串:2016年12月30日至2017年1月1日,即

"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"

我正在使用以下循环: -

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);
}

我没有结果。

但是当我尝试这个时:

       for(String s : objectNameList){
           if((s.contains(fromYear) && s.contains(fromMonth) && s.contains(fromDay))){
               System.out.println(s);
           }
       }

我只得到:

"DataFileDownload/FD/722066/71493/2016/12/30/untitled-1.aux"

任何人都可以告诉我如何改善循环条件?

提前致谢!

2 个答案:

答案 0 :(得分:2)

在列表中逐个字符串,并使用正则表达式提取日期,如下所示:

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");

将其另存为Date对象:

SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
Date date = fmt.parse(dateStr);

然后,从用户读取输入日期并将其保存为Date个对象。

然后,您可以使用以下方法比较用户日期和列表中的日期:

java.util.Date.before(Date when);
java.util.Date.after(Date when);

比分析所有日期和玩字符串容易得多。

答案 1 :(得分:1)

将日期值从String转换为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)));

然后拆分Sting List并从for block

中的字符串中获取年,月,日
 String[] f = s.split("/");      
 int year1 = Integer.parseInt(f[4]);
 int month1 = Integer.parseInt(f[5]);
 int day1 = Integer.parseInt(f[6]);

然后应用逻辑来匹配日期和日期之间的日期

            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);
                            }
                    }