Java Utililty Methods String to Date

List of utility methods to do String to Date

Description

The list of methods to do String to Date are organized into topic(s).

Method

DatestringToDate(String datecontent, String format)

Description:Get the date according to specific String content The default format is "yyyy-MM-dd"

if (format == null || format.equals(""))
    format = "yyyy-MM-dd";
try {
    SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
    Date date = bartDateFormat.parse(datecontent);
    return date;
} catch (ParseException pe) {
    @SuppressWarnings("unused")
...
DatestringToDate(String dateStr)
string To Date
try {
    if (dateStr != null)
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
} catch (Exception e) {
    e.printStackTrace();
    System.gc();
return null;
...
DateStringToDate(String dateStr)
String To Date
SimpleDateFormat sdf = null;
Date date = null;
if (dateStr.length() > 10) {
    if (dateStr.indexOf("T") > -1) {
        dateStr = dateStr.replace("T", " ");
    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else {
...
DatestringToDate(String dateStr)
Converts a String with format DateUtils.DATE_FORMAT to a Date.
if (dateStr == null)
    return null;
try {
    return DATE_FORMAT.parse(dateStr);
} catch (ParseException ex) {
    return null;
DatestringToDate(String dateStr, SimpleDateFormat dateFormat)
string To Date
Date date = dateFormat.parse(dateStr);
return date;
java.util.DateStringtoDate(String dateStr, String format)
Stringto Date
Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format);
try {
    formater.setLenient(false);
    d = formater.parse(dateStr);
} catch (Exception e) {
    d = null;
return d;
DatestringToDate(String dateString)
string To Date
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)
...
DatestringToDate(String dateString)
String to Date.
Date date = null;
LinkedList<SimpleDateFormat> formats = new LinkedList<>();
formats.add(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"));
formats.add(new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"));
formats.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
formats.add(new SimpleDateFormat("dd.MM.yyyy HH:mm"));
for (SimpleDateFormat sdf : formats) {
    try {
...
DatestringToDate(String dateString)
string To Date
try {
    return new SimpleDateFormat(DATE_STORAGE_FORMAT, Locale.ENGLISH).parse(dateString);
} catch (ParseException pex) {
    return new Date(0L);
DatestringToDate(String dateString)
string To Date
String sf = "yyyy-MM-dd HH:mm:ss";
Date dt = stringToDate(dateString, sf);
return dt;