在Android中显示与自定义格式的相对日期
我想将日期转换为人类可读的格式。我正在使用DateUtils. getRelativeDateTimeString,但这不符合标准。我得到的输出如下所示:1小时15分钟。前等在Android中显示与自定义格式的相对日期
我想知道,如果有可能的格式更改为:
3米代替3分钟。前,
1h而不是1小时。 15分钟。前等
使用DateUtils或有另一种方法来做到这一点?
更准确地说,我找这angular-filter的安卓相当于在那里你可以很容易地改变相对日期的格式(例如:{{minutes}} minutes ago
到{{minutes}}m
为了让自己清楚,我不是搜索以格式化日期,但将日期转换为人类可读的格式,例如'今天','1小时','38分钟'(类似于Facebook的相对日期)
经过一番研究,我发现了一些图书馆像Time4A,Joda-Time, PrettyTime,Android-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);
}
}
为什么你不分析日期到你想要的格式?
用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);
如何在3分钟前或1小时前解析Date对象?这不是一行代码。我问是否有这样的库或util。 –
OP不想格式化一个类型为“java.util.Date”的对象,而是这个类型的两个对象之间的增量,即持续时间。这是一个很大的区别。 –
Meno Hochschild,正是!它被称为相对日期或相对时间。 –
您应该使用SimpleDateFormat并指定所需的模板。而你的情况是这样的:
String template = "H'h', m'm'";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(template, Locale.getDefault());
String formatted = simpleDateFormat.format(new Date());
我正在寻找一个相对日期,而不是日期格式! –
使用内置了被列入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
我已经说过我在我的问题中使用DataUtils。 –
我的图书馆[Time4A(https://github.com/MenoData/Time4A)提供的类[PrettyTime](http://time4j.net/javadoc-en/net/time4j/PrettyTime.html )约85种语言和不同的文字宽度。 –