Java Data Type How to - Convert String from ISO Compliant Date format to another format








Question

We would like to know how to convert String from ISO Compliant Date format to another format.

Answer

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//from   w  w  w. ja v a 2s  .co  m
public class Main {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd-'T'HH:mm:ss'Z'");
        Date inDate = inFormat.parse("2015-01-11-T00:00:00Z");

        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMdd");
        String output = outFormat.format(inDate);

        System.out.println("Date: " + output);

    }
}

The code above generates the following result.