java 使用什么系统默认日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4822168/
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
What system default date format to use?
提问by Maxim Veksler
I'm setting the standards for our application.
我正在为我们的应用程序设定标准。
I've been wondering, what default date format should I choose to use ?
我一直在想,我应该选择使用什么默认日期格式?
It should be:
它应该是:
- Internationalization & timezone aware, the format should be able to represent user local time
- Can be efficiently parsed by SimpleDataFormat (or alike, jdk classes only)
- Programming Language agnostic (can parse in java, python, god forbid C++ :) and co.)
- Preferably ISO based or other accepted standard
- Easy to communicate over HTTP (Should such need arises, JSON or YAML or something in this nature)
- Can represent time down to seconds resolution (the more precise the better, micro seconds if possible).
- Human readable is a plus but not required
- Compact is a plus but not required
- 国际化和时区意识,格式应该能够代表用户本地时间
- 可以通过 SimpleDataFormat(或类似的,仅限 jdk 类)有效解析
- 编程语言不可知(可以在 java、python、god forbid C++ :) 和 co. 中解析。)
- 最好是基于 ISO 或其他公认的标准
- 易于通过 HTTP 通信(如果出现这种需求,JSON 或 YAML 或类似的东西)
- 可以将时间表示为秒分辨率(越精确越好,如果可能的话,微秒)。
- 人类可读是加分项,但不是必需的
- 紧凑是一个加分项,但不是必需的
Thank you,
Maxim.
谢谢你,
马克西姆。
回答by jny
回答by aioobe
The most canonical and standard form is probably "Unix Time": The number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970.
最规范和标准的形式可能是“Unix 时间”:自 1970 年 1 月 1 日午夜协调世界时 (UTC) 以来经过的秒数。
If you set that as the default time-format you can easily parse it, store it in memory, write it to disk, easily communicate it over HTTP and so on. It is also definitely an accepted standard, and in a sense it is "time-zone aware", since it is well-defined regardless of time-zones.
如果您将其设置为默认时间格式,您就可以轻松地解析它、将其存储在内存中、将其写入磁盘、轻松地通过 HTTP 进行通信等等。它也绝对是一个公认的标准,从某种意义上说它是“时区感知”,因为它是明确定义的,而不考虑时区。
(This is the format in which I always store all my time stamps; in databases, in memory, on disk, ...)
(这是我始终存储所有时间戳的格式;在数据库中,在内存中,在磁盘上,......)
回答by Stephen P
The "right" default formatreally depends on what you're doing with it. The formats for parsing, storing, and displaying can all be different.
“正确”的默认格式实际上取决于您使用它做什么。解析、存储和显示的格式都可以不同。
For storing the date you're (almost) always going to want to use UTC as aioobe says, even when you want to display it in user local time. I say "(almost)" but I really can't think of a case where I would notwant UTC for a saved date. You maywant to store the TZ information for where the date originated also, so you can report it in that local time, but more often you want to display the local time for the whoever is currently lookingat the date. That means having a way to determine the current user's local time regardless of what the original local time was.
为了存储日期,您(几乎)总是想像 aioobe 所说的那样使用 UTC,即使您想在用户本地时间显示它。我说“(几乎)”,但我真的想不出我不希望 UTC 作为保存日期的情况。您可能还希望存储日期起源地的 TZ 信息,以便您可以在该本地时间报告它,但更多情况下,您希望显示当前查看日期的人的本地时间。这意味着有一种方法可以确定当前用户的本地时间,而不管原始本地时间是什么。
For displaying it, the "default format" should usually be determined by the viewers locale. 08/09/10 usually means 2010-Aug-9 in the U.S. ("Middle endian") but normally means 2010-Sep-8 in most of the rest of the world ("Little endian"). The ISO-8601 format "2010-09-10" is safe and unambiguous but often not what people expect to see. You can also look over RFC-3339for Date and Time on the internet and RFC-2822for message format (transmitting the date)
为了显示它,“默认格式”通常应由查看器的语言环境确定。08/09/10 在美国通常意味着 2010-Aug-9(“ Middle endian”),但在世界其他大部分地区通常意味着 2010-Sep-8(“ Little endian”)。ISO-8601 格式“2010-09-10”是安全且明确的,但通常不是人们期望看到的。您还可以查看Internet 上的日期和时间的RFC-3339和消息格式(传输日期)的RFC-2822
For parsing a date, you'll want to parse it and convert it to UTC, but you should be fairly flexible on what you accept. Again, the end users Locale and timezone, if discoverable, can help you determine what format(s) of string to accept as input. This is assuming user-typed strings. If you're generating a date/time stamp you can control the form and parsing will be no problem.
为了解析日期,您需要解析它并将其转换为 UTC,但您应该对接受的内容相当灵活。同样,最终用户区域设置和时区(如果可发现)可以帮助您确定接受什么格式的字符串作为输入。这是假设用户输入的字符串。如果您生成日期/时间戳,您可以控制表单并且解析将没有问题。
I also second BalusC linkwhich I hadn't seen before and have now favorited.
我还有第二个BalusC 链接,我以前从未见过,现在很喜欢。