Java Date Parse convertStringToDate(String strDate)

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

Description

This method converts a String to a date using the datePattern

License

Open Source License

Parameter

Parameter Description
strDate the date to convert (in format MM/dd/yyyy)

Exception

Parameter Description
ParseException an exception

Return

a date object

Declaration

public static Date convertStringToDate(String strDate) throws ParseException 

Method Source Code


//package com.java2s;

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

import java.util.Date;

public class Main {
    private static String datePattern = "MM-dd-yyyy";

    /**/*from www .  j  a v  a2 s. c  o m*/
     * This method generates a string representation of a date/time
     * in the format you specify on input
     *
     * @param aMask the date pattern the string is in
     * @param strDate a string representation of a date
     * @return a converted Date object
     * @see java.text.SimpleDateFormat
     * @throws ParseException
     */
    public static final Date convertStringToDate(String aMask, String strDate) throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            return null;
        }

        return (date);
    }

    /**
     * This method converts a String to a date using the datePattern
     * @param strDate the date to convert (in format MM/dd/yyyy)
     * @return a date object
     * @throws ParseException
     */
    public static Date convertStringToDate(String strDate) throws ParseException {
        Date aDate = null;

        try {

            aDate = convertStringToDate(datePattern, strDate);
        } catch (ParseException pe) {
            //log.error("Could not convert '" + strDate
            //          + "' to a date, throwing exception");
            pe.printStackTrace();
            return null;

        }
        return aDate;
    }
}

Related

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