2015-07-28 121 views
0

我被困在一個情況數據,其中在for循環我正在約會的時間在這個格式字符串獲取從開始日期和結束日期

16/07/2014 11:06:33 
25/05/2014 06:24:56 
21/05/2014 21:37:03 
21/05/2014 21:18:54 
21/05/2014 21:00:57 
21/05/2014 20:41:12 
21/05/2014 20:24:50 
15/05/2014 09:44:20 
13/05/2014 17:08:27 
05/05/2014 21:48:27 
05/05/2014 12:02:59 

我將其轉換爲SimpleDateFormat的因此,通過使用啓動和結束日期我可以在這些日期範圍之間獲取日期時間。我也有開始日期和結束日期變量。我想知道如何在當前情況下應用開始和結束日期過濾器?

String testStart = "05/05/2014 12:02:59"; 
String testEnd = "15/05/2014 09:44:20"; 
for(int i=0; i<testOutput.getResultBody().getPaymentResult().size(); i++) 
{ 
String oldFormat = "dd/MM/yyyy HH:mm:ss"; 
String newFormat = "dd/MM/yyyy HH:mm:ss"; 
SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat); 
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat); 
System.out.println("-->" + sdf2.format(sdf1.parse(paymentOutput.getResultBody().getPaymentResult().get(i).getMyDate()))); 
} 

我在思考在這種情況下使用的邏輯有困難。

回答

1

試試這個:

String format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 

Date testStart = format.parse("05/05/2014 12:02:59"); 
Date testEnd = format.parse("15/05/2014 09:44:20"); 

for(int i=0; i<testOutput.getResultBody().getPaymentResult().size(); i++) { 
    String dateStr = paymentOutput.getResultBody().getPaymentResult().get(i).getMyDate(); 
    Date date = format.parse(dateStr); 
    if (date.compareTo(testStart) >= 0 && date.compareTo(testEnd) < 0) { 
     System.out.println(dateStr); 
    } 
} 

如果您使用的是Java的最新版本,你也可以考慮使用喬達時間API和流更好的可讀性。

相關問題