3

所以我试图减去我想出的这段代码的 2 个日期:

java.text.DateFormat df = new java.text.SimpleDateFormat("hh:mm:ss_yyyy.MM.dd");
java.util.Date date1 = new java.util.Date();
java.util.Date date2 = df.parse("00:00:00_2013.01.01");
long diff = date2.getTime() - date1.getTime();

但问题是差异不正确。

谁能告诉我我做错了什么?

4

2 回答 2

8

您在评论中的差异示例是错误的。我尝试了您的代码并得到了以下结果:

try {
    DateFormat df = new SimpleDateFormat("hh:mm:ss_yyyy.MM.dd");
    Date date1 = new java.util.Date();
    Date date2 = df.parse("00:00:00_2013.01.01");
    long diff = date2.getTime() - date1.getTime();
    Log.e("TEST" , date1.getTime() + " - " + date2.getTime() + " - " + diff);
} catch (ParseException e) {
    Log.e("TEST", "Exception", e);
}

date1: 1350921506492
date2: 1356994800000
diff: 6073293508

diff / 1000 / 60 / 60 / 24 = 70,292748935

并大致检查了 70 天,直到新年听起来很稳定。

于 2012-10-22T16:01:30.503 回答
3

希望这会有所帮助

如果日期差异小于一天,则下面的代码输出时间,当日期差异小于 7 天时输出日期,依此类推....

public String ConvertDateToReadableDate(String DateTime) {
    if (DateTime != null) {
        if (!DateTime.equals("")) {
            // the input should be in this format 2019-03-08 15:14:29 
            //if not you have to change the pattern in SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
            
                Date newDate;
                Date currentDate = new java.util.Date();
                int hour = 0, min = 0, sec = 0;
                String dayName = "", dayNum = "", monthName = "", year = "";
                long numOfMilliSecondPassed = 0;
                float milliSecond = 86400000.0f; // 1 day is 86400000 milliseconds 
                float numOfDayPass;

                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

                try {
                    newDate = dateFormat.parse(DateTime); // convert String to date
                    numOfMilliSecondPassed = currentDate.getTime() - newDate.getTime(); //get the difference in date in millisecond

                    hour = Integer.parseInt((String) android.text.format.DateFormat.format("hh", newDate));
                    min = Integer.parseInt((String) android.text.format.DateFormat.format("mm", newDate));
                    sec = Integer.parseInt((String) android.text.format.DateFormat.format("ss", newDate));
                    dayName = (String) android.text.format.DateFormat.format("EEEE", newDate);
                    dayNum = (String) android.text.format.DateFormat.format("dd", newDate);
                    monthName = (String) android.text.format.DateFormat.format("MMM", newDate);
                    year = (String) android.text.format.DateFormat.format("yyyy", newDate);


                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //Convert the milliseconds to days
                numOfDayPass = (numOfMilliSecondPassed / milliSecond);       


                if (numOfDayPass < 1) {
                    return hour + ":" + min + ":" + sec;
                } else if ((numOfDayPass >= 1) && (numOfDayPass < 7)) {
                    return dayName + " "+ hour + ":" + min + ":" + sec;
                } else if ((numOfDayPass >= 7) && (numOfDayPass < 30)) {
                    int weeks = (int) numOfDayPass / 7;

                    if(weeks > 1) {
                        return weeks + " weeks ago";
                    }else{
                        return weeks + " week ago";
                    }
                }else if((numOfDayPass >= 30) && (numOfDayPass < 90) ){
                    int months = (int) numOfDayPass/30;

                    if(months > 1) {
                        return months + " months ago";
                    }else{
                        return months + " month ago";
                    }
                }else if((numOfDayPass >= 360) && (numOfDayPass < 1080) ){
                    int years = (int) numOfDayPass/360;

                    if(years > 1) {
                        return years + " years ago";
                    }else{
                        return years + " year ago";
                    }
                }else{
                    return dayName + " " + dayNum + " " + monthName + " " + year + " "+
                    hour + ":" + min + ":" + sec;
                }

        } else {
            return null;
        }
    } else {
        return null;
    }
}
于 2019-07-11T19:04:09.030 回答