如何在java中只显示日期时间组合的时间部分

SRY*_*AVA 5 java time

我是Java新手.我在数据库中从数据库中检索我的第一列,表示数据为:

2014-09-01 10:00:00.000
Run Code Online (Sandbox Code Playgroud)

现在我想只显示时间:

10:00:00
Run Code Online (Sandbox Code Playgroud)

怎么做?我检索我的列的代码是:

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()]);
  for (String s: atime) {
    System.out.println(s);
  }

  return atime;


}
Run Code Online (Sandbox Code Playgroud)

Ish*_*fel 6

用途SimpleDateFormat:

java.util.Date date = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf.format(date));
Run Code Online (Sandbox Code Playgroud)

如果您将日期作为String,则可以在之前的步骤中将其解析为java.util.Date:

SimpleDateFormat sdf = new SimpleDateFormat("YOUR_DATE_PATTERN");
Date date = sdf.parse(string);
Run Code Online (Sandbox Code Playgroud)

根据https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html使用模式