我只是插入并使用示例代码来获取聊天/消息传递应用程序。我正在使用一个gettimedate函数,在我的模型类中我有以下内容:
private volatile long mTimedate;
public myclass(....,DateType date,...){
mTimedate = date != null ? date.time : new Date().getTime();
}
public final long getTimedate() {
return mTimedate;
}
public int hashCode() {
if (mId != null) {
return mId.hashCode();
}
int hash = mBody == null ? 0 : mBody.hashCode();
return 31 * hash + (new Long(mTimedate)).hashCode();
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof myclass)) {
return false;
}
myclass other = (myclass) obj;
return mId != null ? mId.equals(other.mId) : other.mId == null
&& (mBody == null ? other.mBody == null : mBody.equals(other.mBody))
&& mTimedate == other.mdate;
}
现在,我想知道,我如何使用这个时间日期函数,这样我可以从我的视图顶部提取“月 - 日,年”,并从同一函数中提取底部“12小时格式的时间” gettimedate()。
这是我使用此功能进行显示的适配器:
holder.txtInfo.setText(String.valueOf(new Date(msg.getTimedate())));
返回整个包裹,如“Mon Oct 19,13:03:03 EDT 2015” 但我只需要将它用于不同的视图。我有什么想法可以在两个不同的地方使用它吗?
P.S:对于粗略的工作代码示例,这是一个示例代码: http://www.codeproject.com/Tips/897826/Designing-Android-Chat-Bubble-Chat-UI
另外,我想修改这个测试代码,看看我是否只能在会话当天显示一次日期,而每次会话只显示12小时时间格式,例如(下午4:35) ,就像在任何标准聊天中一样。无论如何使用上面的代码吗?
谢谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
使用SimpleDateFormat类。
示例:
public String getDateTimeStringWithSeconds(long time) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d/M-yyyy - HH:mm:ss");
return simpleDateFormat.format(calendar.getTime());
}
这将返回一个字符串,如“20 / 10-2015 - 00:58:33” 但它的印刷方式几乎可以改为任何东西,它都在SimpleDateFormat documentation。
答案 2 :(得分:0)
使用此:
Date date = new Date();
SimpleDateFormat tryDateFormat = new SimpleDateFormat("EEEE MMMM-dd-yyyy");
String formattedDate = tryDateFormat.format(date);
System.out.println(formattedDate);
打印此
Tuesday October-20-2015