java sqlite 日期比较,TimeStamp Java和SQLite之间的区别

Hello I have and SLQLite database in which I have table water_logs

CREATE TABLE water_logs(

_id INTEGER PRIMARY KEY AUTOINCREMENT,

amount REAL NOT NULL,

icon INTEGER NOT NULL,

date INTEGER NOT NULL);

I store date in milliseconds.

Calendar cal = Calendar.getInstance();

cal.getTimeInMillis();

My problem is I want to get the day from the my date column using strftime function. The problem is tjat java calendar timestamp is different from SLQLite time stamp

1436859563832 --> result from cal.getTimeInMillis();

1436607407--> SELECT strftime('%s','now')

What I'm actually trying to do is to group records by day. The following SQL query works just fine if value of SELECT strftime('%s','now') is paste in the date column

SELECT SUM(amount), date(`date`) FROM water_logs

GROUP BY date(`date`, 'unixepoch')

vxSr8.png

ndnkL.png

CcXEm.png

解决方案

Seems to me that you are using 2 different value types.

When you use

Calendar cal = Calendar.getInstance();

long time = cal.getTimeInMillis();

The output value is in Milliseconds, as described here.

While when you use

strftime('%s','now')

The output value is in Seconds, as described here.

So, that might be the cause for the mismatch between the two values.

Of course that the value in seconds might undergo some rounding which might change its value a little.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值