1

タイムスタンプを含む文字列をアンドロイドの RelativeDateTimeString と一致する時間に変換しようとしているので、相対時間としてフォーマットできます。取得するタイムスタンプは次の形式です。

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();
}
4

4 に答える 4

4

SimpleDateFormatを使用して、タイムスタンプを文字列からオブジェクトparse()に変換します。Dateその後Date.getTime()、ミリ秒単位でタイムスタンプの長い値を取得するために使用できます。

于 2011-08-19T15:10:58.473 に答える
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();
}
于 2014-01-28T12:32:23.113 に答える
0

SimpleDateFormatが必要です
。次のコードのようなものが役立つ場合があります。
「format」は、「dd MMM yyyy hh:mm:sszzz」のような文字列日付のコーディング構造です。コード内の
「Value」は文字列日付です。SimpleDateFormatに関するフォーマットおよびその他の「メソッド」の詳細については、http://developer.android.com/reference/java/text/SimpleDateFormat.html
を 参照してください。

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(); 「時間」パラメータと互換性があります(ロングタイプ)

于 2011-08-19T15:16:41.740 に答える
0

トーストの呼び方が間違っていると思います。

このリンクを試してみてください。もう少し役立つはずです。

トーストパン。変な名前つけちゃった笑

于 2011-08-19T15:06:14.207 に答える