アンドロイド - 日付形式前の時間に変換する文字列の日付

Nitesh:

どのように変換するStringように前の時間、日付形式で日付をyyyy-dd-mm hh:ss:mmその手段前の時間、day , month , years , hours

 private String CalculateDatefromcurrentdate(String ago_date, String currant_date) {
        String setalldate = "";
        try {
            SimpleDateFormat convert_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(convert_date.parse(ago_date));
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(convert_date.parse(currant_date));
            long millis1 = calendar1.getTimeInMillis();
            long millis2 = calendar2.getTimeInMillis();
            long diff = millis2 - millis1;
            // Calculate difference in seconds
            long diffSeconds = diff / 1000;
            // Calculate difference in minutes
            long diffMinutes = diff / (60 * 1000);
            // Calculate difference in hours
            long diffHours = diff / (60 * 60 * 1000);
            // Calculate difference in days
            long diffDays = diff / (24 * 60 * 60 * 1000);
            // calculate how much month in days
            long multipledays = diffDays;
            int maonths = (int) multipledays / 30;
            int reamainigdays = maonths % 30;
            // Calculate  the years of monthes
            int getyears = maonths / 12;
            int reamainigmothe = getyears % 12;

            if (diffSeconds < diff / 1000) {
                setalldate = diffSeconds + "Second ago";
            } else if (diffMinutes < diff / (60 * 1000)) {
                setalldate = diffMinutes + "minutes ago";
            } else if (diffHours < diff / (60 * 60 * 1000) ) {
                setalldate = diffHours + "hr ago";
            } else if (diffDays <  diff / (24 * 60 * 60 * 1000)) {
                setalldate = diffDays + "day ago";
            } else if (reamainigdays < maonths % 30) {
                setalldate = reamainigdays + "months ago";
            } else if (reamainigmothe < getyears % 12) {
                setalldate = reamainigmothe + "years ago";
            }

            System.out.println("In milliseconds: " + diff + " milliseconds.");
            System.out.println("In seconds: " + diffSeconds + " seconds.");
            System.out.println("In minutes: " + diffMinutes + " minutes.");
            System.out.println("In hours: " + diffHours + " hours.");
            System.out.println("In days: " + diffDays + " days.");
            System.out.println("In days: " + reamainigdays + " months.");
            System.out.println("In days: " + reamainigdays + " reamaingdays.");
            System.out.println("In days: " + getyears + " years.");
            System.out.println("In days: " + reamainigmothe + " month in this years.");
//            Toast.makeText(this, "days=" + daysDiff, Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return setalldate;
    }
ラケッシュ・クマール:

あなたの既存の以下で、この次の操作を実行できます。

String convTime = null;
        try {

            String ago_date = "2020-01-14 12:52:00",  currant_date = "2020-01-18 12:52:45";

            String suffix = "Ago";

            SimpleDateFormat convert_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(convert_date.parse(ago_date));
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(convert_date.parse(currant_date));
            long millis1 = calendar1.getTimeInMillis();
            long millis2 = calendar2.getTimeInMillis();
            long diff = millis2 - millis1;
            // Calculate difference in seconds
            long diffSeconds = diff / 1000;
            // Calculate difference in minutes
            long diffMinutes = diff / (60 * 1000);
            // Calculate difference in hours
            long diffHours = diff / (60 * 60 * 1000);
            // Calculate difference in days
            long diffDays =  diff / (24 * 60 * 60 * 1000);
            // calculate how much month in days
            long multipledays = diffDays;
            int maonths = (int) multipledays / 30;
            int reamainigdays = maonths % 30;
            // Calculate  the years of monthes
            int getyears = maonths / 12;
            int reamainigmothe = getyears % 12;
            System.out.println("In calculated: ");

            if (diffSeconds<60)  {
                convTime = diffSeconds+" Seconds "+suffix;
            } else if (diffMinutes < 60) {
                convTime = diffMinutes+" Minutes "+suffix;
            } else if (diffHours < 24) {
                convTime = diffHours+" Hours "+suffix;
            } else if (diffDays >= 7) {
                if (reamainigmothe!=0 && reamainigmothe <= getyears % 12) {
                    convTime = reamainigmothe + " Years " + suffix;
                } else if (diffDays > 30) {
                    convTime = maonths + " Months " + suffix;
                } else {
                    convTime = (diffDays / 7) + " Week " + suffix;
                }
            } else if (diffDays < 7) {
                convTime = diffDays+" Days "+suffix;
            }

            System.out.println("In calculated: 222--"+convTime );

        } catch (Exception e) {
            e.printStackTrace();
        }

どこでago_date過去の日付となりcurrant_dateます動作します現在の日付と交換する必要があります。

String ago_date = "2020-01-14 12:52:00",  currant_date = "2020-01-18 12:52:45";

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=363288&siteId=1