如何在通知扩展视图中获取自定义日期格式

3
如果我收到了一条新消息,在通知展开视图中只会显示时间。例如,如果我今天(即2010年6月6日)收到一条消息,则应仅显示字符串“今天”,明天它应该显示接收消息的日期,即应该显示日期06/06/2010。

请勿打“与编程无关”标签。@quintin - Roger Pate
2个回答

0

如果您有以毫秒为单位的日期,例如:

long myDate = Calendar.getInstance().getTimeInMillis();

否则,您可以获取日历实例并设置时间。
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern(“MM/dd/yyyy”);
Calendar calendar = Calendar.getInstance();
Date processDate = sdf.parse(“4/10/2007″);
calendar.setTime(processDate);
long myDate = calendar.getTimeInMillis();

然后你可以将你的日期与系统日期相减

String dateMessage = "today";
final int dayInterval = 86400000;
long difference = Calendar.getInstance().getTimeInMillis() - myDateInMillis;
if (difference > dayInterval)
    //dateMessage = format date MM/DD/YYYY...

从这里开始,您可以更新正在进行的通知以显示预期日期值。请参阅创建自定义扩展视图的开发人员文档。

关于更新通知的说明。如果您有自定义的扩展视图,则可以使用Remoteview访问器更新TextViews。上面的链接描述了如何做到这一点。

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setTextViewText(R.id.text, mTitle);

希望这能有所帮助。

0

您需要编写一个自定义的DateFormat对象,然后调用DateFormat.format(Date)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接