日期中的唯一整数
这是一般编程,但我正在使用 Java 进行工作。
给定一个日期 dd-mm-yy
或 dd-mm-yyyy
(例如 13-01-2011),我想将其转换为一个唯一的数字,以便任何两个日期有不同的号码。年份并不重要。因此,只需将 dd-mm 转换为一个唯一的 int 即可。有谁知道执行此操作的算法?
对不起:我想更具体一些:
唯一的数字应该是从 1 到 365(或 0 到 364),或者应该以 365 为模进行唯一分解。(我现在忽略了闰年的情况) 。
因此连接“ddmm”可能是一个唯一的 4 位数字。但模 365 可能不会是唯一的。
This is general programming, but I'm working in Java.
Given a date dd-mm-yy
or dd-mm-yyyy
(e.g. 13-01-2011) I want to convert this into a unique number such that any two dates have a different number. And the year isin't important. So just converting dd-mm into one unique int is acceptable. Does anyone know an algorithm for doing this?
I'm SOrry: I want to be more specific:
The unique numbers should be from 1 to 365 (or 0 to 364), or should break down uniquely modulo 365. (I'm ignoring the case of leap years at the moment).
So concatenating "ddmm" might be a unique 4 digit number. But modulo 365 probably wouldnt be unique.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
那么您基本上想要获取一年中的哪一天?这与“日期中的唯一整数”不同。
使用
Calendar#get(Calendar.DAY_OF_YEAR )
(从 1 开始)。正如您已经承认的那样,在闰年/非闰年上进行比较将会失败。
So you basically want to get the day of year? That's not the same as "an unique int from date".
Use
Calendar#get(Calendar.DAY_OF_YEAR)
(which is 1-based).As you already admitted, comparing this on leap/non-leap years will fail.
使用自年初以来的天数。
Use the number of days since the beginning of the year.
自 1970 年以来的毫秒数对于每个日期来说应该是唯一的。只需将字符串转换为日期对象并使用 getTime() 即可
The number of milliseconds since 1970 should be unique to each date. Just convert the string to a date object and use
getTime()
使用unix时间戳请参阅 java .util.Date.getTime()
Use unix timestamp see java.util.Date.getTime()
如果您只需要一个唯一的键,Calendar 的 hashCode() 应该可以解决问题。
或者,您可以连接两位数的月份和日期以及四位数的年份。
If you just need a unique key, Calendar's hashCode() should do the trick.
Alternately, you can concatenate the two digit month and day, and four digit year.
更新:OP 澄清了需要 1-364 范围内的唯一值的问题。在这种情况下,我会将日期转换为
Calendar
对象并对其调用get (Calendar.DAY_OF_YEAR)
- 这将返回一年中的第几天,其中正是OP看起来的样子。原答案:
有多少字符串就可以有多少种方法来实现这一点。如果您希望能够使用唯一的数字进行日期的相对比较,那么最明显的解决方案就是执行大多数计算机所做的操作,即将日期转换为一些最小公分母,例如特定纪元的秒数。 Java 提供了使用
Date
和Calendar
类来执行此操作的方法。如果您只关心天和月,您可以使用int v = (month * 120) + day
来实现相同的效果。如果您不关心比较而只想要一个唯一值,则可以使用某种哈希算法得出日期摘要。例如,在 Java 中,您可以采用日期的String
表示形式并对其调用hashCode()
。Update: the OP clarified the question to want a unique value in the range 1-364. In that case I would convert the date into a
Calendar
object and callget (Calendar.DAY_OF_YEAR)
on it - this will return the number of the day within the year, which is exactly what the OP is after it seems.Original answer:
There about as many ways to do this as there are bits of string. If you want to be able to use the unique numbers for relative comparison of dates then the most obvious solution is to do what most computers do anyway, namely convert the date into some lowest common denominator, like the number of seconds from a particular epoch. Java provides ways for doing this with both the
Date
andCalendar
classes. If you only care about days and months, you could achieve the same with something likeint v = (month * 120) + day
. If you don't care about comparisons and just want a unique value, you could come up with a digest of the date using a hashing algorithm of some kind. In Java you could, for example, take aString
representation of the date and callhashCode()
on it.