我们可以用Java创建自己的日期格式吗?

Pan*_*Pan 4 java

我需要将日期(yyyyMMDD)格式化为YYYY-MM-DD.我可以创建后者的日期格式吗?

Kev*_*n D 7

    SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
    String sourceDateStr = "20101012";
    try {
        Date sourceDate = sourceFormat.parse(sourceDateStr);

        String targetDateStr = targetFormat.format(sourceDate);

        System.out.println(targetDateStr);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

给出输出 2010-01-12