Java Data Type How to - Change from strings of numbers to a string of a date








Question

We would like to know how to change from strings of numbers to a string of a date.

Answer

//w w w.  j ava 2s .  co m
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        String maxDate = "15012009";
        SimpleDateFormat fromFormat = new SimpleDateFormat("ddMMyyyy");
        SimpleDateFormat toFormat = new SimpleDateFormat("MMMM dd, yyyy");
        Date date = null;
        try {
            date = fromFormat.parse(maxDate);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        System.out.println("formated date:-" + toFormat.format(date));
    }
}

The code above generates the following result.