I am trying to convert a string that contains a time stamp to a time that is consistent with androids RelativeDateTimeString, so I can format it as a relative time. The time stamps that I get are in this format:
2011-08-17 04:57:38
I would like to take that string and pass it through my relative time function here:
public void RelativeTime(Long time){
String str = (String) DateUtils.getRelativeDateTimeString(
this, // Suppose you are in an activity or other Context subclass
time, // The time to display
DateUtils.SECOND_IN_MILLIS, // The resolution. This will display only minutes
// (no "3 seconds ago"
DateUtils.WEEK_IN_MILLIS, // The maximum resolution at which the time will switch
// to default date instead of spans. This will not
// display "3 weeks ago" but a full date instead
0); // Eventual flags
toast(str);
}
So the function should show a toast of "2 days ago" etc.
EDIT: Sorry I have a toast function I've written as well.
public void toast(String text){
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
Use SimpleDateFormat and it's parse()
function to convert your timestamp from a string to a Date
object. After that you can use Date.getTime()
to get the long value of your timestamp in ms.