我必须比较一个日期并检查它是否在一个范围内。我有两个日期作为输入,例如一个是开始日期,另一个是结束日期。比较日期不应该在开始日期之前或结束日期之后。为什么字符串比较结果在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
为什么不能解析成一个'Date'对象,并与工作,而不是?这比将单个字符串部分分解为日期片段要复杂得多并且远为可靠。 – Makoto
compareTo不返回-1或0或1,请参阅apidoc:“将此对象与指定的对象进行比较,返回负整数,零或正整数,因为此对象小于,等于或大于指定的对象“。 –
你真的需要将日期转换为字符串吗?使用Date API中的'after'和'before'方法。 –