字符串日期格式化,输出指定格式的Date类型,很多博客都写的词不达意,没有找到想要的,遂动手。
直接上代码:
```java
/**
* @projectName galaxyFlink
* @package com.aito.flink.app
* @className com.aito.flink.app.DataUtils
* @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
*/
package com.aito.flink.app;
import org.apache.commons.lang3.time.FastDateFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
/**
* DataUtils
*
* @author Lee.
* @version 1.0
* @description 时间工具类
* @date 2021/5/22 18:28
*/
public class DateUtils {
public static void main(String[] args) throws ParseException {
String dateStr = "2021-05-17 22:50:02";
Date resDate = strToDateyyyMMdd(dateStr);
System.out.println(resDate);
}
public static Date strToDateyyyMMdd(String strDate) throws ParseException {
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
Date nowDate = fdf.parse(strDate);
LocalDate localDate = nowDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return java.sql.Date.valueOf(localDate);
}
}
效果输出: