Android:getRelativeTime示例

Nic*_*ick 15 time datetime android

有人可以告诉我一个如何正确使用这里getRelativeDateTimeString()详细说明的例子.

Viv*_*sse 41

我想你正在谈论getRelativeDateTimeString,你的链接指向.

示例now,其中包含详细说明所有参数的注释:

Date now = new Date();
String str = DateUtils.getRelativeDateTimeString(

        this, // Suppose you are in an activity or other Context subclass

        now.getTime(), // The time to display

        DateUtils.MINUTE_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
Run Code Online (Sandbox Code Playgroud)

对于其他值MINUTE_IN_MILLISYEAR_IN_MILLIS包括:

  • SECOND_IN_MILLIS
  • MINUTE_IN_MILLIS
  • HOUR_IN_MILLIS
  • DAY_IN_MILLIS
  • WEEK_IN_MILLIS
  • YEAR_IN_MILLIS
  • 任何自定义值(以毫秒为单位)

  • 你试过WEEK_IN_MILLIS和YEAR_IN_MILLIS吗?我无法使它工作,这是我的代码:`code` time = now - 7*24*60*60*1000; text + = DateUtils.getRelativeDateTimeString(this,time,DateUtils.MINUTE_IN_MILLIS,DateUtils.YEAR_IN_MILLIS,0)+"\n"; `code`.我想知道为什么它不支持MONTH_IN_MILLIS (6认同)
  • 为什么`DateTimeUtils`(来自Joda Time?)而不是`DateUtils`([android的一部分](http://developer.android.com/reference/android/text/format/DateUtils.html))? (5认同)