How to get Last 3 hours data from SQLite

前端 未结 3 1038
心在旅途
心在旅途 2021-01-15 01:52

I am inserting data in table with current system time like System.currentTimeMillis(). So, while getting data I need to take last 3 hours data only.

相关标签:
3条回答
  • 2021-01-15 02:31

    there could be other methods. but I think this will work for you.

     SELECT * FROM Table1 where timestamp >= (System.currentTimeMillis()-108000000)
    

    subtract 3 hours from current time and and search for time grater than that.

    0 讨论(0)
  • 2021-01-15 02:32

    Try this:

     SELECT * FROM Table1 where julianday(timestamp) >=  (julianday('now')-108000000)
    
    0 讨论(0)
  • 2021-01-15 02:38

    You can get TimeStamp of Time 3 Hours back using following

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, -3);
    Date threeHourBack = cal.getTime();
    

    Then you can pass threeHourBack.getTime() to the query.

    Or You can do this in SQLite

    SELECT * FROM Table1 where datetime(timestamp) >=datetime('now', '-3 Hour')
    
    0 讨论(0)
提交回复
热议问题