Java String to Date strToDate(String strDate)

Here you can find the source of strToDate(String strDate)

Description

str To Date

License

Apache License

Declaration

public static final Date strToDate(String strDate) 

Method Source Code


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

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String dtSimple = "yyyy-MM-dd";
    public static final String simpleFormat = "yyyy-MM-dd HH:mm";

    public static final Date strToDate(String strDate) {
        if (strToSimpleFormat(strDate) != null) {
            return strToSimpleFormat(strDate);
        } else {/*w  w  w .  j  a  v  a 2 s .  c  om*/
            return strToDtSimpleFormat(strDate);
        }

    }

    public static final Date strToSimpleFormat(String strDate) {
        if (strDate == null) {
            return null;
        }

        try {
            return getFormat(simpleFormat).parse(strDate);

        } catch (Exception e) {
        }

        return null;
    }

    public static final Date strToDtSimpleFormat(String strDate) {
        if (strDate == null) {
            return null;
        }

        try {
            return getFormat(dtSimple).parse(strDate);
        } catch (Exception e) {
        }

        return null;
    }

    public static final Date strToDtSimpleFormat(String strDate, String pattern) {
        if (strDate == null) {
            return null;
        }

        try {
            return getFormat(pattern).parse(strDate);
        } catch (Exception e) {
        }

        return null;
    }

    private static final DateFormat getFormat(String format) {
        return new SimpleDateFormat(format);
    }
}

Related

  1. strToDate(String str)
  2. strToDate(String str, String format, java.util.Date defaultValue)
  3. strToDate(String strD)
  4. strToDate(String strDate)
  5. strToDate(String strDate)
  6. strToDate(String strDate, String format)
  7. strToDate(String time, String format)
  8. strToDate3(String i_StrDate)
  9. strToDateM6(String StrToDate)