5 Replies - 840 Views - Last Post: 07 May 2012 - 05:49 PM Rate Topic: -----

#1 chappie591  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-May 12

Create and return a new date object.

Posted 06 May 2012 - 02:57 PM

How do i create and return a new date object that represents the day after the current date object? Im not getting any errors but my nextDay method isn't working correctly.
public class Date {
	private int year;
	private int month;
	private int day;

	// Constructor: Date object with year, month and day as parameters.
	public Date(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public Date() {
		// TODO Auto-generated constructor stub
	}
	// Return year to the date object above
	public int getYear() {
		return year;
	}
	// Return month in the date object above
	public int getMonth() {
		return month;
	}
	// Return day in the date object above
	public int getDay() {
		return day;
	}
	// formats the date to look like; "Year/month/day"
	public String toString() {
		return (Integer.toString(year) + "/" + Integer.toString(month) + "/" + Integer.toString(day));
	}

	// compares the 2 dates given to see if they match.
	public boolean equals(Date date2) {

		return (this.year == date2.getYear() && this.month == date2.getMonth() && this.day == date2.getDay());
	}

	// uses a boolean method to check if there is a leap year.
	public static boolean isLeapYear(int year) {
		boolean check = false;

		if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0)) {
			check = true;
		}
		return check;
	}
	// Return the number of days in the month and year
	// Uses array lookup plus for February check if it is a leap year.

	public static int daysInMonth(int month, int year) {
		// days in month table using indexes 1-12
		final int monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
				30, 31 };
		
		if (month == 2 && isLeapYear(year)) // special case for February
			return 29;
		
		return monthDays[month];
	}

	// return the next Date

	public Date nextDay() {
		Date next = new Date();
		next.month = month;
		next.day = day;
		next.year = year;
		if (month == 2)
			if (day < 28)
				next.day++;
			else {
				next.day = 1;
				next.month = 3;
			}
		else if (month == 4 || month == 6 || month == 9 || month == 11)
			if (day < 30)
				next.day++;
			else {
				next.day = 1;
				next.month = month++;
			}
		else if (month == 12 && day == 31) {
			next.month = 1;
			next.day = 1;
			next.year++;
		} else if (day < 31)
			next.day++;
		else {
			next.day = 1;
			next.month = month++;
		}
		return next;
	}
}



public class DateTester {

	public static void main(String[] args) {
        
		// declares all the variables that will be used.
		Date today = new Date(5,1,2012);
        Date myBirthday = new Date(1,23,1987);
        Date partnerBirthday = new Date(1,11,1785);
        Date xmas = new Date(12,25,2012);
       
        // prints out all the info above^^^/>^^
        System.out.println("Today is " + today);
        System.out.println("Phil's birthday is " + myBirthday);
        System.out.println("Partner's birthday is " + partnerBirthday);
        System.out.println("Xmas is " + xmas);
        System.out.println();
        
        // adds a new variable and displays a date comparison
        Date today2 = new Date(5,2,2012);            // use today's date
        System.out.println(today + " equals " + today2 + " " + today.equals(today2) );
        System.out.println(today + " equals " + xmas + " " + today.equals(xmas) );
        System.out.println();
        
        // print statements that show if a specific year is a leapyear.
        System.out.println("Is 2000 a leap year? " + Date.isLeapYear(2000) );
        System.out.println("Is 2001 a leap year? " + Date.isLeapYear(2001) );
        System.out.println("Is 1900 a leap year? " + Date.isLeapYear(1900) );
        System.out.println("Is 2008 a leap year? " + Date.isLeapYear(2008) );
        System.out.println();
     // print 10 days starting with xmas
        Date date = xmas;
        for (int i = 1; i <= 10; i++) {
            System.out.println(i + ". " + date);
            date = date.nextDay();
        }
        
	}
    }
 

And this is the ouput im getting:

Today is 5/1/2012
Phil's birthday is 1/23/1987
Partner's birthday is 1/11/1785
Xmas is 12/25/2012

5/1/2012 equals 5/2/2012 false
5/1/2012 equals 12/25/2012 false

Is 2000 a leap year? true
Is 2001 a leap year? false
Is 1900 a leap year? false
Is 2008 a leap year? true

1. 12/25/2012 // the days are supposed to increase 10 times with the month and year rolling over as well.
2. 12/25/1
3. 12/25/2
4. 12/25/3
5. 12/25/4
6. 12/25/5
7. 12/25/6
8. 12/25/7
9. 12/25/8
10. 12/25/9

Is This A Good Question/Topic? 0
  • +

Replies To: Create and return a new date object.

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Create and return a new date object.

Posted 06 May 2012 - 03:16 PM

You should check out GregorianCalendar, I think it has most of the methods you would need.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Create and return a new date object.

Posted 06 May 2012 - 06:26 PM

Kakerergodt I guess the OP has to implement it's own for an homework

You wouldn't beleive the number of time we have re-invented Integer.parseInt(), parseInt(,base) and StringBuilder.reverse() methods in this forum not mentioning the LinkedList class. Just by itself it must represent 5% of the posts in this forum :)

This post has been edited by pbl: 07 May 2012 - 05:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 chappie591  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-May 12

Re: Create and return a new date object.

Posted 07 May 2012 - 09:55 AM

i believe it, sorry for the repetition.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Create and return a new date object.

Posted 07 May 2012 - 05:41 PM

View Postchappie591, on 07 May 2012 - 12:55 PM, said:

i believe it, sorry for the repetition.

That's ok, no problem. My remark was for Kakerergodt
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Create and return a new date object.

Posted 07 May 2012 - 05:49 PM

Your constructor of Date is

public Date(int year, int month, int day) {

you always call it with (day, month, year)
that screws up all the rest

Your toString() returns year/month/day so


1. 12/25/2012 // the days are supposed to increase 10 times with the month and year rolling over as well.
2. 12/25/1
3. 12/25/2
4. 12/25/3
5. 12/25/4
6. 12/25/5
7. 12/25/6
8. 12/25/7
9. 12/25/8
10. 12/25/9

is OK. Call nextDay()i 22 more times and you will get
12/26/1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1