解決全域性的jackson日期轉換和解析

2021-09-18 04:09:57 字數 3950 閱讀 5476

解決全域性的jackson日期轉換和解析

因為專案有些特殊,需要解析各種格式的日期型別轉換,後來發現jackson在日期轉換上沒法滿足需要。

遂重寫了com.fasterxml.jackson.databind.util.stddateformat 

和com.fasterxml.jackson.databind.deserializationcontext。

我的版本是jackson-databind-2.8.10

@configuration

public class webconfig extends webmvcconfigureradapter

@override

public void extendmessageconverters(list> converters)

}

沒錯,只有三句話: 

- ******dateformat smt = new ******dateformat(「yyyy-mm-dd hh:mm:ss」);

剩下的全是廢話。 

這樣做只能起到微弱的卵用,僅僅只是讓每乙個date都帶有jsonformat的效果而已,於解析而言,沒有任何益處。 

後經過我的不斷試驗,發現如果我不加任何format的話,預設可以解析yyyy-mm-dd』t』hh:mm:ss.sssz、時間戳、eee, dd mmm yyyy hh:mm:ss zzz等等一系列慘不忍睹的日期格式,經過我的不斷尋找終於找到了,解析的**所在。就是這個stddateformat。這裡我一共寫了幾項:

/**

* todo 自己加入,想加多少加多少

*/protected final static string date_format_str_first= "yyyy-mm-dd hh:mm:ss";

protected final static string date_format_str_second= "eee mmm dd hh:mm:ss z yyyy";`

/**

* for error messages we'll also need a list of all formats.

*/protected final static string all_formats = new string ;

protected final static dateformat date_format_first;//也是同理,照葫蘆畫瓢

protected final static dateformat date_format_second;

date_format_first = new ******dateformat(date_format_str_first, default_locale);

date_format_first.settimezone(default_timezone);

date_format_second = new ******dateformat(date_format_str_second, default_locale);

date_format_second.settimezone(default_timezone);

@override

public date parse(string datestr) throws parseexception

else }}

if ((i < 0)

// let's just assume negative numbers are fine (can't be rfc-1123 anyway); check length for positive

&& (datestr.charat(0) == '-' || numberinput.inlongrange(datestr, false))) else

}if (dt != null)

///這裡加入解析是非常重要的一步,這個類有兩個parse()方法,寫在乙個引數的那個parse()方法裡面,

//parsedate()建議大家直接引入別人的jar包裡封裝好的方法,我這裡自己寫是因為eee mmm dd hh:mm:ss z yyyy

//的格式必須要設定正確的locale才可以解析

string s=;

dt = parsedate(datestr, s,_locale);

if (dt != null)

/        stringbuilder sb = new stringbuilder();

for (string f : all_formats) else

}throw new parseexception

(string.format("can not parse date \"%s\": not compatible with any of standard forms (%s)",

datestr, sb.tostring()), pos.geterrorindex());

}

//雖然不是必要的,但還是將這個方法貼出來

private static date parsedate(string str, string parsepatterns, locale _locale)

throws parseexception

******dateformat parser = null;

parseposition pos = new parseposition(0);

for (int i = 0; i < parsepatterns.length; i++)

pos.setindex(0);

date date = parser.parse(str, pos);

if ((date != null) && (pos.getindex() == str.length()))

}throw new parseexception("unable to parse the date: " + str, -1);

}

這個時候發現不對勁

加入全域性格式轉換後 

failed to parse date value 『xx』: yyyy-mm-dd hh:mm:ss

明顯和 

throw new parseexception (string.format(「can not parse date \」%s\」: not compatible with any of standard forms (%s)」, 

datestr, sb.tostring()), pos.geterrorindex());

報的錯不一樣!, 

不加型別轉換,報下面這個。加了就調皮。 

鑑於我人傻頭鐵時間多,接著找。找到了deserializationcontext。這個類 

他的報錯是

throw new illegalargumentexception(string.format(

"failed to parse date value '%s': %s", datestr, e.getmessage()));

這麼看來應該是他沒跑兒了

//修改parsedate

public date parsedate(string datestr) throws illegalargumentexception

catch (parseexception e) catch (parseexception e1) //}

}

轉至:

感謝大哥的好文章!!

Jackson 實現 bean和json的轉換

bean轉換成json public static void main string args throws jsonprocessingexceptionjson串 轉換成bean string result 這是我實際用到的,除此之外 jackson的api還有很多其他方法。有的時候我們根據具體...

日期的格式 字串轉日期

var mydate new date 輸出 sat nov 26 2016 16 15 27 gmt 0800 中國標準時間 mydate.tolocaledatestring 可以獲取當前日期 下午4 15 27 mydate.tolocaletimestring 可以獲取當前時間 2016 1...

js中日期轉字串,字串轉日期,日期的用法

目錄 建立日期 指定月最後一天 當day為負數的時候,就表示的是指定月的最後一天的向前偏移量。當前日期加一天 日期轉字串 字串轉日期 new date year,month,day 然後簡單使用一下date物件提供的建構函式。var newdate new date 2019,10,9 format...