user3538235
user3538235

Reputation: 2009

Display format of the Date in android

Currently I have an instance of a Date record, like this:

Calendar recordDate = Calendar.getInstance();

try {
    recordDate .setTime(sdf.parse(joinPlanDate));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The problem is , generally I can display the date like 2014-08-20 15:05:11 etc..... And I would like to use another format , e.g.

Today, yesterday, few day ago, week ago , few week ago, year ago, few year ago etc..

Are there android formatter already can handle this case or how can I create a formatter? Thanks for helping

Upvotes: 0

Views: 145

Answers (2)

MilapTank
MilapTank

Reputation: 10076

have create one function that gives me string like twitter let give one anther answer see it and get idea from it and customize own way :)

public static String getTimeString(Date fromdate) {
    SimpleDateFormat twtimeformat = new SimpleDateFormat(
        "dd MMM");

    long then;
    then = fromdate.getTime();
    Date date = new Date(then);

    StringBuffer dateStr = new StringBuffer();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    Calendar now = Calendar.getInstance();

    int days = daysBetween(calendar.getTime(), now.getTime());
    int minutes = hoursBetween(calendar.getTime(), now.getTime());
    int hours = minutes / 60;
    if (days == 0) {

        int second = minuteBetween(calendar.getTime(), now.getTime());
        if (minutes > 60) {

            if (hours >= 1 && hours <= 24) {
                dateStr.append(hours).append("h");
            }

        } else {

            if (second <= 10) {
                dateStr.append("Now");
            } else if (second > 10 && second <= 30) {
                dateStr.append("few seconds ago");
            } else if (second > 30 && second <= 60) {
                dateStr.append(second).append("s");
            } else if (second >= 60 && minutes <= 60) {
                dateStr.append(minutes).append("m");
            }
        }
    } else

    if (hours > 24 && days <= 7) {
        dateStr.append(days).append("d");
    } else {
        dateStr.append(twtimeformat.format(date));
    }

    return dateStr.toString();
}

public static int minuteBetween(Date d1, Date d2) {
    return (int) ((d2.getTime() - d1.getTime()) / DateUtils.SECOND_IN_MILLIS);
}

public static int hoursBetween(Date d1, Date d2) {
    return (int) ((d2.getTime() - d1.getTime()) / DateUtils.MINUTE_IN_MILLIS);
}

public static int daysBetween(Date d1, Date d2) {
    return (int) ((d2.getTime() - d1.getTime()) / DateUtils.DAY_IN_MILLIS);
}

Upvotes: 2

MilapTank
MilapTank

Reputation: 10076

see this DateUtil method

this is standard inbuilt method if you want as you ask than you have to customize own

  1. 3 mins ago, 10:15 AM
  2. yesterday, 12:20 PM
  3. Dec 12, 4:12 AM
  4. 11/14/2007, 8:20 AM

getRelativeDateTimeString

String relativeString = getRelativeDateTimeString (this, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,DateUtils.WEEK_IN_MILLIS, 0));

Upvotes: 1

Related Questions