Android Date String Parse isDateTimeString(String dirtyDateTime)

Here you can find the source of isDateTimeString(String dirtyDateTime)

Description

is Date Time String

Parameter

Parameter Description

Return

: true - if dirtyDate is formatted as "yyyy-MM-dd" or "yyyy-MM-ss HH:mm:ss"; false else

Declaration

public static boolean isDateTimeString(String dirtyDateTime) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w w  w .  j  a  v  a  2 s. c  o m*/
     * @Title : isDateTimeString
     * @param : dirtyDateTime
     * @return : true - if dirtyDate is formatted as "yyyy-MM-dd" or
     *         "yyyy-MM-ss HH:mm:ss"; false else
     */
    public static boolean isDateTimeString(String dirtyDateTime) {
        int len = dirtyDateTime.length();
        if (len != 10 && len != 19)
            return false;
        if (!isDateString(dirtyDateTime.substring(0, 10)))
            return false;
        if (len == 10)
            return true;
        if (dirtyDateTime.charAt(10) != ' ')
            return false;
        String timeString = dirtyDateTime.substring(11);
        len = timeString.length();
        if (len == 8) {
            for (int i = 0; i < len; i++) {
                if (i == 2 || i == 5) {
                    if (timeString.charAt(i) != ':')
                        return false;
                } else {
                    if (!Character.isDigit(timeString.charAt(i)))
                        return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }

    /**
     * @Title : isDateString
     * @param : dirtyDate
     * @return : true - if dirtyDate is formatted as "yyyy-MM-dd"; false else
     */
    public static boolean isDateString(String dirtyDate) {
        int len = dirtyDate.length();
        if (len == 10) {
            for (int i = 0; i < len; i++) {
                if (i == 4 || i == 7) {
                    if (dirtyDate.charAt(i) != '-')
                        return false;
                } else {
                    if (!Character.isDigit(dirtyDate.charAt(i)))
                        return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }
}

Related

  1. ISO8601ToTimestamp(String aDate)
  2. ParseStringToDate(String date)
  3. getDateByTime(String time)
  4. isDateString(String dirtyDate)
  5. parse(String date)
  6. parse(String date, String format)
  7. parse(String date, boolean isDateOnly)
  8. parse(String dateString, String dateFormat)