如何validation时间戳?

我的应用程序采用像这样的字符串“2002-10-15 10:55:01.000000”。 我需要validation字符串是否对db2时间戳有效。

我怎样才能做到这一点?

编辑:这主要是有效的

public static boolean isTimeStampValid(String inputString) { SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS"); try { format.parse(inputString); return true; } catch (ParseException e) { return false; } } 

问题是,如果我将一个错误的格式传递给毫秒,如“2011-05-02 10:10:01.0av”,这将通过validation。 我假设,因为第一个毫秒字符有效,它只是截断字符串的其余部分。

我不完全确定格式,但你可以玩它并尝试这样的东西

 public static bool isTimeStampValid(String inputString) { SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS"); try{ format.parse(inputString); return true; } catch(ParseException e) { return false; } } 

编辑:如果你想在解析成功后validation数字,你可以这样做

  format.parse(inputString); Pattern p = Pattern.compile("^\\d{4}[-]?\\d{1,2}[-]?\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}[.]?\\d{1,6}$"); return p.matcher(inputString).matches(); 

代替

  format.parse(inputString); return true; 

http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

我相信格式是"yyyy-MM-dd HH:mm:ss.SSSSSS"

调用parse(String)并捕获ParseException,指示它无效。

 /** * This method validates the given time stamp in String format * @param timestamp * @return */ public static boolean isTimeStampValid(String timestamp) { //(Considering that formal will be yyyy-MM-dd HH:mm:ss.SSSSSS ) //Tokenize string and separate date and time boolean time = false; try { //Tokenize string and separate date and time StringTokenizer st = new StringTokenizer(timestamp, " "); if (st.countTokens() != 2) { return false; } String[] dateAndTime = new String[2]; int i = 0; while (st.hasMoreTokens()) { dateAndTime[i] = st.nextToken(); i++; } String timeToken = dateAndTime[1]; StringTokenizer timeTokens = new StringTokenizer(timeToken, ":"); if (timeTokens.countTokens() != 3) { return false; } String[] timeAt = new String[4]; int j = 0; while (timeTokens.hasMoreTokens()) { timeAt[j] = timeTokens.nextToken(); j++; } try { int HH = Integer.valueOf(timeAt[0].toString()); int mm = Integer.valueOf(timeAt[1].toString()); float ss = Float.valueOf(timeAt[2].toString()); if (HH < 60 && HH >= 0 && mm < 60 && mm >= 0 && ss < 60 && ss >= 0) { time = true; } else { } } catch (Exception e) { e.printStackTrace(); } //Got Date String dateToken = dateAndTime[0];//st.nextToken(); //Tokenize separated date and separate year-month-day StringTokenizer dateTokens = new StringTokenizer(dateToken, "-"); if (dateTokens.countTokens() != 3) { return false; } String[] tokenAt = new String[3]; //This will give token string array with year month and day value. int k = 0; while (dateTokens.hasMoreTokens()) { tokenAt[k] = dateTokens.nextToken(); k++; } //Now try to create new date with got value of date int dayInt = Integer.parseInt(tokenAt[2]); int monthInt = Integer.parseInt(tokenAt[1]); int yearInt = Integer.parseInt(tokenAt[0]); Calendar cal = new GregorianCalendar(); cal.setLenient(false); cal.set(yearInt, monthInt - 1, dayInt); cal.getTime();//If not able to create date it will throw error } catch (Exception e) { e.printStackTrace(); return false; } //Here we ll check for correct format is provided else it ll return false try { Pattern p = Pattern.compile("^\\d{4}[-]?\\d{1,2}[-]?\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}[.]?\\d{1,6}$"); if (p.matcher(timestamp).matches()) { } else { return false; } } catch (Exception e) { e.printStackTrace(); return false; } //Cross checking with simple date format to get correct time stamp only SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS"); try { format.parse(timestamp); //return true; if (time) { return true; } else { return false; } } catch (ParseException e) { e.printStackTrace(); return false; } catch (Exception e) { e.printStackTrace(); return false; } } 

如果您已连接到数据库,则可以执行尝试将输入字符串强制转换为时间戳的查询,并检查失败消息(在本例中为SQLSTATE 22007)。

 VALUES CAST( ? AS TIMESTAMP ) 

上述查询将完全validation输入字符串,同时几乎不消耗数据库服务器上的任何资源。 如果字符串因任何原因无效,则数据库客户端将遇到exception。