Java Parse Date parseDate(String datestr)

Here you can find the source of parseDate(String datestr)

Description

parse Date

License

Apache License

Declaration

private static Date parseDate(String datestr) 

Method Source Code

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

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

public class Main {

    private static Date parseDate(String datestr) {
        if (null == datestr || "".equals(datestr)) {
            return null;
        }//from   ww  w .  j a  va 2s  .c o m
        try {
            String fmtstr = null;
            if (datestr.contains(":") && datestr.contains("-")) {
                fmtstr = "yyyy-MM-dd HH:mm:ss";
            } else if (datestr.contains(":") && datestr.contains("/")) {
                fmtstr = "yyyy/MM/dd HH:mm:ss";
            } else if (datestr.contains("-")) {
                fmtstr = "yyyy-MM-dd";
            } else if (datestr.contains("/")) {
                fmtstr = "yyyy/MM/dd";
            }

            if (fmtstr != null) {
                SimpleDateFormat sdf = new SimpleDateFormat(fmtstr, Locale.UK);
                return sdf.parse(datestr);
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. parseDate(String dateStr)
  2. parseDate(String dateStr)
  3. parseDate(String dateStr)
  4. parseDate(String dateStr)
  5. parseDate(String dateStr)
  6. parseDate(String dateStr, SimpleDateFormat... formats)
  7. parseDate(String dateStr, String dateFormat)
  8. parseDate(String dateStr, String dateFormat)
  9. parseDate(String dateStr, String format)