我试图确定从某个日期开始的年龄。有谁知道在 Android 中执行此操作的干净方法吗?显然,我有可用的 Java api,但是直接的 java api 相当弱,我希望 Android 有一些东西可以帮助我。
编辑:由于 Android Java - 在 Android 中使用 Joda 时间的多个建议让我有点担心Joda Date 速度慢 以及相关问题。此外,为这种规模的东西引入平台未附带的库可能有点过分了。
I am trying to determine an age in years from a certain date. Does anyone know a clean way to do this in Android? I have the Java api available obviously, but the straight-up java api is pretty weak, and I was hoping that Android has something to help me out.
EDIT: The multiple recommendations to use Joda time in Android worries me a bit due to Android Java - Joda Date is slow and related concerns. Also, pulling in a library not shipped with the platform for something this size is probably overkill.
发布评论
评论(12)
注意:正如 Ole VV 注意到的,这不适用于基督预产期之前的日期日历如何工作。
Note: as Ole V.V. noticed, this won't work with dates before Christ due how Calendar works.
tl;dr
测试零年过去了,一年过去了。
请参阅在 Ideone.com 上运行的代码。
java.time
旧的日期时间类确实很糟糕,以至于 Sun 和 java.time 都非常糟糕。 Oracle 同意用 java.time 类取代它们。如果您对日期时间值做了任何重要的工作,那么向您的项目添加一个库是值得的。 Joda-Time 库非常成功并受到推荐,但现在处于维护模式。该团队建议迁移到 java.time 类。
许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7 并进一步适应 Android 在 ThreeTenABP(请参阅如何使用...)。
转储到控制台。
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
,&SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到java.time 类。
要了解更多信息,请参阅 Oracle 教程 。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 jeps/170" rel="noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。
从哪里获取 java.time 类?
tl;dr
Test for zero years elapsed, and one year elapsed.
See this code run at Ideone.com.
java.time
The old date-time classes really are bad, so bad that both Sun & Oracle agreed to supplant them with the java.time classes. If you do any significant work at all with date-time values, adding a library to your project is worthwhile. The Joda-Time library was highly successful and recommended, but is now in maintenance mode. The team advises migration to the java.time classes.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
Dump to console.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
我建议使用很棒的 Joda-Time 库来处理 Java 中与日期相关的所有内容。
根据您的需要,您可以使用
Years.yearsBetween()
方法。I would recommend using the great Joda-Time library for everything date related in Java.
For your needs you can use the
Years.yearsBetween()
method.我显然还不能发表评论,但我认为你可以使用 DAY_OF_YEAR 来锻炼是否应该将年份向下调整一年(从当前最佳答案复制和修改)
类似地,你可能只需比较时间的 ms 表示并除以按一年中的毫秒数计算。只需将所有内容保留为长整型,在大多数情况下(闰年,哎哟)就足够了,但这取决于您的应用程序的年数以及该函数的性能必须如何,是否值得进行这种黑客攻击。
I apparently can't comment yet, but I think you can just use the DAY_OF_YEAR to workout if you should adjust the years down one (copied and modified from current best answer)
Similarly you could probably just diff the ms representations of the time and divide by the number of ms in a year. Just keep everything in longs and that should be good enough most of the time (leap years, ouch) but it depends on your application for the number of years and how performant that function has to be weather it would be worth that kind of hack.
我知道你要求一个干净的解决方案,但这里有两个肮脏的一次:
I know you have asked for a clean solution, but here are two dirty once:
这是我认为更好的方法:
编辑
除了我修复的错误之外,此方法不适用于闰年。这是一个完整的测试套件。我想你最好使用已接受的答案。
Here's what I think is a better method:
EDIT
Apart from a bug which I fixed, this method does not work well with leap years. Here's a complete test suite. I guess you're better off using the accepted answer.
如果您不想与日历、区域设置或外部库接壤,这是一个方便的方法:
a handy one if you don't want to border Calendar, Locale, or external library:
如果你不想使用java的日历来计算它,你可以使用 Androids Time class 它应该更快,但当我切换时我没有注意到太大的差异。
我找不到任何预定义的函数来确定 Android 中某个年龄的两个日期之间的时间。有一些很好的辅助函数可以在 DateUtils 但这可能不是您想要的。
If you don't want to calculate it using java's Calendar you can use Androids Time class It is supposed to be faster but I didn't notice much difference when i switched.
I could not find any pre-defined functions to determine time between 2 dates for an age in Android. There are some nice helper functions to get formatted time between dates in the DateUtils but that's probably not what you want.
这将起作用,如果您想要将年数替换为 12 到 1
This will work and if you want the number of years replace 12 to 1
感谢@Ole Vv 的审阅:我发现了一些内置的库类,它们的作用相同
Thanks @Ole V.v for reviewing it: i have found some inbuilt library classes which does the same
试试这个:
Try this: