得到年,月,日从Android的天数 [英] get years, months, days from number of days on android
问题描述
我想在格式年,月,日的年龄;这样, X年和y月和z天
。
我能得到天这样的:
生日LOCALDATE =新LOCALDATE(dob_yr,dob_mt,dob_dy);
现在LOCALDATE =新LOCALDATE();
天nm_days = Days.daysBetween(出生日期,现);
我试着用 Log.d(天,nm_days.getDays()+);
,这是我得到了什么: 10365
我怎么能转换几天到几年月份和日期的格式?
修改
确定。现在我得到的东西,但还是没有真正解决我的问题,那就是:
天A = Days.daysBetween(出生日期,现);
月B = Months.monthsBetween(出生日期,现);
年以上C = Years.yearsBetween(出生日期,现);期间G =新周期(B);
PeriodFormatter DHM =新PeriodFormatterBuilder()
.appendYears()
.appendSuffix(年,年)
.appendSeparator(和)
.appendMonths()
.appendSuffix(月,月)
.appendSeparator(和)
.appendDays()
.appendSuffix(天,天)
.toFormatter();Log.d(时代,dhm.print(g.normalizedStandard()));
当我尝试这样做,我得到了28年4个月
,但如果我试图用周期G =新时代(一);
我只拿到了5天
这应该是28年4个月零17天
在一般情况下,你想要的 PeriodFormat.wordBased
和<一个href=\"http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html#Period%28org.joda.time.ReadablePartial,%20org.joda.time.ReadablePartial,%20org.joda.time.PeriodType%29\"相对=nofollow> 新时代
:
周期周期=新时代(出生日期,现);
Log.d(时代,PeriodFormat.wordBased()打印(期));
年龄:28岁4个月17天
块引用>通常情况下,这将拿起你的语言环境,或者你可以明确地传递一个区域设置为<一个href=\"http://joda-time.sourceforge.net/apidocs/org/joda/time/format/PeriodFormat.html#wordBased%28java.util.Locale%29\"相对=nofollow>
PeriodFormat.wordBased
。不幸的是,乔达似乎并没有进行本地化印尼。你可以使自己的格式,如你的问题,或者(如果你有更多的时间,希望有一个良好的CV项目等)的考虑作出印尼本地化乔达。I want to get age in format years, months, and days; like this,
x years and y months and z days
.I was able to get days with this:
LocalDate birthdate = new LocalDate (dob_yr, dob_mt, dob_dy); LocalDate now = new LocalDate(); Days nm_days = Days.daysBetween(birthdate, now);
I tried using
Log.d("days ", nm_days.getDays() +"");
and this is what I got:10365
How can I convert days to years months and days format?EDIT
Ok. now I get something, but still doesn't really solve my problem, here it is:
Days a = Days.daysBetween(birthdate, now); Months b = Months.monthsBetween(birthdate, now); Years c = Years.yearsBetween(birthdate, now); Period g = new Period(b); PeriodFormatter dhm = new PeriodFormatterBuilder() .appendYears() .appendSuffix(" years", " years") .appendSeparator(" and ") .appendMonths() .appendSuffix(" months", " months") .appendSeparator(" and ") .appendDays() .appendSuffix(" day", " days") .toFormatter(); Log.d("age", dhm.print(g.normalizedStandard()));
when I tried this, I got
28 years and 4 months
, but if I tried withPeriod g = new Period(a);
I only got5 days
it supposed to be28 years and 4 months and 17 days
解决方案In general, you want
PeriodFormat.wordBased
andnew Period
:Period period = new Period(birthdate, now); Log.d("age", PeriodFormat.wordBased().print(period));
age: 28 years, 4 months and 17 days
Normally, this would pick up your locale, or you could explicitly pass a locale to
PeriodFormat.wordBased
. Unfortunately, Joda doesn't seem to be localized for Indonesia. You can either make your own formatter, as in your question, or (if you have more time available, want a good CV item, etc.) look into making an Indonesian localization for Joda.这篇关于得到年,月,日从Android的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!