java 如何將多種字符串格式 解析為Date格式
將多種字符串格式 解析為Date格式
現(xiàn)在有多種日期格式,比如"2018/01/01"、"2018-01-01"、"2018 01 01"、"2018-01-01 12:12:12"、"2018年1月1日"
如何解析這些字符串呢?
之前也是被困擾了很長(zhǎng)時(shí)間,官方給我返回的時(shí)間格式為yyyyMMdd,我得解析成我想要的格式。
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateParse {
/**
* @param inputDate 要解析的字符串
* @param patterns 可能出現(xiàn)的日期格式
* @return 解析出來(lái)的日期,如果沒(méi)有匹配的返回null
*/
public static Date parseDate(String inputDate,String[] patterns){
SimpleDateFormat df = new SimpleDateFormat();
for(String pattern:patterns){
df.applyPattern(pattern);
df.setLenient(false);//設(shè)置解析日期格式是否嚴(yán)格解析日期
ParsePosition pos = new ParsePosition(0);
Date date = df.parse(inputDate, pos);
if(date!=null){
return date;
}
}
return null;
}
//驗(yàn)證結(jié)果
public static void main(String[] args) {
String[] possiblePatterns =
{
"yyyy-MM-dd",
"yyyy-MM-dd HH:mm:ss",
"yyyyMMdd",
"yyyy/MM/dd",
"yyyy年MM月dd日",
"yyyy MM dd"
};
String inputDate1 = "2018-01-01";
String inputDate2 = "2018-01-01 12:12:12";
String inputDate3 = "20180101";
String inputDate4 = "2018/01/01";
String inputDate5 = "2018年01月01日";
String inputDate6 = "2018 01 01";
System.out.println(parseDate(inputDate6,possiblePatterns));
System.out.println(parseDate(inputDate1,possiblePatterns));
System.out.println(parseDate(inputDate2,possiblePatterns));
System.out.println(parseDate(inputDate3,possiblePatterns));
System.out.println(parseDate(inputDate4,possiblePatterns));
System.out.println(parseDate(inputDate5,possiblePatterns));
System.out.println(parseDate(inputDate6,possiblePatterns));
}
}
還有一個(gè)更簡(jiǎn)便的方法:
org.apache.commons:commons-lang 包中有一個(gè)DateUtils類已經(jīng)實(shí)現(xiàn)了這個(gè)功能。
public static Date parseDate(String inputDate) {
Date outputDate = null;
String[] possibleDateFormats =
{
"yyyy-MM-dd",
"yyyyMMdd",
"yyyy/MM/dd",
"yyyy年MM月dd日",
"yyyy MM dd"
};
try {
outputDate = DateUtils.parseDate(inputDate, possibleDateFormats);
} catch (ParseException e) {
e.printStackTrace();
}
return outputDate;
}
Java String格式的標(biāo)準(zhǔn)時(shí)間字符串轉(zhuǎn)換為Date格式
場(chǎng)景
前端在往后端傳遞時(shí)間參數(shù)時(shí),傳遞的是標(biāo)準(zhǔn)時(shí)間格式的字符串。
比如下面的lxyf參數(shù)

怎樣將其轉(zhuǎn)換為Date格式。
實(shí)現(xiàn)
調(diào)用如下轉(zhuǎn)換格式的方法
Date lxyfDate = str2Date(lxyf);
方法的具體實(shí)現(xiàn)
public Date str2Date(String dateString) {
String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};
String SPLIT_STRING = "(中國(guó)標(biāo)準(zhǔn)時(shí)間)";
try {
dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);
SimpleDateFormat sf1 = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
Date date = sf1.parse(dateString);
return date;
} catch (Exception e) {
throw new RuntimeException("時(shí)間轉(zhuǎn)化格式錯(cuò)誤" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單的萬(wàn)年歷
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的萬(wàn)年歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
SpringBoot3通過(guò)GraalVM生成exe執(zhí)行文件問(wèn)題
文章介紹了如何安裝GraalVM和Visual Studio,并通過(guò)Spring Boot項(xiàng)目將Java應(yīng)用程序封裝成可執(zhí)行文件(.exe)2024-12-12
SpringBoot項(xiàng)目實(shí)現(xiàn)關(guān)閉數(shù)據(jù)庫(kù)配置和springSecurity
JPA默認(rèn)值設(shè)置沒(méi)有效果的解決
SpringBoot程序打包失敗(.jar中沒(méi)有主清單屬性)

