Java String to Date strToDatetime(String sStr)

Here you can find the source of strToDatetime(String sStr)

Description

str To Datetime

License

Apache License

Declaration

public static Date strToDatetime(String sStr) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static Date strToDatetime(String sStr) {
        if (sStr == null) {
            return null;
        }/*from  w w  w .j  a v  a2s  . c o m*/
        SimpleDateFormat formatter;
        if (sStr.length() == 19) {
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if (sStr.length() == 10) {
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if (sStr.length() == 8) {
            formatter = new SimpleDateFormat("yyyyMMdd");
        } else if (sStr.length() == 14) {
            formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        } else {
            formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        }
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(sStr, pos);
    }
}

Related

  1. strToDate(String strDate, String format)
  2. strToDate(String time, String format)
  3. strToDate3(String i_StrDate)
  4. strToDateM6(String StrToDate)
  5. StrToDateSeco(String str)
  6. strToDateWithFormat(String strDate, Format format)
  7. TimeStringToDate(String s)
  8. timeToDate(String time)
  9. toDate(DateFormat format, String value)