@JsonFormat時間格式化註解使用
@JsonFormat時間格式化註解使用
Javaweb 太平洋學習網 1013瀏覽 評論 <div class="banner banner-post"><div style="text-align:left;padding-left:20px;margin:5px 0 -5px;">
<!-- 文章頁頂部廣告 尺寸:950*70 -->
檢視
@JsonFormat註解是一個時間格式化註解,比如我們儲存在mysql中的資料是date型別的,當我們讀取出來封裝在實體類中的時候,就會變成英文時間格式,而不是yyyy-MM-dd HH:mm:ss這樣的中文時間,因此我們需要用到JsonFormat註解來格式化我們的時間。
JsonFormat註解是jackson包裡面的一個註解,因此在使用的時候需要引入fasterxml maven的jar包,如下所示。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
引入fasterxml maven jar包之後,就可以在實體類屬性上面使用@JsonFormat註解了,要注意的是,它只會在類似@ResponseBody返回json資料的時候,才會返回格式化的yyyy-MM-dd HH:mm:ss時間,你直接使用System.out.println()輸出的話,仍然是類似“Fri Dec 01 21:05:20 CST 2017”這樣的時間樣式。
package demo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Student {
private int id;
private String username;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date createDate;
//getter setter省略。。。
}
當我們這樣@ResponseBody輸出json資料的時候,@JsonFormat註解標識的date屬性就會自動返回yyyy-MM-dd HH:mm:ss樣式的時間了,例如。
@PostMapping("/api/getStudent")
@ResponseBody
public Map<String,Object> findStudentById(Long stuId){
Map<String,Object> resultMap = new HashMap<>();
Student stu = StudentService.findStudentById(stuId);
resultMap.put(“result”,stu);
return resultMap;
}
來源網站:太平洋學習網,轉載請註明出處:http://www.tpyyes.com/a/javaweb/2017/1201/417.html
</article>
<footer class="article-footer">
<!-- <div class="article-tags" style="visibility:hidden">繼續瀏覽有關 JsonFormat 的文章</div> <div class="share" style="height:100%"><div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare"> -->
<div style="font-size:16px;height:100%"><a target="_blank" style="color:red;height:100%" href="https://www.joingf.com/zh/vyprvpn/world-cup-special?offer_id=235&aff_id=4585">點選註冊下載網路加速器,輕鬆瀏覽Facebook,Google,twitter等網站,無限制觀看1080P高清YouTube視訊!</a></div>
<!-- </div></div> --> </footer>
<nav class="article-nav">
<span class="article-nav-prev">上一篇:<a href="/a/javaweb/2017/1126/408.html">java.lang.ArrayIndexOutOfBoundsException錯誤解決</a> </span>
<span class="article-nav-next">下一篇:<a href="/a/javaweb/2017/1204/420.html">java漢字轉拼音pinyin4j功能實現示例</a> </span>
</nav>
<div class="relates">
<h3>與本文相關的文章</h3>
<ul>
<div class="banner banner-related"><div style="margin-bottom:-6px;">
<!-- 文章頁底部廣告 尺寸:950*70 -->
</div></div>
<div class="comt-title">
<div class="comt-avatar pull-left">
<img alt="" src="/dede58/img/head.png" class="avatar avatar-28 photo avatar-default" height="28" width="28"> </div>
<div class="comt-author pull-left">
發表我的評論 </div>
<a id="cancel-comment-reply-link" class="pull-right" href="javascript:;">取消評論</a>
</div>
<div class="comt">
<div class="comt-box">
<textarea placeholder="寫點什麼..." class="input-block-level comt-area" name="comment" id="comment" cols="100%" rows="3" tabindex="1" onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('submit').click();return false};"></textarea>
<div class="comt-ctrl">
<button class="btn btn-primary pull-right" type="button" onclick="PostComment()" name="submit" id="submit" tabindex="5"><i class="icon-ok-circle icon-white icon12"></i> 提交評論</button>
<div class="comt-tips pull-right"><input type="hidden" name="comment_post_ID" value="5127" id="comment_post_ID">
脮媒脭脷脤謾陸祿, 脟毛脡脭潞貌...
#
表情
<div class="comt-comterinfo" id="comment-author-info">
<h4><span class="form-inline">
</span>您的回覆是我們的動力!</h4>
<ul>
<li class="form-inline"><label class="hide" for="author">暱稱</label><input class="ipt" type="text" name="username" id="author" value="遊客" tabindex="2" placeholder="暱稱"><span class="help-inline">暱稱 (必填)</span></li>
<li class="form-inline"><input class="ipt" type="hidden" name="email" id="email" value="" tabindex="3" placeholder="郵箱"></li>
</ul>
</div>
</div>
</form>
</div>
網友最新評論
}
function quoteCommet(fid)
{
document.feedback.fid.value = fid;
}
function ajaxFeedback(aid, fid, type)
{
var taget_obj = $DE('ajaxfeedback_'+fid);
if(taget_obj.innerHTML == '')
{
var myajax = new DedeAjax(taget_obj, true, true, '', 'x');
myajax.SendGet2("/plus/feedback.php?aid="+aid+"&fid="+fid+"&action=quote&type=ajax");
eval('var result = typeof CKEDITOR.instances.msg_'+fid);
if(result != 'undefined')
{
// 刪除例項
eval('var edit = CKEDITOR.instances.msg_'+fid);
CKEDITOR.remove(edit);
}
CKEDITOR.replace(document.getElementById('msg_'+fid) , CKEDITOR.instances.msg.config);
scroll(0, taget_obj.offsetTop - 120);
var formname = 'f = document.ajaxfeedback_'+fid;
eval(formname);
if(f.validate)
{
if($DE('vdimg_'+fid)) $DE('vdimg_'+fid).src = "/include/vdimgck.php?"+f.validate.value;
f.validate.value = '';
}
DedeXHTTP = null;
}
}
function ajaxQuotePost(fid)
{
var formname = ‘f = document.ajaxfeedback_’+fid;
eval(formname);
//var f = document.formname;
//var f = f[0];
var nvalidate = ‘’;
var nnotuser = ‘’;
var nusername = ‘’;
var npwd = ‘’;
var taget_obj = $DE(‘commetcontentNew’);
var waithtml = “
eval(‘var msg = CKEDITOR.instances.msg_’+fid+’.getData()’);
if(f.validate)
{
if(f.validate.value=='') {
alert("請填寫驗證碼!");
return;
}
else {
nvalidate = f.validate.value;
}
}
var myajax = new DedeAjax(taget_obj, false, true, '', '', waithtml);
if(f.notuser.checked) nnotuser = '1';
if(f.username) nusername = f.username.value;
if(f.pwd) npwd = f.pwd.value;
myajax.sendlang = 'gb2312';
myajax.AddKeyN('dopost', 'send');
myajax.AddKeyN('aid', '417');
myajax.AddKeyN('fid', f.fid.value);
myajax.AddKeyN('type', 'ajax');
myajax.AddKeyN('comtype', f.comtype.value);
myajax.AddKeyN('isconfirm','yes');
myajax.AddKeyN('typeid', f.typeid.value);
myajax.AddKeyN('quotemsg', f.quotemsg.value);
myajax.AddKeyN('validate', nvalidate);
myajax.AddKeyN('notuser', nnotuser);
myajax.AddKeyN('username', nusername);
myajax.AddKeyN('pwd', npwd);
myajax.AddKeyN('msg', msg);
console.log(“這裡也執行了嗎?====”);
myajax.SendPost2(’/plus/feedback_ajax.php’);
//alert(f.quotemsg.value);
if($DE(‘ajaxfeedback_’+fid).innerHTML != null)
{
$DE(‘ajaxfeedback_’+fid).innerHTML = ‘’;
}
scroll(0, taget_obj.offsetTop);
}
LoadCommets(1);
</div>