Java Data Type How to - Guess the format pattern based on date value


We would like to know how to guess the format pattern based on date value.


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*from   www .ja  v a  2s  .  c  om*/
public class Main {

  private static final String[] formats = { "yyyy-MM-dd'T'HH:mm:ss'Z'",
      "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss",
      "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
      "yyyy-MM-dd HH:mm:ss", "MM/dd/yyyy HH:mm:ss",
      "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'", "MM/dd/yyyy'T'HH:mm:ss.SSSZ",
      "MM/dd/yyyy'T'HH:mm:ss.SSS", "MM/dd/yyyy'T'HH:mm:ssZ",
      "MM/dd/yyyy'T'HH:mm:ss", "yyyy:MM:dd HH:mm:ss", "yyyyMMdd", };
  public static void main(String[] args) {
    String yyyyMMdd = "20110917";

  public static void parse(String d) {
    if (d != null) {
      for (String parse : formats) {
        SimpleDateFormat sdf = new SimpleDateFormat(parse);
        try {
          Date date = sdf.parse(d);
          System.out.println("Printing the value of " + parse);
        } catch (ParseException e) {


The code above generates the following result.