Java Parse Date Pattern YYYY parse(String dateString)

Here you can find the source of parse(String dateString)

Description

parse

License

Open Source License

Declaration

public static Date parse(String dateString) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.regex.Pattern;

public class Main {
    public static Date parse(String dateString) {
        try {//from w w  w  . jav a  2 s .  c  om
            if (dateString == null || "".equals(dateString)) {
                return null;
            }
            SimpleDateFormat format = new SimpleDateFormat();
            format.setLenient(false);
            if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}.\\d{1,3}", dateString)) {
                format.applyPattern("yyyy-MM-dd HH:mm:ss.SSS");
            } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}", dateString)) {
                format.applyPattern("yyyy-MM-dd HH:mm:ss");
            } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}", dateString)) {
                format.applyPattern("yyyy-MM-dd HH:mm");
            } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}", dateString)) {
                format.applyPattern("yyyy-MM-dd HH");
            } else if (Pattern.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}", dateString)) {
                format.applyPattern("yyyy-MM-dd");
            } else if (Pattern.matches("\\d{4}\\-\\d{1,2}", dateString)) {
                format.applyPattern("yyyy-MM");
            } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}.\\d{1,3}",
                    dateString)) {
                format.applyPattern("yyyy/MM/dd HH:mm:ss.SSS");
            } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}", dateString)) {
                format.applyPattern("yyyy/MM/dd HH:mm:ss");
            } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}", dateString)) {
                format.applyPattern("yyyy/MM/dd HH:mm");
            } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}", dateString)) {
                format.applyPattern("yyyy/MM/dd HH");
            } else if (Pattern.matches("\\d{4}\\/\\d{1,2}\\/\\d{1,2}", dateString)) {
                format.applyPattern("yyyy/MM/dd");
            } else if (Pattern.matches("\\d{4}\\/\\d{1,2}", dateString)) {
                format.applyPattern("yyyy/MM");
            } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}", dateString)) {
                format.applyPattern("yyyyMMdd HH:mm:ss");
            } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2} \\d{1,2}:\\d{1,2}", dateString)) {
                format.applyPattern("yyyyMMdd HH:mm");
            } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2} \\d{1,2}", dateString)) {
                format.applyPattern("yyyyMMdd HH");
            } else if (Pattern.matches("\\d{4}\\d{1,2}\\d{1,2}", dateString)) {
                format.applyPattern("yyyyMMdd");
            } else if (Pattern.matches("\\d{4}\\d{1,2}", dateString)) {
                format.applyPattern("yyyyMM");
            }
            return format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. parse(String dateString)
  2. parse(String dateString)
  3. parse(String dateString)
  4. parse(String dateString)
  5. parse(String DateString)
  6. parse(String dateString)
  7. parse(String datestring)
  8. parse(String dateString)
  9. parse(String dateString)