?? /**
???? * 檢驗輸入是否為正確的日期格式(不含秒的任何情況),嚴格要求日期正確性,格式:yyyy-MM-dd HH:mm
???? * @param sourceDate
???? * @return
???? */
??? public static boolean checkDate(String sourceDate){
????????if(sourceDate==null){
????????????return false;
????????}
????????try {
?????????????? SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
?????????????? dateFormat.setLenient(false);
???????????????dateFormat.parse(sourceDate);
???????????????return true;
????????} catch (Exception e) {
????????}
?????????return false;
??? }
???
??? /**
???? * 根據日期獲得日期(不含秒的任何情況),嚴格要求日期正確性,格式:yyyy-MM-dd HH:mm
???? * @param sourceDate
???? * @return
???? */
??? public static Date getDate(String sourceDate) throws DataFormatException{
???????????if(sourceDate==null){
???????????????throw new DataFormatException("源數據為null");
???????????}
???????????try {
???????????????????? SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
?????????????????????dateFormat.setLenient(false);
??????????????????????return dateFormat.parse(sourceDate);
???????????} catch (Exception e) {
?????????????????????throw new DataFormatException("源數據格式錯誤");
???????????}
??? }


//日期格式可以自定義.
??? public static void main(String[] args) throws IOException {
??? ?????? ? String s="2006-01-30 12:26";
???????????? System.out.println(s+"是否為日期:"+Test.checkDate(s));

???????????? s="2006-1-30 12:26";
???????????? System.out.println(s+"是否為日期:"+Test.checkDate(s));
????????
?????????? ?s="2006-01-32 12:26";
??????????? System.out.println(s+"是否為日期:"+Test.checkDate(s));
??? }