将字符串转为日期格式yyyy-MM-dd||自定义格式
public static String strToDateFormat(String date, String dateFormat, String tarFormat) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(StringUtils.isBlank(dateFormat) ? "yyyyMMdd" : dateFormat);
formatter.setLenient(false);
Date newDate = formatter.parse(date);
formatter = new SimpleDateFormat(StringUtils.isBlank(tarFormat) ? "yyyy-MM-dd" : tarFormat);
return formatter.format(newDate);
}
public static String dateFormat(String ofPattern) {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(ofPattern));
}
public static void main(String[] args) throws ParseException {
System.out.println(dateFormat("yyyy-MM-dd HH:mm:ss"));
System.out.println(strToDateFormat("20111205", "yyyyMMdd", "yyyy-MM-dd"));
System.out.println(strToDateFormat("20121205", "", ""));
System.out.println(strToDateFormat("2013-12-05", "yyyy-MM-dd", "yyyy/MM/dd"));
System.out.println(strToDateFormat("2014-1205", "yyyy-MMdd", "yyyyMM/dd"));
System.out.println(strToDateFormat("2015-12-05", "yyyy-MM-dd", "yyyy/MM/dd HH:mm:ss"));
System.out.println(strToDateFormat("2016/12/05", "yyyy/MM/dd", "yyyy-MM-dd"));
}