0

I am using the following code to check the birthday of a person:

public class Test_Year {


    static int yearsInterval =1; 
    static int yearsSpecial =0; 
    static Date dateYearsReg; 


    public static void main(String[] args){
        yearsToNotify(2013, "0001-10-02");
    }


    public static void yearsToNotify(int yearsElapsedSinceBirth, String dateOfBirth){
        Date dt = Convert(d); 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(dt); 
        System.out.println(); 


        yearsSpecial = yearsInterval*(1+(years/yearsInterval)); 
        System.out.println(yearsSpecial); 
        cal.add(Calendar.YEAR, yearsSpecial);
        dateYearsReg = cal.getTime(); 
        System.out.println(dateYearsReg);
    }

    public static  Date Convert(String S){

        String dateStr = S;
        Date d1 = null ;

        try {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            d1 = f.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return d1; 
    }

}

Ideally the above should give me the next birthday and the year should be 2014, however I get the year as 2015. The system date being todays date that is first of October 2014. Any hints?

In the baove if I give a call like: yearsToNotify(41, "1972-10-17"); I get the correct values. Seems this is a problem with results when I use the year as 0001.

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Can you explicit what are years and d in the `yearsToNotify(int years, String d)` function ? – dekajoo Oct 01 '14 at 10:09
  • Years are the number of years which have passed since birth and d is the date of birth. Sorry for my habit of not using standard variable names. – Skynet Oct 01 '14 at 10:10

1 Answers1

2

I believe the problem stems from this line

cal.add(Calendar.YEAR, yearsSpecial);

If we look at the javadoc for ADD we see it is this

public abstract void add(int field,
       int amount)

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:

Instead you want to be using set:

cal.set(Calendar.YEAR, yearsSpecial);
  • You Sir, are my saviour! :) – Skynet Oct 01 '14 at 10:14
  • However, do check with the following input: yearsToNotify(20, "1994-10-02"); – Skynet Oct 01 '14 at 11:13
  • Looks like I misinterpreted the purpose of the program. The aim is to calculate the next birthday given $years have passed since the $d ? Sure that means it was working correctly in the first place? Example yearsToNotify(2013, "0001-10-02"); so 2013 years have passed since 0001-10-02. That means its 2014-10-02. Thus the next birthday would be 2015. – Matthew Clark Oct 01 '14 at 11:29
  • We have not yet hit 02 as in the day part, so I think the next alert should be for 2014. – Skynet Oct 01 '14 at 11:33
  • Even if in condition is modified to: yearsSpecial = years + 1; – Skynet Oct 01 '14 at 11:41
  • Your code at no point uses the current date so how could it be used to calculate the next birthday? You seem to have conflicting requirements on your program. Do you want it to calculate the next birthday based upon the data supplied and years passed? Or do you want it to calculate the next birthday based upon the current data and their birthdate? – Matthew Clark Oct 01 '14 at 11:41
  • I have divided this problem into two, The number of years elapsed since now has been calculated using a different function. I get 2013 from that. Its in that function that I take system date into consideration. Would you like to have a look at that function? – Skynet Oct 01 '14 at 11:46
  • I believe that may be where your error lie. As for example the difference between 0001-10-02 and 2014-10-01 is less than 2013 years. 2013 years would make is 2014-10-02 – Matthew Clark Oct 01 '14 at 11:54
  • Here is my fnction using JODA: `public static int getDiffYear(Date first) { int yearsBetween = Years.yearsBetween(new LocalDate(first), new LocalDate()).getYears(); return yearsBetween; } ` – Skynet Oct 01 '14 at 11:55
  • Does this method take into account the months? I don't know what your yearsBetween does but you need something similar to this http://stackoverflow.com/questions/7906301/how-can-i-find-the-number-of-years-between-two-dates-in-android – Matthew Clark Oct 01 '14 at 13:01