Here you can find the source of isDateTimeString(String dirtyDateTime)
Parameter | Description |
---|
public static boolean isDateTimeString(String dirtyDateTime)
//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; } }