Right way to format date with strings like today, yesterday, tomorrow etc

前端 未结 6 576
臣服心动
臣服心动 2021-02-04 00:31

I have a date textview. And my textview holds a date string like 2011.09.17. Well I still do want to have that but I also want to add some more user friendly info for some speci

6条回答
  •  春和景丽
    2021-02-04 01:02

    public class RelativeWeekday {                
        private final Calendar mCalendar;                
    
        public RelativeWeekday(Calendar calendar) {                
            mCalendar = calendar;                
        }                
    
        @Override                
        public String toString() {                
            Calendar today = Calendar.getInstance(Locale.getDefault());
            int dayOfYear = mCalendar.get(Calendar.DAY_OF_YEAR);
            if (Math.abs(dayOfYear - today.get(Calendar.DAY_OF_YEAR)) < 2) {
                return getRelativeDay(today);
            }              
    
            return getWeekDay();                
        }                
    
        private String getRelativeDay(Calendar today) {                
            return DateUtils.getRelativeTimeSpanString(                
                    mCalendar.getTimeInMillis(),                
                    today.getTimeInMillis(),                
                    DateUtils.DAY_IN_MILLIS,                
                    DateUtils.FORMAT_SHOW_WEEKDAY).toString();                
        }                
    
        private String getWeekDay() {                
            SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE");                
            return dayFormat.format(mCalendar.getTimeInMillis());                
        }                
    }
    

提交回复
热议问题