为什么字符串比较结果不在Java中打印0、1或-1?
我必须比较一个日期,并检查它是否在一个范围内。我输入了两个日期,一个是开始日期,另一个是完成日期。比较日期不应在开始日期之前或完成日期之后 我把它们都当作弦,把它们都分开 当我比较日期和开始日期时,它是确定的。但与完成日期的月份相比,它显示的结果是为什么字符串比较结果不在Java中打印0、1或-1?,java,date,Java,Date,我必须比较一个日期,并检查它是否在一个范围内。我输入了两个日期,一个是开始日期,另一个是完成日期。比较日期不应在开始日期之前或完成日期之后 我把它们都当作弦,把它们都分开 当我比较日期和开始日期时,它是确定的。但与完成日期的月份相比,它显示的结果是6。它应该显示1、0或-1的结果,因为我正在比较字符串 下面是来源,我已经在我遇到问题的那一行上面发表了评论。并给出了一个输入输出示例。我哪里出错了 System.out.println("................"); //store th
6
。它应该显示1、0或-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
阅读以下内容的javadoc:
返回一个负整数、零或正整数,因为此对象小于、等于或大于指定对象
它没有指定负数或正数的实际值,因此不能期望它是-1
或1
使用compareTo()
时,应始终将结果检查为<0
、=0
、>0
或!=0,具体取决于您的需要。
基本上date1.与(date2)相比阅读以下内容的javadoc:
返回一个负整数、零或正整数,因为此对象小于、等于或大于指定对象
它没有指定负数或正数的实际值,因此不能期望它是-1
或1
使用compareTo()
时,应始终将结果检查为<0
、=0
、>0
或!=0,具体取决于您的需要。
基本上,date1。与(date2)相比还不太完整
虽然这是事实,但它不能解释为什么会这样
这是词典排序的定义。如果两个字符串不同,则要么它们在某个索引处具有不同的字符,该索引是两个字符串的有效索引,要么它们的长度不同,要么两者都不同。如果它们在一个或多个索引位置具有不同的字符,则k是此类索引的最小值;然后,使用<运算符确定其在位置k处的字符具有较小值的字符串按字典顺序位于另一个字符串之前。在本例中,compareTo返回两个字符串中位置k处两个字符值的差值,即:
this.charAt(k)-另一个string.charAt(k)
换句话说,返回的值是不匹配的前两个(同一索引)字符的词典编纂差异。这就是您可能无法收到-1
或1
的原因
javadoc继续:
如果索引位置不存在差异,则按字典顺序,较短字符串优先于较长字符串。在本例中,compareTo返回字符串长度的差值,即值:
this.length()-另一个String.length()
这也是返回不同数字的另一个原因
这就是为什么必须使用不等式进行比较,以确定哪个字符串在词典上大于或小于另一个字符串。不是很完整
虽然这是事实,但它不能解释为什么会这样
这是词典排序的定义。如果两个字符串不同,则要么它们在某个索引处具有不同的字符,该索引是两个字符串的有效索引,要么它们的长度不同,要么两者都不同。如果它们在一个或多个索引位置具有不同的字符,则k是此类索引的最小值;然后,使用<运算符确定其在位置k处的字符具有较小值的字符串按字典顺序位于另一个字符串之前。在本例中,compareTo返回两个字符串中位置k处两个字符值的差值,即:
this.charAt(k)-另一个string.charAt(k)
换句话说,返回的值是不匹配的前两个(同一索引)字符的词典编纂差异。这就是您可能无法收到-1
或1
的原因
javadoc继续:
如果索引位置不存在差异,则按字典顺序,较短字符串优先于较长字符串。在本例中,compareTo返回字符串长度的差值,即值:
this.length()-另一个String.length()
这也是返回不同数字的另一个原因
这就是为什么必须使用不等式进行比较,以确定哪个字符串在词典上大于或小于另一个字符串。正如@Andreas所说,为什么应该将结果与0进行比较,而不是与1或-1进行比较。而@Nick Miller给出了更多的理论细节
我同意@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();
}
As@Andreas告诉我们为什么要将结果与0进行比较,而不是与1或-1进行比较。而@Nick Miller给出了更多的理论细节
我同意@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();
}
为什么不将其解析为日期
对象并使用它呢?与将单个字符串部分分解为日期fr相比,它要简单得多,也可靠得多