How set date as today, 1 day, 2 day and etc

87 views Asked by At

I have date array and i set data in my list.

holder.getDayReceipt().setText(receiptList.get(position).getCreatedDate().toString());

I need set date in this format:

Today  (16 June)
1 day  (15 June)
2 day  (14 June)
..............

How do it?

EDIT: You did not understand me

instead "16 June" i want Today
instead "15 June" i want 1 day instead "14 June" i want 2 day

enter image description here

2

There are 2 answers

0
Garima Mathur On

You can implement this piece of code

public String getTimeDiff(long secondsTimeDiff)
{       
    long secondsInOneDay = 84600;
    int maxDaysAgo = 15;

    if ( secondsTimeDiff < secondsInOneDay)
    {
        return "today";
    }
    else if ( secondsTimeDiff < 2*secondsInOneDay)
    {
         return "1 day";
    }
    else if ( secondsTimeDiff < maxDaysAgo*secondsInOneDay)
    {
        int days = (int) (secondsTimeDiff / secondsInOneDay);
        return days + " day";
    }
    else
    {
        //use normal DateUtils logic here...
        return "....";
    }
}

Hope it would be helpful to you!!

0
Andrea Cinesi On

I suggest to use the Joda library for time

for example:

public boolean isAfterPayDay(DateTime datetime) {
 if (datetime.getMonthOfYear() == 2) {
 // February is month 2!!
 return datetime.getDayOfMonth() > 26;
 }
 return datetime.getDayOfMonth() > 28;
 }