1

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();
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Nick
  • 9,285
  • 33
  • 104
  • 147

4 Answers4

4

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.

1

Please check the below code i have modified it, may this solve your problem:

try {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", Locale.US);
        Date now = formatter.parse("2014-01-26 05:36:38 PM");
        Calendar calendar=Calendar.getInstance(Locale.US);
        calendar.setTime(now);

        RelativeTime(now.getTime());

    } catch (Exception e) {
        e.printStackTrace();
    } 

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);

}

public void toast(String text){
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
Shailendra Patil
  • 377
  • 2
  • 19
0

i believe the way you are calling toast is incorrect.

Try this link it should help you out a little more.

Toasty Bread. i just gave that a crazy name lol

Keeano
  • 309
  • 8
  • 33
0

You need SimpleDateFormat
Something like the following code may help you
"format" is the coding stucture of your string date like "dd MMM yyyy hh:mm:ss zzz"
"Value" in the code is your string date.
See http://developer.android.com/reference/java/text/SimpleDateFormat.html to have details on format and other "methods" about SimpleDateFormat

SimpleDateFormat sf = new SimpleDateFormat(format);
sf.setTimeZone(TimeZone.getTimeZone("UTC"));

//sf.setCalendar(Calendar.getInstance());
ParsePosition pp = new ParsePosition(0); 
Date date = sf.parse(Value,pp);
if (pp.getIndex() == 0) {
    Log.e(TAG,"Can't getDate with format:\""+format+"\" and value:\""+Value + "\" at char index:"+pp.getErrorIndex());
    return Calendar.getInstance();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);

cal.getTimeInMillis(); is compatible with your "time" parameter (long type)

Emmanuel Devaux
  • 3,327
  • 4
  • 25
  • 30