原
@requestbody接收json参数 映射到 实体类中 Date(时间日期)类型转换问题
2018年03月16日 11:03:13
TROY_Musou
阅读数:1304
</div>
<div class="operating">
</div>
</div>
</div>
</div>
<article class="baidu_pl">
<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
<div class="article-copyright">
<svg class="icon" title="CSDN认证原创" aria-hidden="true" style="width:53px; height: 18px; vertical-align: -4px;">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#CSDN_Cert"></use>
</svg>
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TROY_Musou/article/details/79578727 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<div class="htmledit_views" id="content_views">
<span><span style="color:#ff0000;">前台ajax提交, 后台@requestbody接收json参数 映射到 实体类中 Date(时间日期)类型转换问题(在字段上面加上两个注解即可)</span></span><br><br><br>date类型字段的序列化和反序列化 <br><p>@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ", locale = "zh", timezone = "GMT+8")</p><p><br></p><p>1.pattern 指定转化的格式SSSZ(S指的是微秒,Z指时区),此处的pattern和java.text.SimpleDateFormat中的Time Patterns一致</p><p><br></p><p>2.locale主要指语言,如果中文的话,月份输出是五月,但是英文就是May,尤其是在反序列化的时候特别重要</p><p><br></p>3.timezone主要解决“8小时”问题<br><br><br>JsonFormat :出参<br>DateTimeFormate : 入参<br><br><pre onclick="hljs.copyCode(event)"><code class="language-java hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">@DateTimeFormat</span>(pattern = <span class="hljs-string">"yyyy-MM-dd HH:mm:ss"</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">@JsonFormat</span>(pattern = <span class="hljs-string">"yyyy-MM-dd HH:mm:ss"</span>, timezone = <span class="hljs-string">"GMT+8"</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">private</span> Date startTime;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">@DateTimeFormat</span>(pattern = <span class="hljs-string">"yyyy-MM-dd HH:mm:ss"</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">@JsonFormat</span>(pattern = <span class="hljs-string">"yyyy-MM-dd HH:mm:ss"</span>, timezone = <span class="hljs-string">"GMT+8"</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">private</span> Date endTime;</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制"></div></pre><br><br><br> </div>
</div>
</article>