Java Parse Date parseDate(String dateString)

Here you can find the source of parseDate(String dateString)

Description

Method parseDate.

License

Open Source License

Parameter

Parameter Description
dateString a parameter

Return

Date

Declaration

public static Date parseDate(String dateString) 

Method Source Code

//package com.java2s;

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

import java.util.Date;

public class Main {
    /**/*  w ww  .jav  a  2s  . c o  m*/
     * Method parseDate. Return java.util.Date for a given MM/DD/YYYY format string date.
     * 
     * @param dateString
     * @return Date
     */
    public static Date parseDate(String dateString) {
        // Null conditions
        if (dateString == null)
            return null;
        if (dateString.length() == 0)
            return null;

        // Initialize to null
        Date javaDate = null;

        // If there is a date
        dateString = dateString.trim();
        if (dateString.length() <= 10) // Just date
        {
            try {
                javaDate = new SimpleDateFormat("MM/dd/yyyy").parse(dateString);
            } catch (ParseException pe) {
            }
        }

        else if (dateString.length() > 10) // Date + time
        {
            // Try a couple of different formats for date/time
            try {
                javaDate = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa").parse(dateString);
            } catch (ParseException pe) {
            }
            if (javaDate == null) {
                try {
                    javaDate = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa").parse(dateString);
                } catch (ParseException pe) {
                }
            }
        }

        return javaDate;
    }
}

Related

  1. ParseDate(String dateString)
  2. parseDate(String dateString)
  3. parseDate(String dateString)
  4. parseDate(String dateString)
  5. parseDate(String dateString)
  6. parseDate(String dateString)
  7. parseDate(String dateString)
  8. parseDate(String dateString)
  9. parseDate(String dateString)