如何在Servlet里,对日期做格式验证?
解决方案 »
- Java程序员是不是每天很清闲啊,看了一些Java代码很有感触啊
- 有没有用httpclient包的?请教个问题
- 请问如何写程序替换jar中的文件?
- EJB是基于哪些技术实现的?并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别。
- 请问这是怎么错,怎么改!
- 关于接口和抽象类
- 还是没有解决驱动问题啊~~~java连sql server2005这已经是第三贴了~~~前两贴都沉了
- 鼠标事件参数获取的点和实际点击点不匹配怎么办
- sql server的驱动包那里有下载?
- 请您分析这个架构,看看您能想多少???(千万别错过哟!!!)
- 请问问题 求正则表达式 得到值
- 如word一样菜单下边的一排图标,在JFrame中能够加入吗?谢谢了。
* 检验输入是否为正确的日期格式(不含秒的任何情况),严格要求日期正确性,格式: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 (Date)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+"是否为日期:"+TestCompile.checkDate(s)); s="2006/1/30 12:26";
System.out.println(s+"是否为日期:"+TestCompile.checkDate(s));
s="2006/01/32 12:26";
System.out.println(s+"是否为日期:"+TestCompile.checkDate(s));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter(); Long libraryId = Long.valueOf(request.getParameter("library_id"));
LibraryService libraryservice = new LibraryService();
Libraryinfo libraryinfo = libraryservice.getLibraryinfoById(libraryId
.longValue());
String title = request.getParameter("title");
String type = request.getParameter("type");
String author = request.getParameter("author");
String local_foreign = request.getParameter("local_foreign");
String otherinformation = request.getParameter("otherinfomation");
String edition = request.getParameter("edition");
String publisher = request.getParameter("publisher");
String shelfno = request.getParameter("shelfno");
String publicationdate = request.getParameter("publicationdate");
我想给这个publicationdate值作一个日期格式验证,要有错误提示,怎么写?