0

So Basically I have a table in MySql DB, which when a user upload something it register the item with an automated timestamp. Now in my client side I need to show the user how long has it been from item upload. Similar like youtube comments or Something like:

Upload Time | MySql TimeStamp: 2017-03-24 22:13:03 Current time: 2017-03-24 22:14:03 The result for has to be 1 hour.

I would really appreciate if someone tell how to do it automatically. I'm using Java, MySql which ever is easy.

Java:

String date = null,item="auniqueid";
String query = "Select date from table where item=?";
try {
        DBConnect Database = new DBConnect();
        Connection con = Database.getcon();
        PreparedStatement ps = con.prepareStatement(query);
        ps.setString(1, item);
        ResultSet rs=ps.executeQuery();
        if(rs.next()){
            date=rs.getString(1);
        }
        ps.close();
        rs.close();
        con.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

Regards

1 Answers1

0

In java date.getTime() returns milliseconds. Convert your both current and stored time stamp and calculate the difference.

Reference Link

TO calculate the duration, best way to convert both time stamp into seconds.

JAVA 8 (Easy)
You can use the new Java 8 time API (if you are able to use Java 8):

LocalTime now = LocalTime.now();
LocalTime previous = LocalTime.of(0, 0, 0, 0);
Duration duration = Duration.between(previous, now);

System.out.println(now);
System.out.println(previous);
System.out.println(duration);

Reference

Community
  • 1
  • 1
BetaDev
  • 4,516
  • 3
  • 21
  • 47