2011-08-19 97 views
1

我想將包含時間戳的字符串轉換爲與android的RelativeDateTimeString一致的時間,所以我可以將其格式化爲相對時間。時間戳,我得到的都是這種格式:Android:將字符串轉換爲時間

2011-08-17 04:57:38 

我想借這個字符串,並使之通過我的相對時間函數的位置:

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

} 

因此函數應該表現出的麪包「2天前」等。

謝謝!

編輯:對不起,我已經寫了一個吐司功能。

public void toast(String text){ 
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); 
} 
+0

又有什麼問題? – Cristian

+0

問題是如何獲取我擁有的時間戳並將其轉換爲相對時間函數的可用格式。 – Nick

回答

4

使用SimpleDateFormat和它的parse()功能,以您的時間戳從字符串轉換爲Date對象。之後,您可以使用Date.getTime()以毫秒爲單位獲取您的時間戳的長整型值。

0

我相信你叫烤麪包的方式是不正確的。

試試這個鏈接它應該會幫助你多一點。

Toasty Bread.我只是給了一個瘋狂的名字笑

+0

請參閱上面的修改。 – Nick

0

您需要的SimpleDateFormat
類似下面的代碼可以幫助你
「格式」是你的字符串日期,如 「DD MMM YYYY HH:MM:SS ZZZ」 的編碼stucture
「值」代碼中的是您的字符串日期。
參見http://developer.android.com/reference/java/text/SimpleDateFormat.html會對格式和細節上的其它 「方法」 大約的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();是你的「時間」參數(長型)兼容

1

請檢查下面的代碼,我已經修改了它,這可能會解決您的問題:

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(); 
} 
相關問題