判斷日期是否合法的方法

2021-06-08 12:14:45 字數 716 閱讀 7874

public static boolean chkdateformat(string date) ")) 

int year = integer.parseint(date.substring(0, 4));

int month = integer.parseint(date.substring(4, 6)) - 1;

int day = integer.parseint(date.substring(6));

calendar calendar = gregoriancalendar.getinstance();

// 當 calendar 處於 non-lenient 模式時,如果其日曆欄位中存在任何不一致性,它都會丟擲乙個異常。

calendar.setlenient(false);

calendar.set(calendar.year, year);

calendar.set(calendar.month, month);

calendar.set(calendar.date, day);

// 如果日期錯誤,執行該語句,必定丟擲異常.

calendar.get(calendar.year);

} catch (illegalargumentexception e)

return true;

}

這個類是用於判斷所輸入的日期是否合法

Java判斷日期是否合法

記錄一下專案中遇到的乙個bug,當時原有工具類中的 如下 public static boolean isdate string str catch exception e return result 當輸入字串為2018 02 31時返回true,日期預設轉換為了2018 03 03,未對時間格式...

shell指令碼判斷日期是否合法

就是判斷日期是不是真實的日期,比如20191115就是合法的日期,20191132就是不合法的 bin bash f etc init.d functions source etc init.d functions echo 函式庫檔案不存在!read p 請輸入乙個純8位數字組成的日期 date ...

判斷操作是否合法A

time limit 1 sec memory limit 128 mb submit 44 solved 14 submit status web board 假設以i和o分別表示進棧和出棧操作,棧的初態和終態均為空,進棧和出棧的操作序列可表示為僅由i和o組成的序列。順序棧的定義為 typedef...