2

How in Android can I get and display the number of days, months, and years since a date?

For example, let's say the previous date is:

02/02/2017

and today's date is:

04/30/2018

I would want it to say something like:

1 year, 2 months, 28 days

I think I need to get total days like so:

long msDiff = Calendar.getInstance().getTimeInMillis() - oldCalendarDate.getTimeInMillis();
long daysDiff = TimeUnit.MILLISECONDS.toDays(msDiff);

but how do I convert the amount of days to be legible as years, months, and days? Keeping in mind that not all months have 31 days.

Because this way, I am only getting

452 days

As the result. But I want it to be more like 1 year, 2 months, 28 days.

Is there a better solution?

Also I want to get the old date like so in a variable:

val oldCalendarDate = GregorianCalendar(2017, 3, 30).time

but it's not working at all with the above code. Yes I'm aware that variable is in kotlin because I'm working in kotlin so pardon the syntax. I think the more issue about this is how the integers are formatted. I already converted the other java code to kotlin but I posted it as java for this post since more people are familiar with java.

Prags
  • 2,457
  • 2
  • 21
  • 38

2 Answers2

0

Calendar Object can resolve this problem

val msDiff = Calendar.getInstance().timeInMillis - oldCalendarDate.timeInMillis;
val calDiff = Calendar.getInstance()
calDiff.timeInMillis = msDiff
val msg = "${calDiff.get(Calendar.YEAR) - 1970} year, ${calDiff.get(Calendar.MONTH)} months, $calDiff.get(Calendar.DAY_OF_MONTH)} days"
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Jacob
  • 561
  • 4
  • 9
  • this works great. How can I use the same code to also calculator the amount of days until the same date (like a birth date) that next happens? for example, let's say "birthdate" variable is 3/30/2015. Today's date is 3/25/2018. I want to send a message that says "birthdate is in 5 days!" –  Apr 30 '18 at 20:30
-1

You try this code,

 long different = your today date - your previous date;
     // take both dates in date format.

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;
        long mothsInMilli = daysInMilli * 30;
        long yearInMilli = mothsInMilli * 12;

        long elapsedYear = different / yearInMilli;
        different = different % yearInMilli;
        long diffYear = elapsedYear;
        Log.d("", "difference of years : " + diffYear);

        long elapsedMonths = different / mothsInMilli;
        different = different % mothsInMilli;
        long diffMonth = elapsedMonths;
        Log.d("", "difference of Months : " + diffMonth);

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;
        long diffDays = elapsedDays;
        Log.d("", "difference of Days : " + diffDays);

Using this code I got proper difference, It may helps you also. Show below, I had two dates:

pastdate::Mon Mar 26 13:06:52 GMT+05:30 2018

todayDate::Mon Apr 30 13:06:52 GMT+05:30 2018

I got difference of month,year and days like below:-

    difference of Years : 0
    difference of Months : 1
    difference of Days : 4
Janvi Vyas
  • 732
  • 5
  • 16