java - java中如何分割字符串数组

标签 java arrays split date-format

我有一个字符串类型的数组,它保存数据库中的列,该数据库保存日期和时间值,如 2014-11-12 16:08:48。现在我必须拆分数组,以便仅检索时间值,即 16:08:48。如何在数组上应用拆分?

提前致谢。

我检索列值的代码是

public String[] getChartTime() throws SQLException {
  List < String > timeStr = new ArrayList < String > ();
  String atime[] = null;
  getConnection();
  try {
    con = getConnection();


    String sql = "exec vcs_gauge @gauge_name=?,@first_rec_time=?,@last_rec_time=?";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("date is " + df.format(currentDate));
    clstmt = con.prepareCall(sql);
    clstmt.setString(1, "vs3_bag");
    clstmt.setString(2, "2014-09-01 10:00:00");
    clstmt.setString(3, "2014-09-01 11:00:00");
    clstmt.execute();
    rs = clstmt.getResultSet();

    while (rs.next()) {
      // Just get the value of the column, and add it to the list
      timeStr.add(rs.getString(1));

    }

  } catch (Exception e) {
    System.out.println("\nException in  Bean in getDbTable(String code):" + e);
  } finally {
    closeConnection();
  }
  // I would return the list here, but let's convert it to an array
  atime = timeStr.toArray(new String[timeStr.size()]);
  


  return atime;


}

现在我想拆分 timestr 数组,以便只获取时间,而不获取日期。

最佳答案

您可以简单地使用以下代码来完成此操作:

// Just get the value of the column, and add it to the list
timeStr.add(rs.getString(1).split(" ")[1]);

关于java - java中如何分割字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26886394/

相关文章:

java - 从 String[] 的 ArrayList 打印一个字符串?

javascript - React 中的条件状态

php - php 数组创建的意外输出

javascript - .split ("1px") 到 ["1px",1 ,"px"] 在 Javascript

php - post php mysql 的拆分关键字

java - 在另一个线程上有益地执行一件工作的最小尺寸?

java - 使用 jobjectarray 中的参数调用 NewObject 方法 jni

c++ - 拆分绝对文件路径

java - org.hibernate.exception.JDBCConnectionException : could not execute query using hibernate

java - 找不到 WebApplicationContext : no ContextLoaderListener registered?