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

Man*_*dan 71 time android

我开发了一个发送短信的应用程序.Iam通过从数据库中检索时间来存储当前时间并显示在发送的历史记录页面中.在发送的历史记录页面中,我想显示发送消息的时间.在这里,我想检查消息是在今天或昨天或昨天之前发送的.如果昨天发送的消息意味着我需要显示"昨天20:00",即使消息是昨天发送的,也就是"周一20:00".我不知道该怎么做.如果有人知道,请帮助我.

Mar*_*ues 207

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

DateUtils.isToday(long timeInMilliseconds)
Run Code Online (Sandbox Code Playgroud)

此utils类还为相对时间提供人类可读的字符串.例如,

DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42 minutes ago"
Run Code Online (Sandbox Code Playgroud)

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

请参见DateUtils

  • 没有轮子重塑的安卓方式投票! (7认同)
  • DateUtils.isToday(myDate.getTime())工作正常,谢谢! (5认同)
  • `DateUtils.isToday(long millis)`的工作原理如@Maragues所述,但请注意,如果您在要进行单元测试的代码段中使用此方法(如ViewModel或Presenter),您将得到一个运行测试时出现RuntimeException.这是因为用于单元测试的android.jar不包含任何代码.欲了解更多信息[链接](http://g.co/androidstudio/not-mocked) (5认同)
  • 这是仅消耗本地(非UTC)时间还是UTC时间戳? (3认同)

Mat*_*aer 49

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

public static boolean isYesterday(Date d) {
    return DateUtils.isToday(d.getTime() + DateUtils.DAY_IN_MILLIS);
}
Run Code Online (Sandbox Code Playgroud)

之后,您还可以确定明天是否有日期:

public static boolean isTomorrow(Date d) {
    return DateUtils.isToday(d.getTime() - DateUtils.DAY_IN_MILLIS);
}
Run Code Online (Sandbox Code Playgroud)


Sud*_*pta 42

您可以使用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

请查看http://developer.android.com/reference/java/text/DateFormat.html以进一步了解.

  • 你确定它是正确的吗?您只比较日期,年和月的情况?今天是2014年11月20日,但您的代码显示2010年10月20日的"今天" (17认同)
  • 这个答案是不正确的.根据文档,Calendar.DATE是DAY_OF_MONTH的同义词.因此,您既不比较年份也不比较月份. (3认同)
  • 为什么这是最佳答案?这是不对的 (2认同)

luj*_*jop 22

对于今天你可以使用DateUtils.isToday来自Android的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);
}
Run Code Online (Sandbox Code Playgroud)


Rom*_*hev 9

你可以试试这个:

Calendar mDate = Calendar.getInstance(); // just for example
if (DateUtils.isToday(mDate.getTimeInMillis())) {
  //format one way
} else {
  //format in other way
}
Run Code Online (Sandbox Code Playgroud)


Кон*_*кин 9

如果您的 API 级别是 26 或更高,那么您最好使用 LocalDate 类:

fun isToday(whenInMillis: Long): Boolean {
    return LocalDate.now().compareTo(LocalDate(whenInMillis)) == 0
}

fun isTomorrow(whenInMillis: Long): Boolean {
    return LocalDate.now().plusDays(1).compareTo(LocalDate(whenInMillis)) == 0
}

fun isYesterday(whenInMillis: Long): Boolean {
    return LocalDate.now().minusDays(1).compareTo(LocalDate(whenInMillis)) == 0
}
Run Code Online (Sandbox Code Playgroud)

如果您的应用具有较低的 API 级别,请使用

fun isToday(whenInMillis: Long): Boolean {
    return DateUtils.isToday(whenInMillis)
}

fun isTomorrow(whenInMillis: Long): Boolean {
    return DateUtils.isToday(whenInMillis - DateUtils.DAY_IN_MILLIS)
}

fun isYesterday(whenInMillis: Long): Boolean {
    return DateUtils.isToday(whenInMillis + DateUtils.DAY_IN_MILLIS)
} 
Run Code Online (Sandbox Code Playgroud)


Aru*_*ani 8

Blow 片段在回收器视图标题部分很有用。

科特林扩展:

fun Date.isYesterday(): Boolean = DateUtils.isToday(this.time + DateUtils.DAY_IN_MILLIS)

fun Date.isToday(): Boolean = DateUtils.isToday(this.time)


fun Date.toDateString(): String {
   return when {
    this.isToday() -> {
        "Today"
    }
    this.isYesterday() -> {
        "Yesterday"
    }
    else -> {
        convetedDate.format(this)
    }
 }
}
Run Code Online (Sandbox Code Playgroud)


Cho*_*ski 7


没有使用库


昨天

今天

明天

今年

任何一年

 public static String getMyPrettyDate(long neededTimeMilis) {
    Calendar nowTime = Calendar.getInstance();
    Calendar neededTime = Calendar.getInstance();
    neededTime.setTimeInMillis(neededTimeMilis);

    if ((neededTime.get(Calendar.YEAR) == nowTime.get(Calendar.YEAR))) {

        if ((neededTime.get(Calendar.MONTH) == nowTime.get(Calendar.MONTH))) {

            if (neededTime.get(Calendar.DATE) - nowTime.get(Calendar.DATE) == 1) {
                //here return like "Tomorrow at 12:00"
                return "Tomorrow at " + DateFormat.format("HH:mm", neededTime);

            } else if (nowTime.get(Calendar.DATE) == neededTime.get(Calendar.DATE)) {
                //here return like "Today at 12:00"
                return "Today at " + DateFormat.format("HH:mm", neededTime);

            } else if (nowTime.get(Calendar.DATE) - neededTime.get(Calendar.DATE) == 1) {
                //here return like "Yesterday at 12:00"
                return "Yesterday at " + DateFormat.format("HH:mm", neededTime);

            } else {
                //here return like "May 31, 12:00"
                return DateFormat.format("MMMM d, HH:mm", neededTime).toString();
            }

        } else {
            //here return like "May 31, 12:00"
            return DateFormat.format("MMMM d, HH:mm", neededTime).toString();
        }

    } else {
        //here return like "May 31 2010, 12:00" - it's a different year we need to show it
        return DateFormat.format("MMMM dd yyyy, HH:mm", neededTime).toString();
    }
}
Run Code Online (Sandbox Code Playgroud)


Jac*_*per 5

另一种方法来做到这一点。在kotlin 中使用推荐的 lib ThreeTen

  1. 加三十

    implementation 'com.jakewharton.threetenabp:threetenabp:1.1.0'
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加 kotlin 扩展。

    fun LocalDate.isYesterday(): Boolean = this.isEqual(LocalDate.now().minusDays(1L))
    
    fun LocalDate.isToday(): Boolean = this.isEqual(LocalDate.now())
    
    Run Code Online (Sandbox Code Playgroud)


Gas*_*lén 5

科特林

@Choletski 解决方案,但有几秒钟和 Kotlin

 fun getMyPrettyDate(neededTimeMilis: Long): String? {
        val nowTime = Calendar.getInstance()
        val neededTime = Calendar.getInstance()
        neededTime.timeInMillis = neededTimeMilis
        return if (neededTime[Calendar.YEAR] == nowTime[Calendar.YEAR]) {
            if (neededTime[Calendar.MONTH] == nowTime[Calendar.MONTH]) {
                if (neededTime[Calendar.DATE] - nowTime[Calendar.DATE] == 1) {
                    //here return like "Tomorrow at 12:00"
                    "Tomorrow at " + DateFormat.format("HH:mm:ss", neededTime)
                } else if (nowTime[Calendar.DATE] == neededTime[Calendar.DATE]) {
                    //here return like "Today at 12:00"
                    "Today at " + DateFormat.format("HH:mm:ss", neededTime)
                } else if (nowTime[Calendar.DATE] - neededTime[Calendar.DATE] == 1) {
                    //here return like "Yesterday at 12:00"
                    "Yesterday at " + DateFormat.format("HH:mm:ss", neededTime)
                } else {
                    //here return like "May 31, 12:00"
                    DateFormat.format("MMMM d, HH:mm:ss", neededTime).toString()
                }
            } else {
                //here return like "May 31, 12:00"
                DateFormat.format("MMMM d, HH:mm:ss", neededTime).toString()
            }
        } else {
            //here return like "May 31 2010, 12:00" - it's a different year we need to show it
            DateFormat.format("MMMM dd yyyy, HH:mm:ss", neededTime).toString()
        }
    }
Run Code Online (Sandbox Code Playgroud)

你可以通过这里date.getTime()得到输出

Today at 18:34:45
Yesterday at 12:30:00
Tomorrow at 09:04:05
Run Code Online (Sandbox Code Playgroud)