Question

i have this code in my PHP

function nicetime($date)
{
date_default_timezone_set("Asia/Taipei");
if(empty($date)) {
    return "No date provided";
}

$periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths         = array("60","60","24","7","4.35","12","10");

$now             = time();
$unix_date         = strtotime($date);

   // check validity of date
if(empty($unix_date)) {    
    return "Bad date";
}

// is it future date or past date
if($now > $unix_date) {    
    $difference     = $now - $unix_date;
    $tense         = "ago";

} else {
    $difference     = $unix_date - $now;
    $tense         = "from now";
}

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
    $difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
    $periods[$j].= "s";
}

return "$difference $periods[$j] {$tense}";
}

but now i want to do the same but this time in my Android. im having trouble because the string mytime is from a database, in an SQL format.

String mytime = pref.getString("announcementtime" + count, null);

output of mytime:

2013-08-31 15:55:22

i want to convert it to:

23 minutes ago //something like this

please be aware of the default timezone and the DateTime = Now

Was it helpful?

Solution

Its all in the DateUtils class.

CharSequence getRelativeTimeSpanString (long time, long now, long minResolution);

gives you the difference between time and now in a format like:

54 seconds ago.

To use your date string you have to convert it into empoch time first:

String mytime = pref.getString("announcementtime" + count, null); 

// it comes out like this 2013-08-31 15:55:22 so adjust the date format
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(str);
long epoch = date.getTime();

String timePassedString = getRelativeTimeSpanString (epoch, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);

OTHER TIPS

You can modify the input variable "timeAtMiliseconds", for my example was at format of date was at Miliseconds.

  public static String parseDate(String timeAtMiliseconds) {
    if (timeAtMiliseconds.equalsIgnoreCase("")) {
        return "";
    }
    //API.log("Day Ago "+dayago);
    String result = "now";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String todayDate = formatter.format(new Date());
    Calendar calendar = Calendar.getInstance();

    long dayagolong = Long.valueOf(timeAtMiliseconds) * 1000;
    calendar.setTimeInMillis(dayagolong);
    String agoformater = formatter.format(calendar.getTime());

    Date CurrentDate = null;
    Date CreateDate = null;

    try {
        CurrentDate = formatter.parse(todayDate);
        CreateDate = formatter.parse(agoformater);

        long different = Math.abs(CurrentDate.getTime() - CreateDate.getTime());

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        different = different % secondsInMilli;
        if (elapsedDays == 0) {
            if (elapsedHours == 0) {
                if (elapsedMinutes == 0) {
                    if (elapsedSeconds < 0) {
                        return "0" + " s";
                    } else {
                        if (elapsedDays > 0 && elapsedSeconds < 59) {
                            return "now";
                        }
                    }
                } else {
                    return String.valueOf(elapsedMinutes) + "m ago";
                }
            } else {
                return String.valueOf(elapsedHours) + "h ago";
            }

        } else {
            if (elapsedDays <= 29) {
                return String.valueOf(elapsedDays) + "d ago";
            }
            if (elapsedDays > 29 && elapsedDays <= 58) {
                return "1Mth ago";
            }
            if (elapsedDays > 58 && elapsedDays <= 87) {
                return "2Mth ago";
            }
            if (elapsedDays > 87 && elapsedDays <= 116) {
                return "3Mth ago";
            }
            if (elapsedDays > 116 && elapsedDays <= 145) {
                return "4Mth ago";
            }
            if (elapsedDays > 145 && elapsedDays <= 174) {
                return "5Mth ago";
            }
            if (elapsedDays > 174 && elapsedDays <= 203) {
                return "6Mth ago";
            }
            if (elapsedDays > 203 && elapsedDays <= 232) {
                return "7Mth ago";
            }
            if (elapsedDays > 232 && elapsedDays <= 261) {
                return "8Mth ago";
            }
            if (elapsedDays > 261 && elapsedDays <= 290) {
                return "9Mth ago";
            }
            if (elapsedDays > 290 && elapsedDays <= 319) {
                return "10Mth ago";
            }
            if (elapsedDays > 319 && elapsedDays <= 348) {
                return "11Mth ago";
            }
            if (elapsedDays > 348 && elapsedDays <= 360) {
                return "12Mth ago";
            }

            if (elapsedDays > 360 && elapsedDays <= 720) {
                return "1 year ago";
            }

            if (elapsedDays > 720) {
                SimpleDateFormat formatterYear = new SimpleDateFormat("MM/dd/yyyy");
                Calendar calendarYear = Calendar.getInstance();
                calendarYear.setTimeInMillis(dayagolong);
                return formatterYear.format(calendarYear.getTime()) + "";
            }

        }

    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    return result;
}

There is a nice library for just this purpose over on Github: https://github.com/curioustechizen/android-ago

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top