0
您好我正在使用SimpleDateFormat解析和比較字符串中的兩個日期。這裏是我的代碼SimpleDateFormat異常
private static int compareDates(String lineFromFile, String givenDate) throws ParseException, IllegalArgumentException
{
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date dateFromfile = sdf.parse(tmp);
Date givenDateTime = sdf.parse(givenDate);
if (dateFromfile.equals(givenDateTime))
{
return 0;
}
if (dateFromfile.before(givenDateTime))
{
return 1;
}
return -1;
}
這裏是一個主要方法
public static void main(String[] args) {
try
{
int result = compareDates("00:45:44", "09:35:56");
System.out.println(line);
}
catch (ParseException e)
{
e.printStackTrace();
System.out.println("ERROR");
}
}
這正常工作,當我路過有效參數,但!希望在傳遞例如「28:40:04」時出現異常,現在我只有在傳遞包含字母的參數字符串時纔有例外。
我想這一點,但它的結果是一樣的 –
其實這是默認寬鬆,你需要將其設置爲false:sdf.setLenient(假); – lbalazscs
謝謝,它有幫助! –