6

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;


}
SRY_JAVA
  • 323
  • 3
  • 10
  • 21
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Basil Bourque Nov 12 '14 at 15:49
  • Please search StackOverflow before posting. Your Question's topics have been addressed in hundreds of questions and answers. Tips: (1) when serializing a date-time value to a string, use the standard ISO 8601 format. (2) For a database, use a date-time data type rather than strings. Probably TIMESTAMP WITH TIME ZONE. Databases such as Postgres support an array of such values. – Basil Bourque Nov 12 '14 at 15:50

4 Answers4

8

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

Ishkafel
  • 333
  • 1
  • 10
4

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:

Johny
  • 2,128
  • 3
  • 20
  • 33
  • I don't have to pass date and time as it is but have to pass a string array which holds my column values.how to do that.I have edited my question.How can i pass atime which is string array with values of my column.Moreover what is str1 in your line of code. – SRY_JAVA Nov 12 '14 at 11:13
  • how to split an array of string type which contains combination of date and time into time part only.Is there any way to do so. – SRY_JAVA Nov 12 '14 at 11:49
  • @SRY_JAVA In what form is your array can you give an example – Johny Nov 12 '14 at 12:53
  • my array atime is of string type which holds around 3600 values of the form 2014-09-01 10:00:00.000 – SRY_JAVA Nov 13 '14 at 04:16
2

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

        }

    }
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
0

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

        }
SRY_JAVA
  • 323
  • 3
  • 10
  • 21