您应该在数据库中存储什么类型的日期?

发布于 2024-11-18 11:03:19 字数 596 浏览 1 评论 0原文

目前,我将其存储为字符串,但在使用 GQL 按日期查询时使用它时遇到问题。

Date date = Calendar.getInstance().getTime();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss, z");
String todayDate = formatter.format(date);

查询:

"SELECT FROM SomeTable p WHERE date = 01/01/2011"

错误: 例外:

org.datanucleus.store.appengine.query.DatastoreQuery$UnsupportedDatastoreFeatureException: 查询问题:表达式的右侧是 由不受支持的组件组成。 左边: org.datanucleus.query.expression.Literal, 操作: / ,右: DyadicExpression{文字{5} / 文字{11}}

如何按日期搜索?

Currently I'm storing it as a String, but got problems using it when it comes to querying by date with GQL.

Date date = Calendar.getInstance().getTime();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss, z");
String todayDate = formatter.format(date);

The query:

"SELECT FROM SomeTable p WHERE date = 01/01/2011"

Error:
Exception:

org.datanucleus.store.appengine.query.DatastoreQuery$UnsupportedDatastoreFeatureException:
Problem with query : Right side of expression is
composed of unsupported components.
Left:
org.datanucleus.query.expression.Literal,
Op: / , Right:
DyadicExpression{Literal{5} /
Literal{11}}

How can I search by date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-11-25 11:03:19

始终为日期 - 数据库应检查其存储的数据类型,从而确保数据如其所言。有关插入件上已知类型的信息,因此请立即检查,而不是稍后在没有人知道正确值应该是什么时进行检查。

您收到的错误可能不是由于此原因,而是因为如果类型正确或者字符串需要作为单引号字符串,则查询中的日期需要设为日期,请参阅 GQL 参考
例如

"SELECT FROM SomeTable p WHERE date = '01/01/2011'"

,但是 06/07/2011 是什么日期?我认为今天是7月6日,其他人是6月7日。因此,即使您使用字符串,使用 ISO 格式 2011-07-06 日期类型也会隐藏此细节,使其更容易。请注意,GQL 仅使用 ISO 形式,因此即使您获得将日期包含在字符串中的命令,它也会失败。

但更好的是

SELECT FROM SomeTable p WHERE date = DATE( 2011, 1, 1)

Always as a date - the database should check on the types of data that it stores thus making sure the data is what it says. the information about what type is known on the insert so check it then rather than later when none has any idea of what the correct value should have been.

the error you are getting is probably not due to this but because the date in the query needs to be made a date if types are correct or if a string needs to be as a single-quoted string see GQL Reference
e.g.

"SELECT FROM SomeTable p WHERE date = '01/01/2011'"

but what date is 06/07/2011 ? I think it is today 6th July, others 7th June. So even if you use strings use the ISO format 2011-07-06 A date type will hide this detail making it easier. Note that GQL only uses the ISO form so even if you got the command to have the date in a string it would fail.

but better as

SELECT FROM SomeTable p WHERE date = DATE( 2011, 1, 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文