将字符串转为日期格式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"));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)