converting timestamp to date in java

后端 未结 6 1084
既然无缘
既然无缘 2020-12-21 08:14

This is my database: \"enter

Here I have to check the query current date+status=Q info

6条回答
  •  情歌与酒
    2020-12-21 09:05

    Looks like the integer value you are seeing is the UNIX's number of seconds since the start of the Epoch ( which is 1970-01-01T00:00:00Z ) -> http://en.wikipedia.org/wiki/Unix_epoch

    I don't know if the mysql JDBC driver will take care of converting this field for you. If it does, the preferred way of getting its value is:

    java.sql.Timestamp ts = result.getTimestamp( columnNumber );
    

    Timestamp extends java.util.Date, so you can use it where regular java.util.Date is expected.

    If, for whatever reason this does not produce desired result, then get the column value as long and adjust the value to milliseconds. You are in luck here because Java's and UNIX's epochs start at the same time ( Java's is just more precise ).

    long ts = result.getLong( columnNumber ) * 1000L;
    
    java.util.Date date = new java.util.Date( ts );
    

提交回复
热议问题