简体   繁体   English

如何在Java中的两个字符串之间搜索一系列值

[英]How do I search for a range of values in between two Strings in Java

First I know this is bad way to compare dates but I don't understand how to evaluate a strings to include the data between 2 values. 首先我知道这是比较日期的坏方法,但我不明白如何评估字符串以包含2个值之间的数据。 For example in this code how would I list not only transactions that occurred on tdate 1 and 2 but also the transactions that fell between them. 例如,在此代码中,我如何列出tdate 1和2上发生的事务,以及它们之间的事务。

tdate information is set up like 11/07/2013 tdate信息设置如11/07/2013

      System.out.println("Start Date: ");
      tdate = in.next();
      System.out.println("End Date: ");
      tdate2 = in.next();

      transactionSet trr = new transactionSet(); 

      for(int idx = 0; idx < ts.getNumTrans(); idx++){

        t = ts.retrieve(idx);
        if (t.getTdate().equals(tdate) || t.getTdate().equals(tdate2)){
         trr.insert(t);      
        }
      }
SimpleDateFormat format = new SimpleDateFormat("d/M/yyyy");  // If this is the right format
Date first = format.parse(tdate);
Date second = format.parse(tdate2);
Date toCheck = format.parse(someDate);
if (toCheck.after(first) && toCheck.before(second)){
    // ...
}

You might want to do this in a try/catch block, in case the dates that are input are not formatted correctly. 您可能希望在try / catch块中执行此操作,以防输入的日期格式不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM