2017-02-15 58 views
2

我想將日期轉換爲人類可讀的格式。我正在使用DateUtils. getRelativeDateTimeString,但這不符合標準。我得到的輸出如下所示:1小時15分鐘。前等在Android中顯示與自定義格式的相對日期

我想知道,如果有可能的格式更改爲:

3米代替3分鐘。前,

1h而不是1小時。 15分鐘。前

使用DateUtils或有另一種方法來做到這一點?

更準確地說,我找這angular-filter的安卓相當於在那裏你可以很容易地改變相對日期的格式(例如:{{minutes}} minutes ago{{minutes}}m

爲了讓自己清楚,我不是搜索以格式化日期,但將日期轉換爲人類可讀的格式,例如'今天','1小時','38分鐘'(類似於Facebook的相對日期)

+0

我的圖書館[Time4A(https://github.com/MenoData/Time4A)提供的類[PrettyTime](http://time4j.net/javadoc-en/net/time4j/PrettyTime.html )約85種語言和不同的文字寬度。 –

回答

1

經過一番研究,我發現了一些圖書館像Time4AJoda-TimePrettyTimeAndroid-Ago

但是,我決定不使用庫並重寫它的文本資源,而是創建一個方法並在strings.xml中存儲文本以便將來進行本地化。

private static final int SECOND_MILLIS = 1000; 
    private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS; 
    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS; 
    private static final int DAY_MILLIS = 24 * HOUR_MILLIS; 
    private static final int WEEK_MILLIS = 7 * DAY_MILLIS; 

    public static String getTimeAgo(Date date, Context context) { 
     Date now = Calendar.getInstance().getTime(); 
     final long diff = now.getTime() - date.getTime(); 

     if (diff < SECOND_MILLIS) { 
      return context.getString(R.string.just_now); 
     } else if (diff < MINUTE_MILLIS) { 
      return diff/SECOND_MILLIS + context.getString(R.string.seconds_ago); 
     } else if (diff < 2 * MINUTE_MILLIS) { 
      return context.getString(R.string.a_minute_ago); 
     } else if (diff < 59 * MINUTE_MILLIS) { 
      return diff/MINUTE_MILLIS + context.getString(R.string.minutes_ago); 
     } else if (diff < 90 * MINUTE_MILLIS) { 
      return context.getString(R.string.an_hour_ago); 
     } else if (diff < 24 * HOUR_MILLIS) { 
      return diff/HOUR_MILLIS + context.getString(R.string.hours_ago); 
     } else if (diff < 48 * HOUR_MILLIS) { 
      return context.getString(R.string.yesterday); 
     } else if (diff < 6 * DAY_MILLIS) { 
      return diff/DAY_MILLIS + context.getString(R.string.days_ago); 
     } else if (diff < 11 * DAY_MILLIS) { 
      return context.getString(R.string.a_week_ago); 
     } else { 
      return diff/WEEK_MILLIS + context.getString(R.string.weeks_ago); 
     } 
    } 
-1

爲什麼你不分析日期到你想要的格式?

用string.replace()你可以做到這一點在一行代碼中。

編輯1:我通常使用SimpleDateForma這種方式,希望它有助於:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String currentDateandTime = sdf.format(new Date());

你會需要這個進口(我認爲Android的自動導入):

import java.text.SimpleDateFormat; 
import java.util.Date; 

編輯2 :在你的例子中你需要做的是:

SimpleDateFormat sdf = new SimpleDateFormat("HH'h' mm'm'"); String currentDateandTime = sdf.format(new Date()); System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

+1

如何在3分鐘前或1小時前解析Date對象?這不是一行代碼。我問是否有這樣的庫或util。 –

+1

OP不想格式化一個類型爲「java.util.Date」的對象,而是這個類型的兩個對象之間的增量,即持續時間。這是一個很大的區別。 –

+0

Meno Hochschild,正是!它被稱爲相對日期或相對時間。 –

-1

您應該使用SimpleDateFormat並指定所需的模板。而你的情況是這樣的:

String template = "H'h', m'm'"; 
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(template, Locale.getDefault()); 
String formatted = simpleDateFormat.format(new Date()); 
+2

我正在尋找一個相對日期,而不是日期格式! –

0

使用內置了被列入API級別DateUtils實用程序庫3.

CharSequence getRelativeDateTimeString (Context c, 
      long time, 
      long minResolution, 
      long transitionResolution, 
      int flags) Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]". 

爲美國日期格式示例輸出字符串:

3分鐘。以前,10:15 AM昨天,12:20 PM 12月12日,上午04點12分2007年11月14日, 8:20 AM

+0

我已經說過我在我的問題中使用DataUtils。 –