2011-05-05 133 views
1

我的应用程序需要像这样的“2002-10-15 10:55:01.000000”字符串。我需要验证该字符串对于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”这将通过验证。我假设,由于第一个毫秒字符是有效的,它只是截断了字符串的其余部分。

回答

2

我并不完全确定的格式,但你可以围绕它玩,可以尝试这样的事情

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; 
    } 
} 

编辑:如果你想验证成功后,解析数字,你可以做

 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; 
+0

我相信它抛出ParseException没有出现FormatException – JustinKSU 2011-05-05 18:21:02

+0

看到我的编辑以上 – Bill 2011-05-06 13:57:43

+0

@Bill你是正确的,貌似还有一些截断在解析回事(),但你可以使用正则表达式来验证电话后位解析()。 – 2011-05-06 14:33:52

0

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

VALUES CAST(? AS TIMESTAMP) 

上述查询将全面验证输入字符串,而几乎不会消耗数据库服务器上的任何资源。如果字符串由于任何原因而无效,那么您的数据库客户端将遇到异常。

1
/** 
* 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; 
    } 

}