Android subtracting Date

16,976

Solution 1

Your diff sample in the comment is wrong. I tried your code and got this result:

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

And roughly checked 70 days until new year sounds solid.

Solution 2

Hope this will help

The code below output time if the date difference is less than a day, out day when the date difference is less then 7 days and so on....

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;
    }
}
Share:
16,976
Alexander Fuchs
Author by

Alexander Fuchs

Updated on June 04, 2022

Comments

  • Alexander Fuchs
    Alexander Fuchs almost 2 years

    So I am trying to subtract 2 dates I have come up with this code :

    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();
    

    But the problem is the diff isn't correct.

    Can anybody tell me what I am doing wrong ?