Java String to Date stringToDate(String strDate, String strFormat)

Here you can find the source of stringToDate(String strDate, String strFormat)

Description

This method parses the given string to the given date format

License

Open Source License

Parameter

Parameter Description
strDate Date as a String object
strFormat The format in which the String needs to be parsed

Return

Date Date

Declaration

public static Date stringToDate(String strDate, String strFormat)
        throws Exception 

Method Source Code

//package com.java2s;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**//from w ww.ja  va2 s. c  o m
     *  This method parses the given string to the given date format
     *  @param strDate Date as a String object
     *  @param strFormat The format in which the String needs to be parsed
     *  @return Date Date
     *  @exception Exception
     */
    public static Date stringToDate(String strDate, String strFormat)
            throws Exception {
        try {

            if (strDate == null || strDate.trim().equals(""))
                return null;

            String strDtFormat = "";

            strDtFormat = strFormat.toLowerCase();
            strDtFormat = replaceString(strDtFormat, "m", "M");
            if (strDtFormat.indexOf("hh:MM:ss") > 0)
                strDtFormat = replaceString(strDtFormat, "hh:MM:ss",
                        "HH:mm:ss");
            SimpleDateFormat formatter = new SimpleDateFormat(strDtFormat);
            return formatter.parse(strDate);

        } catch (Exception ex) {
            throw ex;
        }
    }

    /**
     * Trim input string.
     * @param sInput Input string.
     * @return       Trim the string if it is not null.
     */
    public static String trim(String sInput) {

        if (sInput != null) {
            return sInput.trim();
        } else {
            return sInput;
        }

    }

    /**
     *  This method replaces all occurance of a substring with the given substring in a string
     *  @param strString The parent string
     *  @param strSrchString The substring which needs to be replaced
     *  @param strRplString The new substring which will replace all occurences of strsrchString
     *  @return String Parent string with all occurences of strsrchString replaced by strRplString
     */
    public static String replaceString(String strString,
            String strSrchString, String strRplString) {

        if (strString == null)
            return "";

        String strOutString = "";
        int intIndex = 0;
        int intPrevIndex = 0;
        int intSrcStrLength = strSrchString.length();

        do {
            intIndex = strString.indexOf(strSrchString, intPrevIndex);

            if (intIndex == -1) {
                strOutString += strString.substring(intPrevIndex);
                break;
            }

            strOutString += strString.substring(intPrevIndex, intIndex)
                    + strRplString;
            intPrevIndex = intIndex + intSrcStrLength;

        } while (true);

        return strOutString;
    }
}

Related

  1. stringToDate(String strDate)
  2. stringToDate(String strDate)
  3. stringToDate(String strDate, String formartStr)
  4. stringToDate(String strDate, String oracleFormat)
  5. stringToDate(String strDate, String pattern)
  6. stringToDate(String string)
  7. stringToDate(String string, String format)
  8. stringToDate(String text)
  9. StringToDate(String thisdate, Locale locale)