在我的 android 应用程序中,我使用 STRFTIME 函数通过比较不同的日期字符串从 SQLite 获取数据。
我将 Promotion StartDate
和 EndDate
分别设置为 2013-10-07
和 2013-12-31
.当前 DateTime 在 StartDate
和 EndDate
之间时存在促销。
以下是有效 strftime() 替换的完整列表:
%d day of month: 00
%f fractional seconds: SS.SSS
%H hour: 00-24
%j day of year: 001-366
%J Julian day number
%m month: 01-12
%M minute: 00-59
%s seconds since 1970-01-01
%S seconds: 00-59
%w day of week 0-6 with Sunday==0
%W week of year: 00-53
%Y year: 0000-9999=
然后我使用以下查询来检索数据。
Select * From Sal_Promotion p, Sal_Promotion_CustomerTypeAndProduct pd
Where p.Sal_PromotionID = d.Sal_PromotionID and
strftime('%s','now') >= strftime('%s',Sal_StartDate) and
strftime('%s','now') = strftime('%s',Sal_EndDate) and
Sal_CustomerTypeID = customerTypeID and
p.Sal_PromotionID = promoID;
当我在 2013 年 10 月 7 日早上 7 点测试我的应用程序时,我得到以下输出。
输出:
开始日期:2013-10-07 00:00:00(值:1381104000)
结束日期:2013-12-31 00:00:00(值:1388448000)
当前日期时间:2013-10-07 07:00:00(值:1381100400)
CurrentDateTime 值应介于 StartDate 和 EndDate 之间。但是为什么值是错误的呢?当前 DateTime 比 StartDate 晚 3600 秒(1 小时)。
我现在所在的城市是新加坡 (GMT+8:00)。是因为时差吗?
谁能指出我的问题是什么?
最佳答案
时区偏移量是您的问题。 SQLite3 日期和时间函数默认为 UTC。
一种解决方案是直接比较日期字符串
date('now') >= Sal_StartDate and date('now) <= Sal_EndDate
您还可以将 'localtime'
选项传递给 SQLite3 日期和时间函数。查看SQLite3 date and time functions documentation .
关于java - 使用 Strftime 当前 DateTime 是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19238027/