springboot中怎么對date參數(shù)進(jìn)行處理,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

10年積累的成都網(wǎng)站制作、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有耿馬免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
測試方法
bean代碼:
public class DateModelNoAnnotation {
private Integer id;
private Date receiveDate;
}controller代碼:
@RestController
@RequestMapping("/date")
public class DateVerifyController {
// 方式一
@PostMapping("/no")
public String dateUnNoAnnotation(DateModelNoAnnotation dateModelNoAnnotation){
System.out.println(dateModelNoAnnotation.toString());
return "SUCCESS";
}
// 方式二
@PostMapping("/has")
public String dateHasAnnotation(@RequestBody DateModelNoAnnotation dateModelNoAnnotation){
System.out.println(dateModelNoAnnotation.toString());
return "SUCCESS";
}
// 方式三
@GetMapping("/param")
public String dateParams(@RequestParam("id")Integer id, @RequestParam("receiveDate")Date receiveDate){
System.out.println("id====="+id);
System.out.println("receiveDate====="+receiveDate);
System.out.println("receiveDate====="+receiveDate.getTime());
return "SUCCESS";
}
// 方式四
@GetMapping("/no/param")
public String dateNoParams(Integer id,Date receiveDate){
System.out.println("id====="+id);
System.out.println("receiveDate====="+receiveDate);
System.out.println("receiveDate====="+receiveDate.getTime());
return "SUCCESS";
}
}接收參數(shù)的幾種方式(實(shí)驗(yàn))
通過bean來接收數(shù)據(jù)(表單方式)
這種方式只支持"yyyy/MM/dd HH:mm:ss"這種格式的time參數(shù)
通過bean來接收數(shù)據(jù)(json格式)
這種方式只支持"yyyy-MM-dd HH:mm:ss"這種格式的time參數(shù)
通過RequestParam注解
這種方式只支持"yyyy/MM/dd HH:mm:ss"這種格式的time參數(shù)
不通過RequestParam注解
這種方式只支持"yyyy/MM/dd HH:mm:ss"這種格式的time參數(shù)
以上幾種接收參數(shù)的方式接收的參數(shù)格式并不統(tǒng)一,而且有時(shí)候web前端傳入的時(shí)間參數(shù)為時(shí)間戳,還得寫修改接口或者讓其自己修改格式;
后端給前端統(tǒng)一返回json格式的數(shù)據(jù),且時(shí)間格式為"yyyy-MM-dd HH:mm:ss"
解決方案
開發(fā)之前統(tǒng)一時(shí)間接口接收的時(shí)間格式
一 yyyy/MM/dd HH:mm:ss 格式
后端所有接口統(tǒng)一接收"yyyy/MM/dd HH:mm:ss"或"yyyy/MM/dd"格式時(shí)間參數(shù)
第一種: 舍棄上邊的方式二的接口
第二種:不舍棄方拾二,在bean的時(shí)間屬性上添加JsonFormat注解,例如:
com.fasterxml.jackson.annotation.JsonFormat; @JsonFormat(timezone = "GMT+8",pattern = "yyyy/MM/dd HH:mm:ss") private Date receiveDate;
優(yōu)勢: 不舍棄方式二接口,且統(tǒng)一了時(shí)間格式
使用該注解的弊端: 當(dāng)pattern="yyyy/MM/dd" 時(shí), 只支持處理“2019/09/03"格式時(shí)間參數(shù),不支持“2019/09/03 00:00:00”,且會報(bào)錯,當(dāng)pattern="yyyy/MM/dd HH:mm:ss"時(shí),只支持處理“2019/09/03 00:00:00"格式時(shí)間參數(shù),其余格式均會報(bào)錯;
二 接收所有時(shí)間格式
yyyy-MM-dd HH:mm:ss 格式
yyyy-MM-dd 格式
時(shí)間戳
yyyy/MM/dd HH:mm:ss 格式
yyyy/MM/dd 格式
注意
該方式不對json或xml的數(shù)據(jù)處理,比如使用@RequestBody注解的bean(也就是方式二)
工具類:
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author gyc
* @title: DateConverter
* @projectName app
* @date 2019/8/1914:36
* @description: 時(shí)間轉(zhuǎn)換類
*/
public class CourseDateConverter implements Converter<String, Date> {
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String dateFormata = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd";
private static final String shortDateFormata = "yyyy/MM/dd";
private static final String timeStampFormat = "^\\d+$";
@Override
public Date convert(String value) {
if(StringUtils.isEmpty(value)) {
return null;
}
value = value.trim();
try {
if (value.contains("-")) {
SimpleDateFormat formatter;
if (value.contains(":")) {
//yyyy-MM-dd HH:mm:ss 格式
formatter = new SimpleDateFormat(dateFormat);
} else {
//yyyy-MM-dd 格式
formatter = new SimpleDateFormat(shortDateFormat);
}
return formatter.parse(value);
} else if (value.matches(timeStampFormat)) {
//時(shí)間戳
Long lDate = new Long(value);
return new Date(lDate);
}else if (value.contains("/")){
SimpleDateFormat formatter;
if (value.contains(":")) {
// yyyy/MM/dd HH:mm:ss 格式
formatter = new SimpleDateFormat(dateFormata);
} else {
// yyyy/MM/dd 格式
formatter = new SimpleDateFormat(shortDateFormata);
}
return formatter.parse(value);
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
}將時(shí)間轉(zhuǎn)換類應(yīng)用到接口上
介紹兩種方式:使用@Component + @PostConstruct或@ControllerAdvice + @InitBinder
第一種方式:
@Component + @PostConstruct
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;
@Component
public class WebConfigBeans {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@PostConstruct
public void initEditableAvlidation() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)handlerAdapter.getWebBindingInitializer();
if(initializer.getConversionService()!=null) {
GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService();
genericConversionService.addConverter(new DateConverterConfig());
}
}
}第二種方式:
@ControllerAdvice + @InitBinder
import com.aegis.config.converter.DateConverter;
import com.aegis.model.bean.common.JsonResult;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
@ControllerAdvice
public class CourseControllerHandler {
@InitBinder
public void initBinder(WebDataBinder binder) {
GenericConversionService genericConversionService = (GenericConversionService) binder.getConversionService();
if (genericConversionService != null) {
genericConversionService.addConverter(new CourseDateConverter());
}
}
}看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
分享名稱:springboot中怎么對date參數(shù)進(jìn)行處理
網(wǎng)站路徑:http://chinadenli.net/article42/jhpehc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、小程序開發(fā)、App開發(fā)、全網(wǎng)營銷推廣、靜態(tài)網(wǎng)站、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)