将yyyyMMdd转成yyyy-MM-dd格式和yyyy-MM-dd格式转成yyyyMMdd

yyyyMMdd转yyyy-MM-dd

yyyy-MM-dd转yyyyMMdd

获取其他格式互转 通用

适用于原格式和目标格式转换;如果性能更好一点的话可以提前定义静态类来处理

/**
 * 原格式日期转为目标格式日期
 * @param value
 * @param originalFormat
 * @param destFormat
 * @return
 * @throws ParseException
 */
public static String getFormatDate(String value, String originalFormat, String destFormat) throws ParseException {
    return new SimpleDateFormat(destFormat).format(new SimpleDateFormat(originalFormat).parse(value));
}
验证:
​​​​​​​public static void main(String[] args) throws Exception {
    String yyyyMMdd = getFormatDate("2022-03-22 18:45:45", "yyyy-MM-dd", "yyyyMMdd");
    System.out.println(yyyyMMdd);
}

输出:20220322


版权声明:本文为haohaounique原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。