java将日期转换为其他格式的日期

/**
     * 将其他日期格式转换为自定义日期格式
     * @param dateStr 需要转换的日期(注意日期格式必须为年月日的标准格式)
     * @param format 需要转换成的格式(如果不输默认为yyyyMMdd)
     * @return 输出结果
     */
    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;
    }

猜你喜欢

转载自blog.csdn.net/u013804636/article/details/81353878