2016-01-06 64 views
0

我必须比较一个日期并检查它是否在一个范围内。我有两个日期作为输入,例如一个是开始日期,另一个是结束日期。比较日期不应该在开始日期之前或结束日期之后。为什么字符串比较结果在Java中不打印0,1或-1?

我把它们全部作为字符串并将它们全部分开。

当我将日期与开始日期进行比较时,就可以了。但是,与完成日期的月份进行比较时,显示的结果为6。它应该显示1, 0, or -1的结果,因为我在比较字符串。

下面是源代码,我已经在我遇到的问题的上面发表了评论。样本输入和输出也被给出。我在哪里犯了一个错误?

System.out.println("................"); 

//store the value of start date limit. 
String[] startDate = start_jTextField1.getText().split("-"); 

//store the value of start date limit. 
String[] finishDate = finish_jTextField2.getText().split("-"); 

//store the date that have to check. 
String[] check_this_date = Check_this_Date_jTextField1.getText().split("-"); 

System.out.println("start:"); 
for(String start : startDate) 
{ 
    System.out.println(start); 
} 

System.out.println("Finish:"); 
for(String finish : finishDate) 
{ 
    System.out.println(finish); 
} 

System.out.println("Check:"); 
for(String checkDate : check_this_date) 
{ 
    System.out.println(checkDate); 
} 


boolean start_range_check = false; 
boolean finish_range_check = false; 

//My question is about the next line and why it is printing 6. 
//It should print 0, 1, or -1. 
System.out.println("Before Finish check block(month):\n"+ check_this_date[1].compareTo(finishDate[1])); 

System.out.println("Before Finish check block(day compare):\n"+ check_this_date[0].compareTo(finishDate[0]));//here date will be compared with finish date's date. 

if(check_this_date[2].compareTo(startDate[2]) == 1 || check_this_date[2].compareTo(startDate[2]) == 0)//if year greater or equal to from start range year. 
{ 
    System.out.println("starZone:"+start_range_check);//print the current start_range_check value to ensure that above condition has satisfied. 
    if(check_this_date[1].compareTo(startDate[1]) == 1 || check_this_date[1].compareTo(startDate[1]) == 0)//if month greater or equal to from start range month. 
    { 
     System.out.println("starZone:"+start_range_check);//print the current start_range_check value to ensure that above condition has satisfied. 
     if(check_this_date[0].compareTo(startDate[0]) == 1 || check_this_date[0].compareTo(startDate[0]) == 0)//if day greater or equal to from start range day. 
     { 
      start_range_check = true; 
      System.out.println("starZone:"+start_range_check);//print the current start_range_check value to ensure that above condition has satisfied and start_range_check changed. 
     } 
    } 
} 

if(check_this_date[2].compareTo(finishDate[2]) == -1 || check_this_date[2].compareTo(finishDate[2]) == 0) 
{ 
    System.out.println("finishZone result(year):\n"+check_this_date[2].compareTo(finishDate[2]));//print the comparision result. 

    System.out.println("finishZone(before month checking):\n"+check_this_date[1].compareTo(finishDate[1]));//print the comparision result.my question why the result is 6 here. 

    if(check_this_date[1].compareTo(finishDate[1]) == -1 || check_this_date[1].compareTo(finishDate[1]) == 0) 
    { 
     System.out.println("finishZone"+start_range_check); 
     if(check_this_date[0].compareTo(finishDate[0]) == -1 || check_this_date[0].compareTo(finishDate[0]) == 0) 
     { 
      finish_range_check = true; 
      System.out.println("finishZone"+start_range_check);//print the current start_range_check value to ensure that above condition has satisfied and start_range_check changed. 
     } 
    } 
} 

if(finish_range_check == true && start_range_check == true) 
{ 
    result_jLabel2.setText("Within Range."); 
} 
else 
{ 
    result_jLabel2.setText("Not in Range"); 
} 


Sample input and output: 
Date Format: dd-MM-yyyy 
................ 
start: 
28 
6 
2015 
Finish: 
28 
12 
2015 
Check: 
28 
7 
2015 
Before Finish check block(month comparing): 
6 
Before Finish check block(day comparing): 
0 
starZone:false 
starZone:false 
starZone:true 
finishZone result(year): 
0 
finishZone(before month checking): 
6 
+0

为什么不能解析成一个'Date'对象,并与工作,而不是?这比将单个字符串部分分解为日期片段要复杂得多并且远为可靠。 – Makoto

+5

compareTo不返回-1或0或1,请参阅apidoc:“将此对象与指定的对象进行比较,返回负整数,零或正整数,因为此对象小于,等于或大于指定的对象“。 –

+0

你真的需要将日期转换为字符串吗?使用Date API中的'after'和'before'方法。 –

回答

1

正如@Andreas已经告诉你,为什么你应该比较结果0,而不是1或-1。 @尼克米勒给出了更多的理论细节。

我同意@Makoto和@Dariusz Sendkowski在您尝试的方式中它很复杂。

以下是您的问题的示例解决方案。更多的细节和例子在下面的链接中。还有其他一些方法可以做到这一点。

SimpleDateFormat setDateFormat = new SimpleDateFormat ("dd-MM-yyyy"); 

     try 
      { 
      Date startDate = setDateFormat.parse(start_jTextField1.getText() ); 
      Date finishDate = setDateFormat.parse(finish_jTextField2.getText()); 
      Date check_this_date = setDateFormat.parse(Check_this_Date_jTextField1.getText()); 

       System.out.println("Start Date:"+ setDateFormat.format(startDate)); 
       System.out.println("Finish Date:"+ setDateFormat.format(finishDate)); 

      if(check_this_date.before(finishDate) && check_this_date.after(startDate)) 
       { 
        result_jLabel2.setText("Within Range."); 
       } 
      else if(check_this_date.equals(finishDate) || check_this_date.equals(startDate)) 
       { 
       result_jLabel2.setText("Within Range."); 
       } 
      else 
       { 
       result_jLabel2.setText("Not in rannge."); 
       } 
      } 
     catch(Exception ex) 
      { 
      ex.printStackTrace(); 
      } 

More examples are here...

4

查看compareTo()的Javadoc:

返回负整数,或根据此对象正整数比小于,等于或大于指定的对象。

它不指定负或正数的实际价值,所以你不能指望它是-11

当使用compareTo()您应经常检查作为< 0<= 0== 0>= 0> 0,或!= 0,根据您的需要造成的。
基本上date1.compareTo(date2) <= 0是爪哇语为date1 <= date2

1

Andreas's answer不完整。

虽然这是事实,但并不能解释为什么如此。

The derivation still comes from the javadocs

这是词典排序的定义。如果两个字符串不同,那么它们在某个索引处具有不同的字符,这是两个字符串的有效索引,或者它们的长度不同,或者两者都有。如果它们在一个或多个索引位置具有不同的字符,则令k为最小的这样的索引;那么字符串在位置k处具有较小值的字符串(通过使用<运算符确定)按字典顺序在另一字符串之前。在这种情况下,的compareTo返回两个字符值的位置k处两个字符串的差 - 即,值:

this.charAt(k)-anotherString.charAt(k)

换句话说,返回的值是字典序差前两个(相同索引)的字符不匹配。这是你不能收到-11的原因。

的Javadoc继续:

如果在它们没有不同的索引位置,则较短的串较长的字符串按字典顺序之前。在这种情况下,的compareTo返回字符串的长度之差 - 也就是说,值:

this.length()-anotherString.length()

这是为不同的号码返回另一个原因。

这就是为什么你必须使用不等式比较以确定哪个字符串按字典顺序大于或小于另一个字符串。

相关问题