Best way to format a date relative to now on Android

前端 未结 10 1690
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 15:57

I am creating a feature in an Android app to get an arbitrary date (past, present or future) and find the difference relative to now.

Both my now and

相关标签:
10条回答
  • 2021-01-31 16:36

    The actual reason is the number 864000 is in miliseconds, which corresponds to 14 minutes. 14 minutes is so small compared to DAY_IN_MILLIS (a day). There for you get "in 0 days". If you want it to produce "in 14 mins", just change DAY_IN_MILLIS to MIN_IN_MILLIS.

    0 讨论(0)
  • 2021-01-31 16:37

    What I have in mind is changing:

    DateUtils.getRelativeTimeSpanString(due, now, 0L, DateUtils.FORMAT_ABBREV_ALL);

    Since the documentation says it returns the time relative to now.

    If that fails use some of the brilliant libraries:

    Joda Time

    PrettyTime

    TimeAgo

    0 讨论(0)
  • 2021-01-31 16:39

    for Android you can use most simple way with Joda-Time-Android library:

    Date yourTime = new Date();
    DateTime dateTime = new DateTime(yourTime); //or simple DateTime.now()
    final String result = DateUtils.getRelativeTimeSpanString(getContext(), dateTime);
    
    0 讨论(0)
  • 2021-01-31 16:43

    build.gradle

    compile 'joda-time:joda-time:2.9.9'
    

    Utils.java

    private static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM dd, yyyy");
        private static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat(" 'at' h:mm aa");
        public static String getRelativeDateTimeString(Calendar startDateCalendar) {
            if (startDateCalendar == null) return null;
    
            DateTime startDate = new DateTime(startDateCalendar.getTimeInMillis());
            DateTime today = new DateTime();
            int days = Days.daysBetween(today.withTimeAtStartOfDay(), startDate.withTimeAtStartOfDay()).getDays();
    
            String date;
            switch (days) {
                case -1: date = "Yesterday"; break;
                case 0: date = "Today"; break;
                case 1: date = "Tomorrow"; break;
                default: date = DATE_FORMAT.format(startDateCalendar.getTime()); break;
            }
            String time = TIME_FORMAT.format(startDateCalendar.getTime());
            return date + time;
        }
    

    Output

    Yesterday at 9:52 AM
    Today at 9:52 AM
    Tomorrow at 9:52 AM
    Sep 05, 2017 at 9:52 AM
    
    0 讨论(0)
  • 2021-01-31 16:43

    I came here for an alternative but I can't find perfect rather than my code. So I shared here any improvements are welcome.

    public String getCreatedAtRelative() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
        df.setTimeZone(TimeZone.getTimeZone("IST"));
        CharSequence relative = null;
        try {
            relative = DateUtils.getRelativeTimeSpanString(df.parse(createdAt).getTime(), new Date().getTime(),
                    0L, DateUtils.FORMAT_ABBREV_ALL);
        } catch (ParseException e) {
            Log.e("Parse Exception adapter", "created at", e);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        if (null == relative) {
            return createdAt;
        } else {
            return relative.toString().replace(".", " ");
        }
    }
    
    0 讨论(0)
  • 2021-01-31 16:46

    Best way to format a date relative to now on Android

    I suggest you to use JodaTime

    It's lightweight handy library and i think actually the best tool for working with Date instances.

    And you can start here.

    0 讨论(0)
提交回复
热议问题