使用 TimeZone 将日期从 bootstrap 日期选择器发送到 Spring 控制器
Send Date from bootstrap datepicker to Spring Controller with TimeZone
1) jsp 页:
<input class="datepicker"/> -- 10.02.2017
jQuery:
$('.datepicker').datepicker({
'format': 'dd.mm.yyyy',
});
2) 控制器 - Test.java
@RequestMapping("test")
public ModelAndView test(@PathVariable Date testDate){
// testDate = 09.02.2017 23:00:00
}
示例:我在 datapicker (bootstrap) 10.02.2017 (№1) 中选择并在 Controller (№2) 09.02.2017 中获取 testDate 23:00:00.
我认为 Spring 或 java.util.Date 将 testDate 转换为服务器时区。但我可能是错的
如何使用客户端 TimeZone 从 jsp 上的 bootstrap 日期选择器向 Spring 控制器发送日期值?我应该得到 10.02.2017
我找到了适合我的解决方案。
转换的日期类型值 Spring。
我已经写了:
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat sf = new SimpleDateFormat("dd.MM.yyyy");
if (!source.isEmpty()){
try {
return sf.parse(source);
} catch (ParseException e) {
LOGGER.error(e.getLocalizedMessage());
}
}
return null;
}
并添加到 class @Configuration
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter());
}
当然可能会以不同的方式覆盖方法转换
1) jsp 页:
<input class="datepicker"/> -- 10.02.2017
jQuery:
$('.datepicker').datepicker({
'format': 'dd.mm.yyyy',
});
2) 控制器 - Test.java
@RequestMapping("test")
public ModelAndView test(@PathVariable Date testDate){
// testDate = 09.02.2017 23:00:00
}
示例:我在 datapicker (bootstrap) 10.02.2017 (№1) 中选择并在 Controller (№2) 09.02.2017 中获取 testDate 23:00:00.
我认为 Spring 或 java.util.Date 将 testDate 转换为服务器时区。但我可能是错的
如何使用客户端 TimeZone 从 jsp 上的 bootstrap 日期选择器向 Spring 控制器发送日期值?我应该得到 10.02.2017
我找到了适合我的解决方案。 转换的日期类型值 Spring。 我已经写了:
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat sf = new SimpleDateFormat("dd.MM.yyyy");
if (!source.isEmpty()){
try {
return sf.parse(source);
} catch (ParseException e) {
LOGGER.error(e.getLocalizedMessage());
}
}
return null;
}
并添加到 class @Configuration
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter());
}
当然可能会以不同的方式覆盖方法转换