Java convert date to date in other format

/**
     * Convert other date formats to custom date formats
     * @param dateStr The date to be converted (note that the date format must be the standard format of year, month and day)
     * @param format The format to be converted (if you don’t enter it, the default is yyyyMMdd)
     * @return output result
     */
    public static String dateFormatSwitch(String dateStr,String format){         String key = null;         String dateSwitch = null;         if(dateStr.length()==8){             key = "";         }else {             key = dateStr.substring(4,5);         }         if(format == null||"".equals(format)||format.isEmpty()){             format = "yyyyMMdd";         }         try {











            Date dateNow = new SimpleDateFormat("yyyy"+key+"MM"+key+"dd").parse(dateStr);
            dateSwitch = new SimpleDateFormat(format).format(dateNow);
        } catch (ParseException e) {
            System.out.println("日期格式错误");
        }
        return dateSwitch;
    }

Guess you like

Origin blog.csdn.net/u013804636/article/details/81353878