Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implementing pretty time to format Date time in android

Tags:

android

I am implementing pretty time library from this site http://www.ocpsoft.org/prettytime/ to get a formatted date string with a text in my android application.

I am feeding my date as a string from my local sqlite database,

     String dateString="2015-09-25 15:00:47";
     SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");

     Date convertedDate = new Date();

     try {
         convertedDate = dateFormat.parse(dateString);
     } catch (ParseException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }

     PrettyTime p  = new PrettyTime();

     String datetime= p.format(convertedDate);
     textview.setText(datetime);

I want to be able to get my date formatted with parsetext like

 2 minutes ago
 1 day ago
 ...

However the snippet codes above gives me just the text and avoids the time. This is what it gives

 minutes ago
...

Please is there something i need to do, I would be grateful if some could help. Thanks

like image 460
NewBIe Avatar asked Oct 26 '25 11:10

NewBIe


2 Answers

I'm not sure what PrettyTime is, but you can use something like this:

private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;

public static Date currentDate() {
    Calendar calendar = Calendar.getInstance();
    return calendar.getTime();
}

public static String getTimeAgo(long time) {
    if (time < 1000000000000L) {
        // if timestamp given in seconds, convert to millis
        time *= 1000;
    }

    long now = currentDate().getTime();
    if (time > now || time <= 0) {
        return "in the future";
    }

    final long diff = now - time;
    if (diff < MINUTE_MILLIS) {
        return "Moments ago";
    } else if (diff < 2 * MINUTE_MILLIS) {
        return "A minute ago";
    } else if (diff < 50 * MINUTE_MILLIS) {
        return diff / MINUTE_MILLIS + " minutes ago";
    } else if (diff < 90 * MINUTE_MILLIS) {
        return "An hour ago";
    } else if (diff < 24 * HOUR_MILLIS) {
        return diff / HOUR_MILLIS + " hours ago";
    } else if (diff < 48 * HOUR_MILLIS) {
        return "Yesterday";
    } else {
        return diff / DAY_MILLIS + " days ago";
    }
}
like image 173
jyanks Avatar answered Oct 28 '25 03:10

jyanks


You're not actually passing a date into the PrettyTime object, because your date-conversion fails. Confirm that in your logcat.

The problem is that you're passing a different date format and 24h time into this date format:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");

You can either change the date format string to use 24h time and match your date format, like so:

String dateString="2015-09-25 15:00:47";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

or change the time you pass in to be:

 String dateString="09/25/2015-09-25 3:00:47 pm";
 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
like image 21
323go Avatar answered Oct 28 '25 01:10

323go