Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remaining years, months, days using joda time (Android)

Tags:

java

android

I am trying to obtaining remaining years, months, and days between two dates: So I have used Joda Time to do so:

DateTime endDate  = new DateTime(2018,12,25,0,0);   
DateTime startDate = new DateTime();
Period period = new Period(startDate,endDate,PeriodType.yearMonthDay());

PeriodFormatter formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" Year ").
            appendMonths().appendSuffix(" Month ").appendDays().appendSuffix(" Day ").appendHours()..toFormatter();

String time = formatter.print(period);

This gives me string time: 2 Year 4 Month 22 Day

However, I want integer values of each number of remaining years, months, days. So, Instead of "2 Year 4 Month 22 Day", I want to set my variables:

int year = 2
int month = 4
int day = 22

Is there any way to obtain these values separately instead of obtaining one string? Thank you so much! :)

like image 507
ABOm Avatar asked Sep 17 '25 10:09

ABOm


1 Answers

i had the same requirement once ,here is the code snippet

    LocalDate d=LocalDate.of(yy,mm,dd);
    LocalDate d2=LocalDate.of(yy, mm, dd);
    Period p=Period.between(d, d2);
    long day,month,year;
    day=p.getDays();
    month=p.getMonths();
    year=p.getYears();
    System.out.println(day+" : "+month+" : "+year);
like image 115
Pavneet_Singh Avatar answered Sep 20 '25 02:09

Pavneet_Singh