gogs09
gogs09

Reputation: 169

Remaining days calculation in java

I have implemented the following code to calculate the remaining days left. I have used SimpleDateFormat to convert string to date. If I enter 10/02/1993 (today's date) as the date, the output shows 0 days left. But if I choose 11/02/1993, it shows the same output as 0 days left. But if I change the month i.e. 22/07/1993, it shows 160 days left instead of 162 days, which is the original result. What am I doing wrong here ? Any help is greatly appreciated.

private static void calculateNextBirthday() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter your birthday(dd/MM/yyyy)");
        user_input = in.nextLine();

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        Calendar user_selected_day = new GregorianCalendar();

        Calendar today = new GregorianCalendar();

        Date selected_date = null;
        try {
            selected_date = formatter.parse(user_input);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        user_selected_day.setTime(selected_date);

        int user_selected_month = user_selected_day.get(Calendar.MONTH);
        int CMonth = today.get(Calendar.MONTH);

        int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
        int CDate = today.get(Calendar.DAY_OF_MONTH);

        user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));

        user_selected_day.set(Calendar.DAY_OF_WEEK,today.get(Calendar.DAY_OF_WEEK));

        if (user_selected_month < CMonth) {
            user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
        }
        else if (user_selected_month == CMonth){
            if(user_selected_date < CDate){
                user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);  
            }
        }

        long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
        long days;
        days = (millis / (24 * 60 * 60 * 1000));

        System.out.println(String.valueOf(days) + " days left!!");
    }

UPDATE : I have updated the Code below and it works fine. Hope it helps someone in the future.

`       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        Calendar user_selected_day = new GregorianCalendar();

        Calendar today = new GregorianCalendar();

        Date selected_date = null;

        try {
            selected_date = formatter.parse(plainDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        user_selected_day.setTime(selected_date);

        int user_selected_month = user_selected_day.get(Calendar.MONTH);
        int CMonth = today.get(Calendar.MONTH);

        int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
        int CDate = today.get(Calendar.DAY_OF_MONTH);

        user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));

        user_selected_day.set(Calendar.HOUR_OF_DAY,today.get(Calendar.HOUR_OF_DAY));
        user_selected_day.set(Calendar.MINUTE,today.get(Calendar.MINUTE));
        user_selected_day.set(Calendar.SECOND,today.get(Calendar.SECOND));
        user_selected_day.set(Calendar.MILLISECOND,today.get(Calendar.MILLISECOND));


        if (user_selected_month < CMonth) {
            user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
        }
        else if (user_selected_month == CMonth){
            if(user_selected_date < CDate){
                user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);  
            }

        }

        long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
        long days;
        if(user_selected_day.getTime()==today.getTime()){
            days = 0;
        }
        else{
            days = (millis / (24 * 60 * 60 * 1000));
        }`

Upvotes: 1

Views: 2182

Answers (3)

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18743

You can also use LocalDate class in Java 8, to add/subtract dates on the go.

java.time offers a Period class to represent a length of time, such as a number of days, or hours and minutes. LocalDate and friends offer plus() and minus() methods to add or subtract a Period or other time-related object. Period offers factory methods such as ofDays().

read this example for reference. It computes what the date will be 700 days from now:

/**
 * DateAdd -- compute the difference between two dates (e.g., today and 700 days
 * from now).
 */
public class Main {
    public static void main(String[] av) {
        /** Today's date */
        LocalDate now = LocalDate.now();
        Period p = Period.ofDays(700);
        LocalDate then = now.plus(p);
        System.out.printf("Seven hundred days from %s is %s%n", now, then);
    }
}

Running this program reports the current date and time, and what the date and time will be seven hundred days from now. For example:

Seven hundred days from 2013-11-09 is 2015-10-10

Upvotes: 1

Shivangi Nigam
Shivangi Nigam

Reputation: 26

The problem is in the lines

user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));

This will set the year of today's date to user_selected day and as month is equal it will go inside elseif and go out of the same without making any changes.

Also check the if else condition you have mentioned.

Upvotes: 1

JAC
JAC

Reputation: 287

Do not make the calculation yourself. Once you have the Calendar objects, use the "add" method to increment the value you want.

Upvotes: 0

Related Questions