Java Date Parse asDate(String value, String format)

Here you can find the source of asDate(String value, String format)

Description

as Date

License

Open Source License

Declaration

public static Date asDate(String value, String format) 

Method Source Code

//package com.java2s;
/**/*w ww  .  ja  va 2 s .c  o m*/
 * jetbrick-template
 * http://subchen.github.io/jetbrick-template/
 *
 * Copyright 2010-2014 Guoqiang Chen. All rights reserved.
 * Email: subchen@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    private static final String[] DATE_PATTERNS = new String[] { "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss",
            "yyyy-MM-dd", "yyyy/MM/dd HH:mm:ss.SSS", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd", "HH:mm:ss",
            "yyyy-MM-dd'T'HH:mm:ss:SSSZZ", "EEE, dd MMM yyyy HH:mm:ss z", "EEE, dd MMM yyyy HH:mm z",
            "EEE, dd MMM yy HH:mm:ss z", "EEE, dd MMM yy HH:mm z", "dd MMM yyyy HH:mm:ss z", "dd MMM yyyy HH:mm z",
            "dd MMM yy HH:mm:ss z", "dd MMM yy HH:mm z", };

    public static Date asDate(String value) {
        value = (value != null) ? value.trim() : null;
        ParsePosition pp = null;
        Date d = null;
        for (int i = 0; d == null && i < DATE_PATTERNS.length; i++) {
            DateFormat df = new SimpleDateFormat(DATE_PATTERNS[i]);
            df.setLenient(false);
            try {
                pp = new ParsePosition(0);
                d = df.parse(value, pp);
                if (pp.getIndex() != value.length()) {
                    d = null;
                }
            } catch (Exception e) {
                // try next pattern
            }
        }
        return d;
    }

    public static Date asDate(String value, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        df.setLenient(false);
        try {
            ParsePosition pp = new ParsePosition(0);
            Date d = df.parse(value, pp);
            if (pp.getIndex() != value.length()) {
                d = null;
            }
            return d;
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. asDate(final String literal)
  2. asDate(String date)
  3. asDate(String param, SimpleDateFormat format)
  4. asDate(String s)
  5. asDate(String string)
  6. convertDateForExcel(String timestamp)
  7. convertDateFromString(String data)
  8. convertDateStringToDate(String dateString, String format)
  9. convertDateStringToDB(String date)