1

我正在使用 jquery timepicker 插件,它工作正常。我可以选择一个时间并提交我的表格。我正在使用spring MVC 3.0,当我尝试将代表时间的字符串解析为java carlendar时出现问题。

我一直在阅读这个日期/时间转换,http://www.odi.ch/prog/design/datetime.php,它看起来很复杂。有人可以提供某种形式的建议。这是我的代码,它是特定于弹簧的。

@RequestMapping(value = "scheduleadd", method = RequestMethod.POST)
public String scheduleadd(  @Valid Schedule schedule, BindingResult bindingResult,
                            @RequestParam("startdate") @org.springframework.format.annotation.DateTimeFormat(iso=ISO.DATE) java.util.Calendar startdate,
                            @RequestParam("enddate") @org.springframework.format.annotation.DateTimeFormat(iso=ISO.DATE) java.util.Calendar enddate,
                            @RequestParam("starttime") @org.springframework.format.annotation.DateTimeFormat(iso=ISO.NONE) java.util.Calendar starttime,
                            @RequestParam("endtime") @org.springframework.format.annotation.DateTimeFormat(iso=ISO.NONE) java.util.Calendar endtime,
                            @RequestParam("moduleInstanceId") Long mId, Model uiModel, HttpServletRequest httpServletRequest) { //my stuff goes here}

如您所见,我正在尝试将“09:30”之类的字符串解析为java Carlendar。我需要日期部分吗?如何指定日期部分?

4

1 回答 1

3

使用@DateTimeFormatpattern的属性来指定“时间”字段不是完全格式的ISO 日期时间,而只是时间;例如:

...
@RequestParam("starttime") @DateTimeFormat(pattern="hh:mm") Calendar starttime,
@RequestParam("endtime") @DateTimeFormat(pattern="hh:mm") Calendar endtime,
...

在您的scheduleadd()方法中,组合这些Calendar字段以获得完整的日期时间:

startdate.set(Calendar.HOUR_OF_DAY, starttime.get(Calendar.HOUR_OF_DAY));
startdate.set(Calendar.MINUTE, starttime.get(Calendar.MINUTE));
...
于 2011-10-24T22:30:25.653 回答