【问题标题】:SimpleDateFormat Parsing error with jQueryUI datepickerjQueryUI datepicker 的 SimpleDateFormat 解析错误
【发布时间】:2013-03-25 18:39:29
【问题描述】:

我正在尝试使用 jQueryUi datepicker 中的 SimpleDateFormat 解析日期值,但每次我尝试将其保存到数据库时,它都会以一种奇怪的格式保存,例如我输入 02/10/1995 它会保存为 0002- 1 月 1 日。

try {
        Date albumRelease = new SimpleDateFormat("MM/dd/yyyy").parse(request.getParameter("AlbumRelease"));
        album.setAlbumRelease(albumRelease);
    } catch (ParseException e) {
        e.printStackTrace();
    }


<label>Release Date</label>
<input type="text" name="AlbumRelease" id="datepicker" value="<fmt:formatDate pattern="MM/dd/yyyy" value="${album.albumRelease}" />" />

调试模式下的变量检查将 albumRelease 返回为“Sun Dec 27 00:00:00 IST 11”

我在 AlbumController 的第 60 行遇到断点错误)

https://github.com/linuxsavvy/music-library/blob/master/com.solancer/src/com/solancer/controller/AlbumController.java

https://github.com/linuxsavvy/music-library/blob/master/com.solancer/WebContent/Album.jsp

【问题讨论】:

  • 如果输入String02/10/1995,您是否应该使用SimpleDateFormat("MM/dd/yyyy")
  • 抱歉,这是一个测试代码,但我使用的是 MM/dd/yyyy。
  • 在servlet中,你是如何获取请求参数值的?
  • 此外,我认为parse 方法需要两个这样的参数。 new SimpleDateFormat("MM/dd/yyyy").parse(request.getParameter("AlbumRelease"), new ParsePosition(0));。看看docs.oracle.com/javase/7/docs/api/java/text/…
  • @RavindraGullapalli request.getParameter("AlbumRelease");

标签: java jsp jsp-tags simpledateformat


【解决方案1】:

当您在 servlet 中获得 Sun Dec 27 00:00:00 IST 11 时,

您必须在 servlet 中使用 SimpleDateFormat("EEE MMM dd HH:mm:ss z yy") 而不是 SimpleDateFormat("MM/dd/yyyy")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多