String類型的xxxx年x月xx日與LocalDateTime類型的 yyyy-mm-dd相互轉(zhuǎn)化
1.導(dǎo)入DataUtil工具類
public class DataUtil {
/**
* 將String類型的xxxx年x月xx日 ——> LocalDateTime yyyy-mm-dd
* @param strData
* @return
* @throws
*/
public static LocalDateTime sTd(String strData) throws ParseException {
// String strData = "2023年5月12日";
Date sData = new SimpleDateFormat("yyyy年MM月dd日").parse(strData);
LocalDateTime localDateTime = sData.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
return localDateTime;
}
/**
* 將LocalDateTime yyyy-mm-dd ——> String類型的xxxx年x月xx日
* @param localDateTime
* @return
*/
public static String dTs(LocalDateTime localDateTime){
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年M月dd日");
String strData = localDateTime.format(pattern);
return strData;
}
}
2.調(diào)用工具類
@Test
public void dataTest() throws ParseException {
LocalDateTime dateTime = DataUtil.sTd("2023年5月12日");
System.out.println(dateTime);
String strData = DataUtil.dTs(dateTime);
System.out.println(strData);
}
3.輸出結(jié)果文章來源:http://www.zghlxwxcb.cn/news/detail-849991.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-849991.html
到了這里,關(guān)于將前端傳過來的String的日期數(shù)據(jù)轉(zhuǎn)換成LocalDateTime類型的數(shù)據(jù)存入數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!