Java String to Date stringToDate(String dateString)

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

Description

string To Date

License

Open Source License

Declaration

public static Date stringToDate(String dateString) 

Method Source Code

//package com.java2s;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class Main {

    public static Date stringToDate(String dateString) {
        if (dateString == null || dateString.trim().length() == 0)
            return null;
        String datestr = dateString.trim();

        String sf = "yyyy-MM-dd HH:mm:ss";
        Date dt = stringToDate(datestr, sf);
        if (dt == null)
            dt = stringToDate(datestr, "yyyy-MM-dd");
        if (dt == null)
            dt = stringToDate(datestr, "yyyyMMdd");
        return dt;
    }/*  w ww  .  j a v a 2  s  . c  o  m*/

    public static Date stringToDate(String dateString, String sf) {
        ParsePosition pos = new ParsePosition(0);
        SimpleDateFormat sdf = new SimpleDateFormat(sf);
        Date dt = sdf.parse(dateString, pos);
        return dt;
    }
}

Related

  1. stringToDate(String dateStr)
  2. StringToDate(String dateStr)
  3. stringToDate(String dateStr)
  4. stringToDate(String dateStr, SimpleDateFormat dateFormat)
  5. StringtoDate(String dateStr, String format)
  6. stringToDate(String dateString)
  7. stringToDate(String dateString)
  8. stringToDate(String dateString)
  9. stringToDate(String dateString)