多种时间格式字符串转换为Date对象
生活随笔
收集整理的這篇文章主要介紹了
多种时间格式字符串转换为Date对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
maven引入包
org.apache.commons
commons-lang3
3.8
使用類
org.apache.commons.lang3.time.DateUtils
?
調用示例
Date date = DateUtils.parseDate(strDate, "yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy/MM/dd","yyyy/MM/dd HH:mm:ss");
?
方法說明
/*** <p>Parses a string representing a date by trying a variety of different parsers.</p>** <p>The parse will try each parse pattern in turn.* A parse is only deemed successful if it parses the whole of the input string.* If no parse patterns match, a ParseException is thrown.</p>* The parser will be lenient toward the parsed date.** @param str the date to parse, not null* @param parsePatterns the date format patterns to use, see SimpleDateFormat, not null* @return the parsed date* @throws IllegalArgumentException if the date string or pattern array is null* @throws ParseException if none of the date patterns were suitable (or there were none)*/public static Date parseDate(final String str, final String... parsePatterns) throws ParseException {return parseDate(str, null, parsePatterns);}第一個參數是時間字符串
后面的參數都是時間格式字符串,可以添加多個
總結
以上是生活随笔為你收集整理的多种时间格式字符串转换为Date对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS数字转中文
- 下一篇: 【洛谷P2680】运输计划