Java String to Date StringToDate(String dateStr)

Here you can find the source of StringToDate(String dateStr)

Description

String To Date

License

Open Source License

Declaration

public static Date StringToDate(String dateStr) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static Date StringToDate(String dateStr) {
        SimpleDateFormat sdf = null;
        Date date = null;/*from   www  . j a  v  a 2 s  .  c o  m*/
        if (dateStr.length() > 10) {
            if (dateStr.indexOf("T") > -1) {
                dateStr = dateStr.replace("T", " ");
            }
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }
        try {
            date = sdf.parse(dateStr);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
            return null;
        }
        return date;
    }
}

Related

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