Simplify replacement of date object with “today” and “yesterday” strings in Java static method

后端 未结 9 2188
鱼传尺愫
鱼传尺愫 2020-12-30 05:55

I have following method that I would like to make shorter or faster if nothing else. Please all comments are welcome:

Bellow method takes a date object, formates i

9条回答
  •  北海茫月
    2020-12-30 06:21

    I know I am late to this party. But I have shortest solution for this problem. If you want to show "Today" or "Yesterday" based on the Date then you just need to use this

    String strDate = "";
    if (DateUtils.isToday(date.getTime()))
        strDate = "Today";
    else if (DateUtils.isToday(date.getTime() + DateUtils.DAY_IN_MILLIS))
        strDate = "Yesterday";
    

    here variable date is the Date object.

提交回复
热议问题