例如:
65438+10月31,表示為0131。
65438+2月3日,表示為1203。
也就是說,月份和日期各占兩個字符,沒有數字的位置用0填充。
在Java中,String是按照字典順序進行排序和比較的,所以0X壹定比任何XX都小,而在XX中,第壹位大,整個XX也大。反之,第壹位小,整個XX也小,符合月日對比的邏輯。
所以,可以這樣寫:
公共靜態字符串季節(int a,int b)
{
字符串s =//季節
字符串month =
字符串day =
如果(a & lt10)
month = " 0 "+string . value of(a);
其他
month = string . value of(a);
if(b & lt;10)
day = " 0 "+string . value of(b);
其他
day = string . value of(b);
字符串日期=月+日;
if(date . compare to(" 0316 ")& gt;= 0 & amp& ampdate . compare to(" 0615 ")& lt;=0)
s = "春天";
else if(date . compare to(" 0616 ")& gt;= 0 & amp& ampdate . compare to(" 0915 ")& lt;=0)
s = "夏天";
else if(date . compare to(" 0916 ")& gt;= 0 & amp& ampdate . compare to(" 1215 ")& lt;=0)
s = " Fall
else if((date . compare to(" 1216 ")& gt;= 0 & amp& ampdate . compare to(" 1231 ")& lt;= 0)| |(date . compare to(" 0101 ")& gt;= 0 & amp& ampdate . compare to(" 0315 ")& lt;=0))
s = "冬天";
其他
s = "錯誤!";//輸入的日期格式有錯誤。
返回s;
}