如何找到时间是今天或昨天在android

Iam开发一个发送短信的应用程序。 Iam通过从数据库中检索时间来存储当前时间并显示在发送的历史页面中。 在发送的历史记录页面中,我想显示发送消息的时间。 在这里,我想检查一下,这个消息是今天还是昨天还是昨天之前发送的。 如果邮件昨天发送意味着那么我需要显示“昨天20:00”这样,甚至信息昨天发送意味着“星期一20:00”。 我不知道该怎么做。 如果有人知道,请帮助我。

你可以使用android.text.format.DateFormat类轻松地做到这一点。 尝试这样的事情。

public String getFormattedDate(Context context, long smsTimeInMilis) { Calendar smsTime = Calendar.getInstance(); smsTime.setTimeInMillis(smsTimeInMilis); Calendar now = Calendar.getInstance(); final String timeFormatString = "h:mm aa"; final String dateTimeFormatString = "EEEE, MMMM d, h:mm aa"; final long HOURS = 60 * 60 * 60; if (now.get(Calendar.DATE) == smsTime.get(Calendar.DATE) ) { return "Today " + DateFormat.format(timeFormatString, smsTime); } else if (now.get(Calendar.DATE) - smsTime.get(Calendar.DATE) == 1 ){ return "Yesterday " + DateFormat.format(timeFormatString, smsTime); } else if (now.get(Calendar.YEAR) == smsTime.get(Calendar.YEAR)) { return DateFormat.format(dateTimeFormatString, smsTime).toString(); } else { return DateFormat.format("MMMM dd yyyy, h:mm aa", smsTime).toString(); } } 

查看http://developer.android.com/reference/java/text/DateFormat.html以获得进一步的理解。

要检查日期是否是今天,请使用Android utils库

 DateUtils.isToday(long timeInMilliseconds) 

这个工具类还为相对时代提供了可读的字符串。 例如,

 DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42 minutes ago" 

您可以使用几个参数来定义时间跨度的精确度

见DateUtils

今天你可以使用android API的 DateUtils.isToday

对于昨天你可以使用该代码:

 public static boolean isYesterday(long date) { Calendar now = Calendar.getInstance(); Calendar cdate = Calendar.getInstance(); cdate.setTimeInMillis(date); now.add(Calendar.DATE,-1); return now.get(Calendar.YEAR) == cdate.get(Calendar.YEAR) && now.get(Calendar.MONTH) == cdate.get(Calendar.MONTH) && now.get(Calendar.DATE) == cdate.get(Calendar.DATE); } 

如上所述, DateUtils.isToday(d.getTime())将用于确定Date d是否为今天。 但是这里的一些回答实际上并没有回答如何确定一个日期是否是昨天。 您也可以使用DateUtils轻松完成DateUtils

 public static boolean isYesterday(Date d) { return DateUtils.isToday(d.getTime() + DateUtils.DAY_IN_MILLIS); } 

之后,你也可以确定一个日期是否是明天:

 public static boolean isTomorrow(Date d) { return DateUtils.isToday(d.getTime() - DateUtils.DAY_IN_MILLIS); } 

你可以试试这个:

 Calendar mDate = Calendar.getInstance(); // just for example if (DateUtils.isToday(mDate.getTimeInMillis())) { //format one way } else { //format in other way } 
  Calendar now = Calendar.getInstance(); long secs = (dateToCompare - now.getTime().getTime()) / 1000; if (secs > 0) { int hours = (int) secs / 3600; if (hours <= 24) { return today + "," + "a formatted day or empty"; } else if (hours <= 48) { return yesterday + "," + "a formatted day or empty"; } } else { int hours = (int) Math.abs(secs) / 3600; if (hours <= 24) { return tommorow + "," + "a formatted day or empty"; } } return "a formatted day or empty"; 

我可以建议你一件事。 当你发送短信存储到数据库的细节,以便你可以显示在历史页​​面上发送短信的日期和时间。

DateUtils.isToday()应被视为弃用,因为android.text.format.Time现在已被弃用。 在他们更新isToday的源代码之前,在这里没有解决方案,昨天,这个解决方案处理夏令时转换,并且不使用已弃用的代码。 这里是在Kotlin,使用today领域,必须定期保持最新(例如onResume等):

 @JvmStatic fun dateString(ctx: Context, epochTime: Long): String { val epochMS = 1000*epochTime val cal = Calendar.getInstance() cal.timeInMillis = epochMS val yearDiff = cal.get(Calendar.YEAR) - today.get(Calendar.YEAR) if (yearDiff == 0) { if (cal.get(Calendar.DAY_OF_YEAR) >= today.get(Calendar.DAY_OF_YEAR)) return ctx.getString(R.string.today) } cal.add(Calendar.DATE, 1) if (cal.get(Calendar.YEAR) == today.get(Calendar.YEAR)) { if (cal.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) return ctx.getString(R.string.yesterday) } val flags = if (yearDiff == 0) DateUtils.FORMAT_ABBREV_MONTH else DateUtils.FORMAT_NUMERIC_DATE return DateUtils.formatDateTime(ctx, epochMS, flags) } 

我提交了https://code.google.com/p/android/issues/detail?id=227694&thanks=227694&ts=1479155729 ,对其投票

这是获取像今天,昨天和Whtsapp应用程序的日期值的方法

 public String getSmsTodayYestFromMilli(long msgTimeMillis) { Calendar messageTime = Calendar.getInstance(); messageTime.setTimeInMillis(msgTimeMillis); // get Currunt time Calendar now = Calendar.getInstance(); final String strTimeFormate = "h:mm aa"; final String strDateFormate = "dd/MM/yyyy h:mm aa"; if (now.get(Calendar.DATE) == messageTime.get(Calendar.DATE) && ((now.get(Calendar.MONTH) == messageTime.get(Calendar.MONTH))) && ((now.get(Calendar.YEAR) == messageTime.get(Calendar.YEAR))) ) { return "today at " + DateFormat.format(strTimeFormate, messageTime); } else if ( ((now.get(Calendar.DATE) - messageTime.get(Calendar.DATE)) == 1) && ((now.get(Calendar.MONTH) == messageTime.get(Calendar.MONTH))) && ((now.get(Calendar.YEAR) == messageTime.get(Calendar.YEAR))) ) { return "yesterday at " + DateFormat.format(strTimeFormate, messageTime); } else { return "date : " + DateFormat.format(strDateFormate, messageTime); } } 

使用这种方法只是传递毫秒

  getSmsTodayYestFromMilli(Long.parseLong("1485236534000"));