Java Date Parse convertStringToDate(String dateString, String dateFormat)

Here you can find the source of convertStringToDate(String dateString, String dateFormat)

Description

Method converts a String to Date, format of the String should be specified Example: dateString: 1999-10-15 dateFormat: yyyy-mm-dd

License

Open Source License

Parameter

Parameter Description
dateString a parameter
dateFormat a parameter

Declaration

public static Date convertStringToDate(String dateString, String dateFormat) 

Method Source Code

//package com.java2s;
/**/*from   w w  w  .j av a2  s.  c om*/
 *   theMovieDb-V3-API
 *   Copyright (C) 2012  Amith GC
 *   
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *   
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *   
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   @author Amith GC
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**
     * Method converts a String to Date, format of the String should be specified
     * Example: 
     * dateString: 1999-10-15 
     * dateFormat: yyyy-mm-dd
     * 
     * @param dateString
     * @param dateFormat
     * @return
     */
    public static Date convertStringToDate(String dateString, String dateFormat) {
        if (isStringBlank(dateString) || isStringBlank(dateFormat)) {
            return null;
        }

        DateFormat df = new SimpleDateFormat(dateFormat);
        try {
            Date dateObject = df.parse(dateString);
            return dateObject;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * Method to Validate the String
     * @param string
     * @return
     */
    public static boolean isStringBlank(String string) {
        if (string == null || string.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. convertStringToDate(String dateFormat, String date)
  2. convertStringToDate(String dateInput, String formatter)
  3. convertStringToDate(String dateStr, String format)
  4. convertStringToDate(String dateStr, String pattern)
  5. convertStringToDate(String dateString, String dateFormat)
  6. convertStringToDate(String dateString, String pattern)
  7. convertStringToDate(String dateText)
  8. convertStringToDate(String dateTime)
  9. convertStringToDate(String dateTime)