2017-05-24 43 views
0

1):发送日期从自举日期选择器到弹簧控制器,具有时区

<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 
} 

实施例:我选择数据中心(bootstrap)10.02.2017(№1)并获得控制器中的测试日期(№2)09.02.2017 23:00:00。

我认为Spring或java.util.Date将testDate转换为server TimeZone。但我可能是错误的

如何发送日期值到Spring Controller从引导日期选择器jsp与客户端TimeZone?我应该得到10.02.2017

回答

0

我找到了解决方案。 转换的日期类型值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; 
} 

,并加入到类@Configuration

@Override 
public void addFormatters(FormatterRegistry registry) { 
    registry.addConverter(new DateConverter()); 
} 

当然可以覆盖梅托德转换不同