請看下邊這段代碼java
public static void main(String[] args) throws Exception { Date date1 = new Date(); System.out.println("date1: " + date1.toString()); Date date2 = new Date(date1.toString()); System.out.println("date2: " + date2.toString()); }
執行結果以下web
date1: Mon Jul 22 08:47:19 CST 2019 date2: Mon Jul 22 22:47:19 CST 2019
當前時間是2019年7月22日8點48分,CST是中國的時區China Standard Time的簡稱,可是能夠看到date2的輸入比實際時間多了14個小時。spring
CTS表明的時區其實有四個(Central Standard Time (USA) UT-6:00、Central Standard Time (Australia) UT+9:30、China Standard Time UT+8:00、Cuba Standard Time UT-4:00),同時表示美國,澳大利亞,中國,古巴四個國家的標準時間。mvc
new Date(date1.toString())
這個方法會調用Date.parse(String)方法,它傳的參數是Mon Jul 22 08:47:19 CST 2019,這個方法上有一段註釋app
* <li>Any word that matches <tt>EST, CST, MST</tt>, or <tt>PST</tt>, * ignoring case, is recognized as referring to the time zone in * North America that is five, six, seven, or eight hours west of * Greenwich, respectively. Any word that matches <tt>EDT, CDT, * MDT</tt>, or <tt>PDT</tt>, ignoring case, is recognized as * referring to the same time zone, respectively, during daylight * saving time.</ul><p>
能夠看到CST會被看成美國中部的時區Central Standard Time,即JVM認爲你傳入的時間是美國中部的時間,而當date2調用toString方法的時候,它會檢測到系統的時區是中國,就會自動加14個小時(東八區與西六區的時差),就變成了Mon Jul 22 22:47:19 CST 2019ide
這個問題其實若是本身寫代碼的話很難出現,由於全部的Java書籍都不會這麼教,大多數都是經過SimpleDateFormat,進行Date和String的轉換,畢竟new Date(date1.toString())這個方法已經標註爲廢棄了spa
Feign客戶端在進行通訊時,會調用Date的toString方法轉爲String類型,服務端在接受的時候,使用的就是new Date(String)這個方法,這裏就會發生前邊介紹的問題,產生14個小時的時差code
在客戶端添加代碼,規定Feign在將Date參數轉化成String參數的格式:orm
import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.openfeign.FeignFormatterRegistrar; import org.springframework.core.convert.converter.Converter; import org.springframework.format.FormatterRegistry; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; @Slf4j @Component public class FeignDateFormatRegister implements FeignFormatterRegistrar { @Override public void registerFormatters(FormatterRegistry registry) { registry.addConverter(Date.class, String.class, new Date2StringConverter()); } private class Date2StringConverter implements Converter<Date, String> { @Override public String convert(Date source) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(source); } } }
在服務端添加代碼,規定SpringContext在String和Date時的用的轉化器,讓轉化器知道咱們在客戶端配置的參數格式:seo
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.GenericConversionService; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import javax.annotation.PostConstruct; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @Slf4j @Configuration public class FeignConfiguration { @Autowired private RequestMappingHandlerAdapter handlerAdapter; /** * 增長字符串轉日期的功能 */ @PostConstruct public void initEditableValidation() { ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer(); if (initializer.getConversionService() != null) { GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService(); genericConversionService.addConverter(String.class, Date.class, new String2DateConverter()); } } class String2DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return simpleDateFormat.parse(source); } catch (ParseException e) { log.error("", e); } return null; } } }
注意以上兩個配置類須要本身配置包掃描之類的把它們加到Spring環境中
參考資料: