Android in date write today yesterday 2 days ago like that

后端 未结 8 2156
悲哀的现实
悲哀的现实 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:31

    Imagine you have data like this:

    {"data":[{"id":"79tiyfjgdfg","sales_name":"Sengkuni","sales_branch":"Kingdom of Hastina","message":"Leader of Kurowo sent you a gold","timestamp":"2020-03-23 10:16:01"}]}
    

    and call function like this:

    holder.timestamp.setText(Utils.beautifyDate(context, notifications.get(position).getTimestamp(), "EEEE, MMMM d, YYYY"));
    

    for another format you can check from https://developer.android.com/reference/java/text/SimpleDateFormat

    Use this function below

    public static String beautifyDate(Context context, String timestamp, String formatDate) {
    
        String beautifyFormat;
    
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-M-dd H:mm:ss", Locale.getDefault());
        Date date = new Date();
    
        try {
            date = fmt.parse(timestamp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        String mYear = new SimpleDateFormat("yyyy", Locale.getDefault()).format(date);
        String mMonth = new SimpleDateFormat("M", Locale.getDefault()).format(date);
        String mDay = new SimpleDateFormat("dd", Locale.getDefault()).format(date);
        String mHour = new SimpleDateFormat("H", Locale.getDefault()).format(date);
        String mMinutes = new SimpleDateFormat("mm", Locale.getDefault()).format(date);
    
        Calendar now = Calendar.getInstance();
        int years = now.get(Calendar.YEAR);
        int months = now.get(Calendar.MONTH) + 1;
        int days = now.get(Calendar.DATE);
        int hours = now.get(Calendar.HOUR_OF_DAY);
        int minutes = now.get(Calendar.MINUTE);
    
        if (mYear.equals(String.valueOf(years)) && mMonth.equals(String.valueOf(months)) && mDay.equals(String.valueOf(days))) {
            Log.i("timestamp", "on same day");
            hours -= Integer.parseInt(mHour);
    
            beautifyFormat = hours > 1 ? hours + " " + context.getString(R.string.hours_ago): hours + " " + context.getString(R.string.hour_ago);
    
            if (hours == 0) {
                minutes -= Integer.parseInt(mMinutes);
                beautifyFormat = minutes > 1 ? minutes + " " + context.getString(R.string.minutes_ago): context.getString(R.string.moments_ago);
            }
        } else {
            days = now.get(Calendar.DATE) - Integer.parseInt(mDay);
    
            if (days == 1) {
                beautifyFormat = context.getString(R.string.tomorrow);
            } else {
                beautifyFormat = new SimpleDateFormat(formatDate, Locale.getDefault()).format(date);
            }
        }
    
        return beautifyFormat;
    }
    

    simply adjust the time you want to be able to see results like today or tomorrow and others

    Hope it is useful.

提交回复
热议问题