use*_*376 4 java android simpledateformat android-dateutils
如何在 android 开发中自定义日期格式,使其类似于 twitter 和 instagram。我在当前代码下面有什么,但我不喜欢它产生的格式,如“11 分钟前”或“34 分钟前”。我更喜欢 Twitter 格式,如“11m”或“34m”。请有人知道我如何格式化我的日期吗?
Date createdAt = message.getCreatedAt();//get the date the message was created from parse backend
long now = new Date().getTime();//get current date
String convertedDate = DateUtils.getRelativeTimeSpanString(
createdAt.getTime(), now, DateUtils.SECOND_IN_MILLIS).toString();
mPostMessageTimeLabel.setText(convertedDate); //sets the converted date into the message_item.xml view
Run Code Online (Sandbox Code Playgroud)
有同样的问题。与其使用库,我想我可能会编写自己的版本,并让它更容易理解正在发生的事情(并且可以在需要时稍微调整一下)。
这是我制作的实用方法(Log包括对 Android 用户进行测试的有用声明):
public static String convertLongDateToAgoString (Long createdDate, Long timeNow){
Long timeElapsed = timeNow - createdDate;
// For logging in Android for testing purposes
/*
Date dateCreatedFriendly = new Date(createdDate);
Log.d("MicroR", "dateCreatedFriendly: " + dateCreatedFriendly.toString());
Log.d("MicroR", "timeNow: " + timeNow.toString());
Log.d("MicroR", "timeElapsed: " + timeElapsed.toString());*/
// Lengths of respective time durations in Long format.
Long oneMin = 60000L;
Long oneHour = 3600000L;
Long oneDay = 86400000L;
Long oneWeek = 604800000L;
String finalString = "0sec";
String unit;
if (timeElapsed < oneMin){
// Convert milliseconds to seconds.
double seconds = (double) ((timeElapsed / 1000));
// Round up
seconds = Math.round(seconds);
// Generate the friendly unit of the ago time
if (seconds == 1) {
unit = "sec";
} else {
unit = "secs";
}
finalString = String.format("%.0f", seconds) + unit;
} else if (timeElapsed < oneHour) {
double minutes = (double) ((timeElapsed / 1000) / 60);
minutes = Math.round(minutes);
if (minutes == 1) {
unit = "min";
} else {
unit = "mins";
}
finalString = String.format("%.0f", minutes) + unit;
} else if (timeElapsed < oneDay) {
double hours = (double) ((timeElapsed / 1000) / 60 / 60);
hours = Math.round(hours);
if (hours == 1) {
unit = "hr";
} else {
unit = "hrs";
}
finalString = String.format("%.0f", hours) + unit;
} else if (timeElapsed < oneWeek) {
double days = (double) ((timeElapsed / 1000) / 60 / 60 / 24);
days = Math.round(days);
if (days == 1) {
unit = "day";
} else {
unit = "days";
}
finalString = String.format("%.0f", days) + unit;
} else if (timeElapsed > oneWeek) {
double weeks = (double) ((timeElapsed / 1000) / 60 / 60 / 24 / 7);
weeks = Math.round(weeks);
if (weeks == 1) {
unit = "week";
} else {
unit = "weeks";
}
finalString = String.format("%.0f", weeks) + unit;
}
return finalString;
}
Run Code Online (Sandbox Code Playgroud)
用法:
Long createdDate = 1453394736888L; // Your Long
Long timeNow = new Date().getTime();
Log.d("MicroR", convertLongDateToAgoString(createdDate, timeNow));
// Outputs:
// 1min
// 3weeks
// 5hrs
// etc.
Run Code Online (Sandbox Code Playgroud)
随意测试一下,如果您发现任何问题,请告诉我!
我可能有点晚了,但我把它写下来供那些正在寻找解决方案的人使用。使用PrettyTime您可以获得格式化日期,例如“2 个月前”等。为了满足您的需求,您必须为其提供自定义TimeFormat对象,无需创建新TimeUnit对象,因为我们正在格式化正常时间单位。要做到这一点,只需创建您的TimeFormat对象几分钟,例如:
public class CustomMinuteTimeFormat implements TimeFormat {
@Override
public String format(Duration duration) {
return Math.abs(duration.getQuantity()) + "m";
}
@Override
public String formatUnrounded(Duration duration) {
return format(duration);
}
@Override
public String decorate(Duration duration, String time) {
return time;
}
@Override
public String decorateUnrounded(Duration duration, String time) {
return time;
}
}
Run Code Online (Sandbox Code Playgroud)
然后实例化一个新PrettyTime实例并设置格式化程序。
PrettyTime pretty = new PrettyTime();
//This line of code is very important
pretty.registerUnit(new Minute(), new CustomMinuteTimeFormat());
//Use your PrettyTime object as usual
pretty.format(yourDateObject);
Run Code Online (Sandbox Code Playgroud)
如果经过的时间为 2 分钟,则输出“2m”。