Android Date String Parse ParseStringToDate(String date)

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

Description

Parse String To Date

Declaration

public static Date ParseStringToDate(String date) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static Date ParseStringToDate(String date) {
        Date result = null;/*from w  ww .  j a v  a  2s  . c om*/

        if (date != null && !date.equals("")) {
            String parse = date;
            parse = parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1");
            parse = parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)",
                    "$1MM$2");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2");
            parse = parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)", "$1HH$2");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)",
                    "$1mm$2");
            parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)",
                    "$1ss$2");

            SimpleDateFormat format = new SimpleDateFormat(parse);
            try {
                result = format.parse(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}

Related

  1. ISO8601ToTimestamp(String aDate)
  2. getDateByTime(String time)
  3. isDateString(String dirtyDate)
  4. isDateTimeString(String dirtyDateTime)
  5. parse(String date)