Best way to format a date relative to now on Android

前端 未结 10 1887
隐瞒了意图╮
隐瞒了意图╮ 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:49

    So your computation is based on milliseconds unit, then you format the result with SimpleDateFormat.

    For this, you can easily use SimpleDateFormat formatter like this :

    Date date = new Date(milliseconds);
    SimpleDateFormat formatter = new SimpleDateFormat("EEEE dd MMMM yyyy");
    String strDate = formatter.format(date);
    

    So your computation should be based on milliseconds unit, then you format the result with SimpleDateFormat.

    The pattern ("EEEE dd MMMM yyyy") allows you to get a date format like Monday, 04 February 2013.

    You can change the pattern as you like : "EEEE dd/MM/yy", ...

提交回复
热议问题