Java String to Date stringToDate(String date, String pattren)

Here you can find the source of stringToDate(String date, String pattren)

Description

string To Date

License

Open Source License

Declaration

public static Date stringToDate(String date, String pattren) 

Method Source Code

//package com.java2s;
/**//  w  ww.j a v a  2 s  .c o m
 *   Copyright ? 2013 Aftab Mahmood
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   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 <http://www.gnu.org/licenses/>.
 **/

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

public class Main {
    public static Date stringToDate(String date, String pattren) {
        if (date == null || date.trim().isEmpty())
            return null;

        if (pattren != null && !pattren.trim().isEmpty()) {
            DateFormat formater = new SimpleDateFormat(pattren);
            try {
                return formater.parse(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    public static Date stringToDate(String date, String time, String pattren) throws ParseException {
        if (time == null || time.trim().isEmpty())
            time = "00:00:00,000";

        return stringToDate(date + " " + time, pattren);

    }
}

Related

  1. stringToDate(String date)
  2. stringToDate(String date)
  3. stringToDate(String date)
  4. stringToDate(String date)
  5. stringToDate(String date, String format)
  6. stringToDate(String datecontent, String format)
  7. stringToDate(String dateStr)
  8. stringToDate(String dateStr)
  9. StringToDate(String dateStr)