SRY_JAVA
SRY_JAVA

Reputation: 323

How to display only time part from date time combination in java

I am new to Java. I am retrieving my first column from database in a String Array which represents data as:

2014-09-01 10:00:00.000

Now I want to display only time as:

10:00:00

How to do it? My code to retrieve my Column is:

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;


}

Upvotes: 6

Views: 44812

Answers (4)

SRY_JAVA
SRY_JAVA

Reputation: 323

I got it,just use substring() as

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

        }

Upvotes: 0

Johny
Johny

Reputation: 2188

If I have understood the question correctly 2014-09-01 10:00:00.000 is in a string and 10:00:00 has to be extracted from that. Given below is my solution.

  • First split the string with space as the delimiter.
  • Then from that take the second part of the string.
  • After that split the string again using . as the delimiter and take the first sting from that.

The above things are done using the line.

str.split("\\s")[1].split("\\.")[0];

COMPLETE CODE

String str = new String("2014-09-01 10:00:00.000");
String time = str.split("\\s")[1].split("\\.")[0];
System.out.print(time);

OUTPUT

10:00:00

For more details check the links given below:

Upvotes: 4

Philipp
Philipp

Reputation: 333

Use SimpleDateFormat:

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

If you have the date as a String, you can parse it to a java.util.Date in a step before:

SimpleDateFormat sdf = new SimpleDateFormat("YOUR_DATE_PATTERN");
Date date = sdf.parse(string);

Use patterns according to https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Upvotes: 8

Sagar Pudi
Sagar Pudi

Reputation: 4814

Refer official Java docs here

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;


    public class MyDate {

        public static void main(String[] args){
            DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
            Date date = new Date();
            String time=dateFormat.format(date);
            System.out.println(time);

        }

    }

Upvotes: 2

Related Questions