我有@Entity
个java.util.Date
个字段;其中两个应该是时间格式化的。我需要一种方法来接受时间,最好是选择一个,以便坚持到我的数据库。
我试过使用,例如,
@DateTimeFormat(pattern="hh:mm a")
private Date startTime;
与此一起,以及添加type="time"
等的各种尝试,
...
<label for="startTime" class="sr-only">Start</label>
<form:input path="startTime" name="startTime" placeholder="Start" /
...
...但我收到Bad Request
错误。
我知道这意味着什么,我只需要知道一种可靠的解决方法。如何在Spring表单中可靠地接受时间输入?
<小时/> 其他网络信息:
Remote Address:[::1]:8080
Request URL:http://localhost:8080/shift_create/1.html
Request Method:POST
Status Code:400 Bad Request
Response Headers
view source
Cache-Control:must-revalidate,no-cache,no-store
Content-Length:307
Content-Type:text/html; charset=ISO-8859-1
Date:Mon, 10 Aug 2015 08:37:27 GMT
Server:Jetty(9.2.8.v20150217)
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:58
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=1w0rkel0w4eha96edvwq7rz1m
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/profile.html
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36
Form Data
view source
view URL encoded
name:ThisOne
shiftDate:08/13/2015
startTime:10:10 PM
Form data source : name=ThisOne&shiftDate=08%2F13%2F2015&startTime=10%3A10+PM
答案 0 :(得分:0)
向控制器添加InitBinder
并指定要获取的格式:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(true);
sdf.setTimeZone(TimeZone.getTimeZone("CET"));
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf,true));
}
答案 1 :(得分:0)
获得接受时间的方式原来是
String
return
a Date
POST
上使用(4)中的方法将数据从DTO传输到要保存到数据库的实体大部分代码都是微不足道的;解析部分可能如下所示:
// Time from a ClockPicker is "hh:mm"
private Date getTime(String time) {
if (time != null)
return makeCalendar(getTimeComponents(getTimeComponents(time))).getTime();
return null;
}
private String[] getTimeComponents(String time) {
return time.split(":");
}
private int[] getTimeComponents(String... time) {
int hour = Integer.parseInt(time[0]);
return new int[] {
hour,
Integer.parseInt(time[1]),
0,
hour >= PM ? Calendar.AM : Calendar.PM
};
}