简体   繁体   English

生日:每个地方的生日相同吗?

[英]Date of birth: Same date of birth in every local?

Currently im storing the date of birth of a user in milliseconds (since 01.01.1970). 当前,im以毫秒为单位存储用户的出生日期(自1970年1月1日开始)。 Do i have to ensure that it is the same date of birth in every country or is it common that it can happen that it is the 11th November in country X and the 12th November in country Y? 我是否必须确保每个国家/地区的生日相同?或者X国的11月11日和Y国的11月12日是正常的情况?

If instead it is common practice to have the exact same date of birth in each country, how could i ensure that the milliseconds i store always point to the exact same day of month in each country? 取而代之的是,如果在每个国家/地区都具有完全相同的出生日期,那么我如何确保我存储的毫秒数始终指向每个国家/地区中的确切月份?

Ignore time-of-day 忽略时间

Few people know the time-of-day of their birth. 很少有人知道他们的出生时间。 So, no, that level of detail is not commonly tracked. 因此,不,通常不会跟踪该细节级别。 You never see time-of-birth and birth-time-zone on passports and such. 您从不会在护照等上看到出生时间和出生时区。 They track a date-only value, without time of day and without time zone. 他们跟踪仅日期的值,没有一天中的时间,也没有时区。

Bartenders, border control agents, etc. use the local current date to calculate age, with no attempt to consider time of day or adjust for time zone. 调酒师,边界控制人员等使用当地当前日期来计算年龄,而不会尝试考虑一天中的时间或针对时区进行调整。 Consider that partial-day difference to be truncated, ignored. 考虑到部分日差将被截断,忽略。

To adjust a moment accurately from one time zone to another, you need a date and a time-of-day. 要从一个时区准确地调整到另一个时区,您需要一个日期一个时间。 Without a time-of-day you have perhaps 23-25 hours of possible moments when their birth may have occurred. 没有一天的时间,您也许有23-25个小时的可能时刻出生了。

For example, a birth that occurs a few minutes after midnight in Paris (1 or 2 hours ahead of UTC) on the 24th is still “yesterday” the 23rd in Montréal (4 or 5 hours behind UTC). 例如,在巴黎的午夜后24分钟(比UTC提前1或2小时)出生的时间仍然是“昨天”在蒙特利尔的23日(比UTC落后4或5小时)。 But if that birth occurs at 06:00 in the 24th, then the date is the same (24th) in both Paris & Montreal, but is “yesterday” (23rd) in Vancouver BC and Seattle where the clocks are 7 or 8 hours behind UTC. 但是,如果该出生日期是24日的06:00,那么巴黎和蒙特利尔的日期是相同的(24日),而在卑诗省温哥华和西雅图的日期是“昨天”(23日),那里的时钟要晚7或8小时世界标准时间。

SQL 的SQL

In SQL use a data type akin to the standard DATE type. 在SQL中,使用类似于标准DATE类型的数据类型。

Java 爪哇

In Java, use the LocalDate type. 在Java中,使用LocalDate类型。 To represent the recurring month and day of the birthday celebration, use the MonthDay class. 要表示生日庆祝活动的重复月份和日期,请使用MonthDay类。

For interacting with a database, drivers that comply with JDBC 4.2 should be able to work directly with LocalDate via the getObject & setObject methods on a PreparedStatement . 为了与数据库进行交互,符合JDBC 4.2的驱动程序应该能够通过PreparedStatement上的getObjectsetObject方法直接与LocalDate使用。 If not, fall back to using the old java.sql.Date . 如果不是,则退回到使用旧的java.sql.Date New methods added to that old class facilitate conversion. 添加到该旧类中的新方法促进了转换。

ISO 8601 ISO 8601

When generating string representations of date-time values, use the formats defined in the ISO 8601 standard such as YYYY-MM-DD , ex: 1960-07-11 . 生成日期时间值的字符串表示形式时,请使用ISO 8601标准中定义的格式,例如YYYY-MM-DD ,例如: 1960-07-11 For date without year use --MM-DD , ex: --07-11 . 对于不带年份的日期--MM-DD ,例如:-- --07-11 The java.time classes in Java use ISO 8601 formats by default when parsing and generating Strings. 解析和生成字符串时,Java中的java.time类默认使用ISO 8601格式。

Forcing time-of-day 强迫一天中的时间

If for some reason you are forced to put a date-only value into a date-time field, then you must set some arbitrary time-of-day. 如果由于某种原因您被迫将仅日期值放入日期时间字段中,则必须设置一些任意的时间。 Noon is one possibility. 中午是一种可能。 I suggest setting the time portion to the first moment of the day. 我建议将时间部分设置为一天的第一时刻。

First moment of the day 一天的第一刻

Be aware that the first moment is not always 00:00:00 . 请注意,第一时刻并不总是00:00:00 Daylight Saving Time (DST) and perhaps other anomalies affect midnight in some time zones. 夏令时(DST)和其他异常可能会影响某些时区的午夜。 So first moment of the day might be 01:00:00 for example. 因此,例如,一天的第一时刻可能是01:00:00 Java can determine the first moment of the day appropriate to a particular time zone. Java可以确定适合特定时区的一天中的第一时刻。

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
ZonedDateTime startOfToday = today.atStartOfDay( zoneId );

It is up to you to decide what time zone to interpret a selected date of birth as and what time zone to use when displaying the date of birth. 由您决定将哪个出生日期解释为哪个时区,以及在显示出生日期时使用哪个时区。 In my opinion, it is logical to always use the timezone of the place of birth (if you wish to collect that) when converting the date of birth from a timestamp to readable form and when storing it. 我认为,在将出生日期从时间戳转换为可读格式并进行存储时,总是使用出生地的时区(如果您希望收集)是合乎逻辑的。 Otherwise, you can always use GMT or any other timezone as your convention. 否则,您可以始终使用GMT或任何其他时区作为约定。 This will ensure that all users in all countries will see the same date of birth but it is recommended to append the time zone to the date representation to prevent confusion. 这样可以确保所有国家/地区的所有用户都可以看到相同的出生日期,但是建议在日期表示中附加时区,以免造成混淆。 You may of course choose to interpret dates as GMT based when creating the timestamp to store and then render the date of birth using a user-defined timezone (possibly timezone for the user's account). 当然,您可以在创建要存储的时间戳时选择将日期解释为GMT,然后使用用户定义的时区(可能是用户帐户的时区)呈现出生日期。 In this case, the date (and time if you include the time of birth) will appear different for each user. 在这种情况下,每个用户的日期(如果包括出生时间,也会显示时间)会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM