Return the next date without using any java library


In this article, we will give a date and after a given day our program return the next date without using any java library.

Objective: After giving a date will show the next date

Basically, this problem will divide into the 3 major parts. We know 12 months a year.

  1. 7 months consist of 31 days (January, March, May, July, August, October and December)
  2. 4 months consist of 30 days (April, June, September and November)
  3. February month consist of 28 days (29 days if leap year)

First, check the input values

  • If the year is less than 1812 or greater than 2022 or
  • If the month is less than 1 or greater than 12 or
  • If the day is less than 1 or greater than 31

If one of these conditions is true, then the return on this date is out of the range. Otherwise, execute the else condition. First, increase day + 1 and store in the newday. 

  • If the month is (1, 3, 5, 7, 8, 10, 12) and day is greater than 31 then, set newday = 1 and month++
  • If the month is (4, 6, 9, 11) and day is greater than 30 then, set newday = 1 and month++
  • Otherwise, this month is February, check given year is a leap year or not. If the month is a leap year and day is greater than 29 then, set newDay = 1 and month++. Else day is greater than 28 then, set newDay = 1 and month++.     
  • If the month is greater 12 then, set month = 1 and year++ 
  • Finally, return the newDay
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.Scanner;

public class FindNextDate {

	public String getNextDate(int year, int month, int day) {

		String[] invalidMeg = new String[] { "Invalid data", "Out of the range..."};

		if ((year < 1812 || year > 2022) || (month < 1 || month > 12) || (day < 1 || day > 31)) {
			return invalidMeg[1]; //Out of the range
		} else {

			int newDay = day + 1;
			String nextDay = "";

			if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
				if (day > 31) {
					return invalidMeg[0]; //Invalid data
				}
				if (newDay > 31) {
					newDay = 1;
					month++;
				}
			} else if (month == 4 || month == 6 || month == 9 || month == 11) {
				if (day > 30) {
					return invalidMeg[0]; //Invalid data
				}
				if (newDay > 30) {
					newDay = 1;
					month++;
				}
			} else {
				if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
					if (day > 29) {
						return invalidMeg[0]; //Invalid data
					}
					if (newDay > 29) {
						newDay = 1;
						month++;
					}

				} else {
					if (day > 28) {
						return invalidMeg[0]; //Invalid data
					}
					if (newDay > 28) {
						newDay = 1;
						month++;
					}
				}
			}

			if (month > 12) {
				month = 1;
				year++;
			}
			nextDay += year + "-" + month + "-" + newDay;

			return nextDay;
		}
	}

	public static void main(String[] args) {
		int year, month, day;
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the date (yyyy-mm-dd):");
		String x = input.nextLine();
		String[] s = x.split("-");
		year = Integer.parseInt(s[0]);
		month = Integer.parseInt(s[1]);
		day = Integer.parseInt(s[2]);

		FindNextDate next = new FindNextDate();
		System.out.println(next.getNextDate(year, month, day));

	}
}

Output

Enter the date (yyyy-mm-dd):
2019-11-6
2019-11-7
Next Post Previous Post
No Comment
Add Comment
comment url