try{ Date.valueOf(String aString);}catch(Exception ex){ System.err.println("The format of string is not correct");}
或者
public class forother { public static void main(String[] args){ SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss"); String ooo="2005-02-18 15:39:15"; String ppp="2005-02-01 15:25:15"; try{ Date tt=s.parse(ooo); Date yy=s.parse(ppp); long aa; if(tt.getTime()>yy.getTime()) aa=tt.getTime()-yy.getTime(); else aa=yy.getTime()-tt.getTime(); long date=aa/1000/60/60/24; System.out.println("***"); System.out.println("hahahha"+date); System.out.println("*******"); }catch(Exception e){ } }}