Java String to Date stringToDate(String date)

Here you can find the source of stringToDate(String date)

Description

string To Date

License

Apache License

Declaration

public static final Date stringToDate(String date) 

Method Source Code


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

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

import java.util.Date;

public class Main {

    public static final Date stringToDate(String date) {
        if (date == null) {
            return null;
        }//from   w w w . jav a2s. com
        String separator = String.valueOf(date.charAt(4));
        String pattern = "yyyyMMdd";
        if (!separator.matches("\\d*")) {
            pattern = "yyyy" + separator + "MM" + separator + "dd";
            if (date.length() < 10) {
                pattern = "yyyy" + separator + "M" + separator + "d";
            }
        } else if (date.length() < 8) {
            pattern = "yyyyMd";
        }
        pattern += " HH:mm:ss.SSS";
        pattern = pattern.substring(0, Math.min(pattern.length(), date.length()));
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. stringToDate(String date)
  2. stringToDate(String date)
  3. stringToDate(String date)
  4. StringToDate(String date)
  5. stringToDate(String date)
  6. stringToDate(String date, String format)
  7. stringToDate(String date, String pattren)
  8. stringToDate(String datecontent, String format)
  9. stringToDate(String dateStr)