Java Parse Date parseDate(String datastr, String pattern)

Here you can find the source of parseDate(String datastr, String pattern)

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(String datastr, String pattern) 

Method Source Code


//package com.java2s;

import java.text.DateFormat;
import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static final String DEF_DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_FORMAT_1 = "yyyy/MM/dd";
    private static final String DATE_FORMAT_2 = "yyyyMMdd";
    private static final String DATE_FORMAT_3 = "MM/dd/yy";
    private static final String DATE_FORMAT_4 = "dd.MM.yy";
    private static final String DATE_FORMAT_5 = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT_6 = "yyyy-MM-dd HH:mm";
    private static final String DATE_FORMAT_7 = "yyyy:MM:dd HH:mm:ss";
    private static final String DATE_FORMAT_8 = "yyyy:MM:dd HH:mm";
    private static final String DATE_FORMAT_9 = "yyyyMMdd HHmmss";
    private static final String DATE_FORMAT_10 = "yyyyMMdd HHmm";
    private static final String DATE_FORMAT_11 = "dd.MM.yy HH:mm";
    private static final String DATE_FORMAT_12 = "MM/dd/yy hh:mm a";
    private static final String DATE_FORMAT_13 = "yyyy:MM:dd HH:mm:sss";
    private static final String DATE_FORMAT_14 = "HH:mm:ss dd.MM.yyyy";

    public static Date parseDate(String datastr, String pattern) {
        try {/* w  w  w . ja v a2 s.  c  o m*/
            DateFormat parser = new SimpleDateFormat(pattern);
            return parser.parse(datastr);
        } catch (ParseException ex) {
            return null;
        }
    }

    public static Date parseDate(String datestr) {
        if (datestr == null || datestr.trim().equals(""))
            return null;
        String[] patterns = new String[15];
        patterns[0] = DEF_DATE_FORMAT;
        patterns[1] = DATE_FORMAT_1;
        patterns[2] = DATE_FORMAT_2;
        patterns[3] = DATE_FORMAT_3;
        patterns[4] = DATE_FORMAT_4;
        patterns[5] = DATE_FORMAT_5;
        patterns[6] = DATE_FORMAT_6;
        patterns[7] = DATE_FORMAT_7;
        patterns[8] = DATE_FORMAT_8;
        patterns[9] = DATE_FORMAT_9;
        patterns[10] = DATE_FORMAT_10;
        patterns[11] = DATE_FORMAT_11;
        patterns[12] = DATE_FORMAT_12;
        patterns[13] = DATE_FORMAT_13;
        patterns[14] = DATE_FORMAT_14;
        for (int i = 0; i < patterns.length; i++) {
            try {
                DateFormat parser = new SimpleDateFormat(patterns[i]);
                return parser.parse(datestr);
            } catch (ParseException ex) {
            }
        }

        return null;
    }
}

Related

  1. parseDate(String certDate)
  2. parseDate(String currDate)
  3. parseDate(String d)
  4. parseDate(String d)
  5. parseDate(String d)
  6. parseDate(String date)
  7. parseDate(String date)
  8. parseDate(String date)
  9. parseDate(String date)