Android in date write today yesterday 2 days ago like that

后端 未结 8 2110
悲哀的现实
悲哀的现实 2021-01-02 17:03

I need to print date like today,Yesterday,2 days ago like that for that i have done I am getting date like : String date1 = \"Thu Nov 13 19:01:25 GMT+05:30 2014\";

8条回答
  •  借酒劲吻你
    2021-01-02 17:22

    In you DateTimeFormatter, it expects a Date in a certain format, i.e. "EEE hh:mma MMM d, yyyy":

    DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
                .parseDateTime(date);
    

    But what you pass, is of format "MM/dd/yyyy":

    SimpleDateFormat outputFormat1 = new SimpleDateFormat("MM/dd/yyyy");
    op = outputFormat1.format(d);
    op = formatToYesterdayOrToday(op);
    

    So, what you could do is to change your DateTimeFormatter to expect you initial format "MM/dd/yyyy":

    DateTime dateTime = DateTimeFormat.forPattern("MM/dd/yyyy")
                .parseDateTime(date);
    

    This should solve the error that you get. I don't know if the whole thing in the end gives you what you want ("today", "yesterday", "2 days ago", ...).

提交回复
热议问题