Java date format example 

Joined:
04/09/2007
Posts:
753

February 01, 2010 23:56:25    Last update: February 01, 2010 23:56:25
This simple class demonstrates the use of java.text.SimpleDateFormat. The strict option can be used to validate a date string. The lenient option takes all kinds of junk and is not suitable for validating date strings.

import java.util.*;
import java.text.*;

public class TestDateParser {
    private static String[][] testCases = {
                            // strict parsing
                            {"MM/dd/yyyy", "11/2/2009", "false"}, 
                            {"MM/dd/yyyy", "11/2/2009 11:23:00", "false"},
                            {"MM/dd/yyyy HH:mm:ss", "11/12/2009 11:98:66", "false"},
                            {"MM/dd/yyyy", "15/2/2009 11:23:00", "false"},
                            {"MM/dd/yyyy", "11/32/2009 11:23:00", "false"},
                            {"MM/dd/yyyy", "11/0/2009 11:23:00", "false"},
                            {"MM/dd/yyyy", "11//2009 11:23:00", "false"},
                            {"MM/dd/yyyy", "11/2009 11:23:00", "false"},

                            // lenient parsing
                            {"MM/dd/yyyy", "11/2/2009", "true"}, 
                            {"MM/dd/yyyy", "11/2/2009 11:23:00", "true"},
                            {"MM/dd/yyyy HH:mm:ss", "11/12/2009 11:98:66", "true"},
                            {"MM/dd/yyyy", "15/2/2009 11:23:00", "true"},
                            {"MM/dd/yyyy", "11/32/2009 11:23:00", "true"},
                            {"MM/dd/yyyy", "11/0/2009 11:23:00", "true"},
                            {"MM/dd/yyyy", "11/-1/2009 11:23:00", "true"},
                            {"MM/dd/yyyy", "11//2009 11:23:00", "true"},
                            {"MM/dd/yyyy", "11/2009 11:23:00", "true"}
                         };

    private static DateFormat US_FORMAT = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    public static void main(String[] args) {
        for (int i = 0; i < testCases.length; i++) {
            String format = testCases[i][0];
            String dateStr = testCases[i][1];
            boolean lenient = Boolean.parseBoolean(testCases[i][2]);
            try {
                Date d = getDate(format, dateStr, lenient);
                System.out.println("\"" + dateStr + "\" \t(" + format
                                   + (lenient ? " Lenient" : " Strict")
                                   + ") is: " + US_FORMAT.format(d));
            }
            catch (Exception e) {
                System.out.println("\"" + dateStr + "\" \t(" + format
                                   + (lenient ? " Lenient" : " Strict")
                                   + "): cannot be parsed!");
            }
        }
    }

    public static Date getDate(String format,
                               String dateStr, 
                               boolean lenient) throws ParseException {
        DateFormat f =  new SimpleDateFormat(format);
        f.setLenient(lenient);
        Date valDate = f.parse(dateStr);
        return valDate;
    }
}
Share |
| Comment  | Tags