问题
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 with Period g = new Period(a);
I only got 5 days
it supposed to be 28 years and 4 months and 17 days
回答1:
In general, you want PeriodFormat.wordBased and new 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.
来源:https://stackoverflow.com/questions/20013301/get-years-months-days-from-number-of-days-on-android