ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

[Part 5] Create your own Calendar (Date/Time) library from scratch using Java

Updated on April 3, 2015

Part 5 - Next date

In Part 4, we have created a method that will return the next month of any given date. We have also eliminated the manual work of doing so and the exception we have encountered in Part 3. We are left with the task of getting the next day and year so we can get the actual next date.

Since we have reached the fifth part, I have some news.

We're on GitHub. Hooray!
We're on GitHub. Hooray! | Source

We're on GitHub!

The source code from Part 1 - 5 are now on GitHub. You can download the source code and play around with it. Some source codes are available before they are discussed here. The link to the repository can be found at the end of this article.

Method
Purpose
nextYear()
Returns the year after a given year.
nextDay()
Returns the next day.
nextDate()
Returns the full date (day, month, and year) after a given full date.
Methods and their purpose.

Getting the next year

The first method we're going to create would be easy and simple. The nextYear() method with an int return type accepts a parameter called "date" with a string data type and contains the full date including day, month, and year. We initialized the int variable called "nextYear" with the value of the year that we will get when we parse the value of the parameter "date" plus 1 in order to get the next year. The value of "nextYear" is returned.

This is pretty straightforward and there is no need for conditional statements since all you need is to add 1 at all times.

	public int nextYear(String date){
		int nextYear = getYear(date) + 1;
		
		return nextYear;
	}

Getting the next day

For the nextDay() method, we have two conditional statements. We need to find out first if the day that is passed into the method is the last day of the month or not then we decide whether we should add 1 to or assign 1 to the value of the "nextDay" variable.

In the "if" statement, we took the day out of the full date and checked if it not equal to the number of days of the month in the full date. If it's not, the day will simply be added 1 and the result will be assigned to the variable "nextDay". For the example below, we will use the date "03-MAR-2000".

	public int nextDay(String date){
		int nextDay = 0;
		
		if (getDay(date) != numOfDays(date)){
			nextDay = getDay(date) + 1;
		}
		else
			nextDay = 1;
		
		return nextDay;
	}

Example

This is an example of a day that is not equivalent the last day of a month.

Code (Underlined): if (getDay(date) != numOfDays(date))
Actual value (Result): "03-MAR-2000"

Code (Underlined): if (getDay(date) != numOfDays(date))
Actual value (Result): numOfDays = 31

Code (Underlined): if (getDay(date) != numOfDays(date))
Actual value (Result): "03-MAR-2000"

Code (Underlined): if (getDay(date) != numOfDays(date))
Actual value (Result): 3

Code (Underlined): if (getDay(date) != numOfDays(date))
Actual value (Result): 3 != 31

Is the day 3 not equal to 31 which is the last day of March? True. Since the condition is true, it will execute the code under the if statement.

nextDay = getDay(date) + 1;
nextDay = 3 + 1
nextDay = 4

We got the answer 4, let's see if we will get the same result when we run the code.

SampleClass

		System.out.println("The next day after " + myCalendar.getDay("03-MAR-2000") + " in 03-MAR-2000" + " is " + myCalendar.nextDay("03-MAR-2000") + ".");
We got 4 as expected.
We got 4 as expected.

Now what if the day passed to the method is the last day of the month? Since it will not satisfy the first condition (if day is not equal to last day), it will proceed to the "else" part and execute the code below it.

As simple as it is, the value of the variable "nextDay" would simply be assigned the value 1. Since the day has reached the end of a month, the next day is the first day of the next month.

else
nextDay = 1;

		System.out.println("The next day after " + myCalendar.getDay("31-JAN-2015") + " in 31-JAN-2015" + " is " + myCalendar.nextDay("31-JAN-2015") + ".");
We got 1 as expected.
We got 1 as expected.

Getting the next date

Now that we created methods to get the next day, next month, and next year, we can now combine these individual and specific methods to form another method that can give us the complete next date after a given full date.

	public String nextDate(String date){
		String nextDate = "";
		
		if (nextDay(date) != 1){
			nextDate = nextDay(date) + "-" + getMonth(date) + "-" + getYear(date);
		}
		else if (nextDay(date) == 1 & monthID(date) != 12){
			nextDate = "1" + "-" + nextMonth(date) + "-" +  getYear(date);
		}
		else
			nextDate = "1" + "-" + nextMonth(date) + "-" + nextYear(date);
		
		return nextDate;
	}

We have three conditions under the "nextDate()" method.

[1] If the next day is not equivalent to 1.
[2] If the next day is equivalent to 1 and the month is not December.
[3] None of the above.

MyCalendar

Below is the code for the nextDate() method and the other three methods; nextDay(), nextMonth(), and nextYear().

	public int nextDay(String date){
		int nextDay = 0;
		
		if (getDay(date) != numOfDays(date)){
			nextDay = getDay(date) + 1;
		}
		else
			nextDay = 1;
		
		return nextDay;
	}
	
	public String nextMonth(String date){
		String nextMonth = "";
		
		if (monthID(date) != 12){
			nextMonth = monthName(monthID(date) + 1);
		}
		else
			nextMonth = monthName(1);
		
		return 	nextMonth;
	}
	
	public int nextYear(String date){
		int nextYear = getYear(date) + 1;
		
		return nextYear;
	}
	
	public String nextDate(String date){
		String nextDate = "";
		
		if (nextDay(date) != 1){
			nextDate = nextDay(date) + "-" + getMonth(date) + "-" + getYear(date);
		}
		else if (nextDay(date) == 1 & monthID(date) != 12){
			nextDate = "1" + "-" + nextMonth(date) + "-" +  getYear(date);
		}
		else
			nextDate = "1" + "-" + nextMonth(date) + "-" + nextYear(date);
		
		return nextDate;
	}

SampleClass

I wrote a few samples for possible and reasonable values that may be passed to our method. This includes:

[1] Ordinary days; days that are not:
[2] Last day of a month
[3] Last day of February on a leap year
[4] Last day of the year

public class SampleClass {
	
	public static void main(String[] args) {
		MyCalendar myCalendar = new MyCalendar();
				
		System.out.println("************** Ordinary days ***************");
		System.out.println("Next to " + "01-JAN-2016" + " is " + myCalendar.nextDate("01-JAN-2016"));
		System.out.println("Next to " + "26-FEB-1991" + " is " + myCalendar.nextDate("26-FEB-1991"));
		System.out.println("Next to " + "03-MAR-2013" + " is " + myCalendar.nextDate("03-MAR-2013"));
		System.out.println("Next to " + "08-APR-1868" + " is " + myCalendar.nextDate("08-APR-1868"));
		System.out.println("Next to " + "11-MAY-2001" + " is " + myCalendar.nextDate("11-MAY-2001"));
		System.out.println("Next to " + "21-JUN-2011" + " is " + myCalendar.nextDate("21-JUN-2011"));
		System.out.println("Next to " + "15-JUL-1998" + " is " + myCalendar.nextDate("15-JUL-1998"));
		System.out.println("Next to " + "03-AUG-2013" + " is " + myCalendar.nextDate("03-AUG-2013"));
		System.out.println("Next to " + "30-SEP-2005" + " is " + myCalendar.nextDate("30-SEP-2005"));
		System.out.println("Next to " + "27-OCT-1984" + " is " + myCalendar.nextDate("27-OCT-1984"));
		System.out.println("Next to " + "06-NOV-2003" + " is " + myCalendar.nextDate("06-NOV-2003"));
		System.out.println("Next to " + "04-DEC-1879" + " is " + myCalendar.nextDate("04-DEC-1879"));
		
		System.out.println("\n********** Last day of each month **********");
		System.out.println("Next to " + "31-JAN-2016" + " is " + myCalendar.nextDate("31-JAN-2016"));
		System.out.println("Next to " + "28-FEB-1991" + " is " + myCalendar.nextDate("28-FEB-1991"));
		System.out.println("Next to " + "31-MAR-2013" + " is " + myCalendar.nextDate("31-MAR-2013"));
		System.out.println("Next to " + "30-APR-1868" + " is " + myCalendar.nextDate("30-APR-1868"));
		System.out.println("Next to " + "31-MAY-2001" + " is " + myCalendar.nextDate("31-MAY-2001"));
		System.out.println("Next to " + "30-JUN-2011" + " is " + myCalendar.nextDate("30-JUN-2011"));
		System.out.println("Next to " + "31-JUL-1998" + " is " + myCalendar.nextDate("31-JUL-1998"));
		System.out.println("Next to " + "31-AUG-2013" + " is " + myCalendar.nextDate("31-AUG-2013"));
		System.out.println("Next to " + "30-SEP-2005" + " is " + myCalendar.nextDate("30-SEP-2005"));
		System.out.println("Next to " + "31-OCT-1984" + " is " + myCalendar.nextDate("31-OCT-1984"));
		System.out.println("Next to " + "30-NOV-2003" + " is " + myCalendar.nextDate("30-NOV-2003"));
		
		System.out.println("\n**************** Leap year *****************");
		System.out.println("Next to " + "28-FEB-2016" + " is " + myCalendar.nextDate("28-FEB-2016"));
		
		System.out.println("\n*************** End of year ****************");
		System.out.println("Next to " + "31-DEC-1879" + " is " + myCalendar.nextDate("31-DEC-1879"));
	}
}
The result after running the samples for the "nextDate()" method.
The result after running the samples for the "nextDate()" method.

Importance of very simple methods

Looks like we got the right results. The isLeapYear() method we created in Part 2 was pretty useful too. This is the advantage of creating each method that only does a very specific task. It becomes very easy to re-use that it is no longer necessary to hard-code its contents to a method where we will be needing it.

Repository on GitHub

I uploaded the source code on GitHub. I also added a few samples so you can test the methods and play around with it. It would be more convenient than having to go back to Part 1 just to manually copy the codes and assemble it all together. So go ahead and experiment with the code. If you encounter any problems, you can reach out to me via email.

Check out the repository.

Also, if you find any bugs or something that just doesn't work, please let me know. If you have any questions, you can email me at jvmistica@gmail.com.

MyCalendar repository on GitHub.
MyCalendar repository on GitHub. | Source

End of Part 5

We now have a complete method that returns the next date in "DD-MON-YYYY" format. One of the features that our library should have is to make operations using dates such as adding and subtracting days, months, or years. We should also be able to do something as simple as counting the days between two different dates. All of these will be discussed in the next parts.

Read Part 6

© 2015 Joann Mistica

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.