Java Date Parse convertStringToDate(String str_date)

Here you can find the source of convertStringToDate(String str_date)

Description

Convert string to date.

License

Apache License

Parameter

Parameter Description
str_date the str_date

Return

the date

Declaration

public static Date convertStringToDate(String str_date) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**//from  www . j a  v a 2 s.co m
     * Convert string to date.
     * 
     * @param str_date
     *            the str_date
     * @return the date
     */
    public static Date convertStringToDate(String str_date) {
        if (str_date != null && str_date.length() == 19)
            return convertStringToDateWithTime(str_date);

        Date dt = null;
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        try {
            if (str_date != null) {
                dt = (Date) formatter.parse(str_date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * Convert string to date with time.
     * 
     * @param str_date
     *            the str_date
     * @return the date
     */
    public static Date convertStringToDateWithTime(String str_date) {
        Date dt = null;
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
        try {
            if (str_date != null) {
                dt = (Date) formatter.parse(str_date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dt;
    }
}

Related

  1. convertStringToDate(String dt)
  2. convertStringToDate(String input)
  3. convertStringToDate(String s)
  4. convertStringToDate(String str)
  5. convertStringToDate(String str, String pattern)
  6. convertStringToDate(String strDate)
  7. convertStringToDate(String strDate, String parseFormat)
  8. convertStringToDate(String stringDate, String pattern)
  9. convertStringToDate(String value)