2
import java.util.Calendar;

public class Person {

    private Calendar dob = Calendar.getInstance();
    private String name;
    private int phoneNum;


    public Person (String name, int birthYear, int birthMonth, int birthDate) {
        this.name = name;
        this.dob.set(birthYear, birthMonth, birthDate);
    }

    public Calendar getDob() {
        return dob;
    }
    public void setDob(Calendar dob) {
        this.dob = dob;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(int phoneNum) {
        this.phoneNum = phoneNum;
    }

    // I want this to return true if today is my birthday
    public boolean isCurrentDayInMonthSameAsBirthDayInMonth() {
        if (Calendar.DAY_OF_MONTH == this.dob.get(this.dob.DAY_OF_MONTH)) {
            return true;
        }
        else { 
            return false;
        }
    }

    // I want this to return true if the month is the same as my birth month
    public boolean isCurrentMonthSameAsBirthMonth() {
        if (Calendar.MONTH == this.dob.get(this.dob.MONTH)) {
            return true;
        }
        else {
            return false;
        }
    }

}

Sorry for the format but everything that I need help with is formatted correctly. I need help setting the dob to whatever date I desire and then testing to see if it is my birthday or if the month is the same as my birth month. Thanks!

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
kprog
  • 59
  • 2
  • 8
  • 6
    I cannot more strongly advise against using the legacy `java.util.Calendar` and `java.util.Date` classes. You should use the classes in the `java.time` package instead. – Joe C Feb 12 '17 at 18:22
  • Note, you can rewrite your boolean methods as following `return Calendar.MONTH == this.dob.get(this.dob.MONTH;` (if else is redundant, in such methods). – matoni Feb 12 '17 at 18:26
  • I would use java.time if I could but I have to use java.util.Calendar for this particular assignment – kprog Feb 12 '17 at 18:27
  • Maybe you should point your teacher to these comments! – Steve Chaloner Feb 12 '17 at 18:33

2 Answers2

2

You have two problems:

  1. Calendar.MONTH starts with 0 for January, whereas Calendar.DAY_OF_MONTH starts with 1. So you need this.dob.set(birthYear, birthMonth-1, birthDate);
  2. Calendar.DAY_OF_MONTH and Calendar.MONTH are field indices and not the day and month of today. You need to create a "today" instance of Calendar and test against that Calendar.getInstance().get(Calendar.DAY_OF_MONTH)

As @matoni also suggested, you could shorten your check methods. @Andreas found a better name for the testing method::

// I want this to return true if today is my birthday
public boolean isCurrentDayInMonthSameAsBirthDayInMonth() {
    return isSameAsToday(Calendar.DAY_OF_MONTH);
}

// I want this to return true if the month is the same as my birth month
public boolean isCurrentMonthSameAsBirthMonth() {
    return isSameAsToday(Calendar.MONTH);
}

private boolean isSameAsToday(int field){
    return Calendar.getInstance().get(field) == this.dob.get(field);
}
ppasler
  • 3,579
  • 5
  • 31
  • 51
2

tl;dr

Is today his/her birthday?

Boolean isBirthdayToday =
    MonthDay.from( LocalDate.of( 1953 , 1 , 23 ) )
            .equals( 
                MonthDay.from( LocalDate.now( ZoneId.of( "America/Montreal" ) ) ) 
            ) ;

Is this month the month of his/her birthday?

Boolean isBirthdayThisMonth =
    Month.from( LocalDate.of( 1953 , 1 , 23 ) )
         .equals( 
             Month.from( LocalDate.now( ZoneId.of( "America/Montreal" ) ) ) 
         ) ;

Using java.time

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

Your Person class’ date-of-birth member variable should be of type LocalDate.

LocalDate birthDate = LocalDate.of( 1953 , 1 , 23 );

I suggest passing LocalDate objects around your code rather than mere integers for year-month-day. Doing so makes your code more self-documenting, provides type-safety, and ensures valid values.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

MonthDay

To see if today is the person’s birthday we need to compare month and day while ignoring year. For that, use the MonthDay class.

MonthDay birthMonthDay = MonthDay.from( birthDate );
MonthDay todayMonthDay= MonthDay.from( today );
Boolean isBirthdayToday = birthMonthDay.equals( todayMonthDay );

Month

To compare birth-month rather than month-day of birth, use the Month enum. Same idea as the code seen above.

Month birthMonth = Month.from( birthDate );
Month todayMonth= Month.from( today );
Boolean isBirthdayThisMonth = birthMonth.equals( todayMonth );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154