2012-10-10 89 views
34

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

+0

您可以使用Java – keyser

+0

做到这一点,请展示你的代码,你已经做了... – Goofy

+0

@Keyser你能告诉我怎么做吗? – Manikandan

回答

28

你可以使用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以进一步了解。

+11

你确定它是正确的吗?你只比较日期,年和月呢?今天是20.11.2014,但你的代码显示20.10.2010 – Daryn

+2

是“今日”是@Daryn,不正确 –

+3

这个答案不正确。根据文档,Calendar.DATE是DAY_OF_MONTH的同义词。因此,你不是比较年份和月份。 –

0

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

+0

是iam将时间存储在数据库中。但我需要检查存储的时间是否是今天或昨天或昨天之前。 – Manikandan

+0

如果你存储的时间。那么为什么你不能存储他的约会呢? – Goofy

+0

是的,我可以,但我需要显示像“今天8:00”,就像在textview – Manikandan

136

要检查,如果今天的日期是,使用Android utils的库

DateUtils.isToday(long timeInMilliseconds) 

这utils的类还提供人类可读的字符串相对时间。例如,

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

的几个参数,你可以定义的时间跨度应该如何准确是

DateUtils

+4

DateUtils.isToday(myDate.getTime())工作正常,谢谢! – Loenix

+2

这是否仅消耗本地(非UTC)时间或UTC时间戳? – Matthias

+4

投票为Android的方式没有车轮改造! – ruX

1
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"; 
8

你可以试试这个玩:

Calendar mDate = Calendar.getInstance(); // just for example 
if (DateUtils.isToday(mDate.getTimeInMillis())) { 
    //format one way 
} else { 
    //format in other way 
} 
+0

工程就像一个魅力! –

+0

完美,像魅力作品 –

20

今天,您可以使用DateUtils.isTodayandroid API

昨天您可以使用代码:

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); 
} 
+0

@lujpo完美! – swooby

+0

上面的代码与now.get(Calendar.MONTH)似乎返回上个月!? – Tina

0

DateUtils.isToday()应该考虑废弃了,因为android.text.format.Time现在已经过时。 在他们更新isToday的源代码之前,在这里没有解决方案,昨天,今天检测到处理转换到/从夏令时转换,并且不使用已弃用的代码。这是在科特林,使用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,下去吧

1

投票这是获取值的方法像今天,昨天和日期一样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")); 
12

如前所述,DateUtils.isToday(d.getTime())将决定如果今天Date d是工作。但是这里的一些回应并没有真正回答如何确定一个日期是否是昨天。你也可以做到这一点很容易与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); 
} 
+0

这应该是被接受的答案。简单易读,且有效。唯一能让它更有效的方法是为毫秒时间戳写入方法,这样可以将它用于日历,日期或任何你喜欢的类。 – user1806772

相关问题