处理从日期选择器解析异常到日期格式

huangapple go评论79阅读模式
英文:

Handling parse exception to a date format from a date picker

问题

这里我正在尝试使用LocalDate和日期格式化来处理预订系统的开始日期和结束日期。我已经搜索了我遇到的错误,然而它们与类似的问题无关。就像这里我需要使用request.getParameter来获取用户输入。

// 从jsp获取输入值
String cruiseName = request.getParameter("cruiseName");
DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MMM/yyyy");

LocalDate startDate = LocalDate.parse(request.getParameter("startDate"), df);

LocalDate endDate = LocalDate.parse(request.getParameter("endDate"), df);

错误信息:

异常
org.springframework.web.util.NestedServletException: 请求处理失败;嵌套异常是java.time.format.DateTimeParseException: 无法解析文本 '03/10/2020'(索引 3 处的字符)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)** 
    
根本原因:
java.time.format.DateTimeParseException: 无法解析文本 '03/10/2020'(索引 3 处的字符)
	java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
	java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)
	java.base/java.time.LocalDate.parse(LocalDate.java:428)
英文:

Here I am trying to handle the start date of my booking system as well as the end date using LocalDate and date formatted. I have done some search on the error I am getting, however, they don't deal with similar issues. Like here I need to get the user input using request.getParameter.

        //Getting input values from jsp from 
	    String cruiseName = request.getParameter("cruiseName");
	    DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MMM/yyyy");

		LocalDate startDate =(LocalDate) LocalDate.parse(request.getParameter("startDate"),df);
	
		LocalDate endDate =(LocalDate) LocalDate.parse(request.getParameter("endDate"),df);

The error message:

Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.time.format.DateTimeParseException: Text '03/10/2020' could not be parsed at index 3
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)** 

Root Cause:
java.time.format.DateTimeParseException: Text '03/10/2020' could not be parsed at index 3
	java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
	java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)
	java.base/java.time.LocalDate.parse(LocalDate.java:428)

答案1

得分: 0

你尝试解析的格式与实际获得的格式不同,可能是因为用户提供的格式有误,或者你使用了错误的格式进行解析。

你正在尝试解析 dd/MMM/yyyy 格式,它看起来像是 03/Oct/2020,但你实际获得的格式可能是 dd/MM/yyyy 或者 MM/dd/yyyy,根据你得到的错误消息,例如 03/10/2020,可能是两者之一。你需要检查预期的输入格式,并调整你的解析代码,或者向用户询问特定的格式。

英文:

The format you are trying to parse is different from the format you get, either because there's incorrect format asked from the user, or because you used the wrong format to parse.

You are trying to parse dd/MMM/yyyy format which would look like 03/Oct/2020, but the format you get is either dd/MM/yyyy or MM/dd/yyyy, from the error message you get 03/10/2020 which can be either. You need to check what the expected input is and adjust your parsing code, or ask user for particular format.

huangapple
  • 本文由 发表于 2020年3月16日 21:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/60706771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定