Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

time

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;


}
like image 449
SRY_JAVA Avatar asked Nov 12 '14 10:11

SRY_JAVA


People also ask

How do you get only time from ZonedDateTime?

now() now() method of a ZonedDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return ZonedDateTime based on system clock with default time-zone to obtain the current date-time.

How do I get only time from LocaldateTime?

You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.

What does date () getTime () do in Java?

The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object.


3 Answers

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

like image 120
Ishkafel Avatar answered Nov 03 '22 21:11

Ishkafel


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:

  • String.split().
  • Patterns
like image 39
Johny Avatar answered Nov 03 '22 20:11

Johny


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

        }

    }
like image 40
Sagar Pudi Avatar answered Nov 03 '22 19:11

Sagar Pudi