在 JSON 中存储日期/时间的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22354899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:12:00  来源:igfitidea点击:

What is the best way to store a date/time in JSON?

jsondatetypes

提问by zgall1

I am new to JSON and in reviewing the JSON specification, I have noticed that there is no data type for dates and times. I have done some research and come across a few suggestions, one of which was using a UNIX timestamp. Is that the simplest approach? Will I run into any problems down the line?

我是 JSON 的新手,在查看 JSON 规范时,我注意到日期和时间没有数据类型。我做了一些研究,并提出了一些建议,其中之一是使用 UNIX 时间戳。这是最简单的方法吗?我会遇到任何问题吗?

回答by

I recommend to use ISO 8601 dates. Especially this format

我建议使用ISO 8601 日期。特别是这种格式

2014-03-12T13:37:27+00:00

is portable across many programming languages.

可移植到多种编程语言。

Edit:

编辑:

JSON only knows these types:

JSON 只知道这些类型

string
number
object
array
true
false
null

Dates and datetimes are best stored as strings in a format that is widely used.

日期和日期时间最好以广泛使用的格式存储为字符串。

回答by S. Fernandez

Of course you should save String in a JSON. But lets say you want to filter dates. You can still store a "number" as a String like "125"(Binary Codified Decimal) and it will be useful when you convert it back to 125. That said, If you want to customize your date number for filtering for example: Use long type and perform bitwiseoperations on it. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

当然,您应该将 String 保存在 JSON 中。但是假设您要过滤日期。您仍然可以将“数字”存储为“125”(二进制编码十进制)之类的字符串,当您将其转换回 125 时它会很有用。也就是说,如果您想自定义日期数字进行过滤,例如:使用long 类型并对其执行按位运算。 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

  • Reserve 12 bits for YYYY until year 4096-1.
  • Reserve 4 bits for MM because (0-11)is enough.
  • Reserve 5 bits for DD because day of the month is (1-31).
  • 为 YYYY 保留 12 位,直到 4096-1 年。
  • 为 MM 保留 4 位,因为 (0-11) 就足够了。
  • 为 DD 保留 5 位,因为月中的第几天是 (1-31)。

You can continue reserving bits for hours, minutes until 64bit. In this case DD/MM/YYYY is 21 bit. By using this aproach, you agree to @Clay Ferguson and you are being able to filter a KeySet in a Map in terms of "<" and ">" by year, month, day very easy and probably very fast.

您可以继续保留数小时、数分钟直到 64 位。在这种情况下,DD/MM/YYYY 是 21 位。通过使用这种方法,您同意@Clay Ferguson,并且您可以非常容易且可能非常快地根据“<”和“>”按年、月、日过滤 Map 中的 KeySet。

回答by S. Fernandez

If you care more about efficiency of memory (and diskspace) than human readability of the raw JSON it's best to store as an integer (i.e. the millisecond value of the date). Also if you really wanted to optimize for space savings you could encode that integer to Base64, and then let the JSON hold the base64 string as a JSON string. I don't even mention hex, because if you are encoding to something non-decimal you may as well go to base64. In other words there are no benifits to hex encoding over base64 so just go with base64.

如果您更关心内存(和磁盘空间)的效率而不是原始 JSON 的人类可读性,那么最好将其存储为整数(即​​日期的毫秒值)。此外,如果您真的想优化节省空间,您可以将该整数编码为 Base64,然后让 JSON 将 base64 字符串保存为 JSON 字符串。我什至没有提到十六进制,因为如果您要编码为非十进制的东西,您也可以转到 base64。换句话说,通过 base64 进行十六进制编码没有任何好处,因此只需使用 base64。