gpt4 book ai didi

java - 为什么 Java 中字符串比较结果不打印 0,1 或 -1?

转载 作者:行者123 更新时间:2023-12-01 07:52:42 24 4
gpt4 key购买 nike

我必须比较一个日期并检查它是否在一个范围内。我输入了两个日期,一个是开始日期,另一个是结束日期。比较日期不应早于开始日期或晚于结束日期。

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

当我将日期与开始日期进行比较时,这是可以的。但与完成日期月份相比,它显示的结果是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

最佳答案

阅读 compareTo() 的javadoc :

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

它没有指定负数或正数的实际值,因此您不能期望它是 -11 .

当使用compareTo()时您应该始终检查结果为 < 0 , <= 0 , == 0 , >= 0 , > 0 ,或!= 0 ,取决于您的需要。
基本上date1.compareTo(date2) <= 0 Java 语言中的 date1 <= date2 .

关于java - 为什么 Java 中字符串比较结果不打印 0,1 或 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34639721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com