Home Java Example Java Core Java Find Next Date Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Find Next Date Example

Advertisement
This example explains you about how to display the next date of input date. This tutorial explains you all the steps involved in developing this application. To develop this application we will use the Eclipse IDE and JDK 1.6.

Java Find Next Date Example

In this example we will discuss about how to find the next date of the input date.

This example explains you about how to display the next date of input date. This tutorial explains you all the steps involved in developing this application. To develop this application we will use the Eclipse IDE and JDK 1.6.

Example :

Here I am giving a simple example which will demonstrate you about how to find the next date of the input date. In this example we will handle the leap year also. To create this application I have create a class where I have taken three integer fields for month, day, and year. In this class I have created a default constructor for initializing the default date value and a parameterized constructor for initializing the fields value with the input value. Then I have created getter methods of these fields for finding their values. I have created a method for checking the leap year which will check the input year whether the input is a leap year or not. In this application you will find the next year of the input year. Such as if you inserted the date like 2/16/2013 then the next date will be 2/17/2013 and if the date is 2/28/2013 then the next date will be 3/1/2013 and if the input date is 12/31/2013 then the next date will be 1/1/2014

Source Code

NextDateExample.java

public class NextDateExample {
int month, day, year;

public NextDateExample() { 
month = 1;
day = 1;
year = 2000;
}

public NextDateExample(int month, int day, int year) { 
this.day = day;
this.month = month;
this.year = year;
}

public int getMonth() {
return month;
}

public int getDay() {
return day;
}

public int getYear() {
return year;
} 

public boolean isLeapYear() {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}

public NextDateExample nextDate()
{ 
if((month == 1 && day < 31 ) || (month == 3 && day < 31)
|| (month == 5 && day < 31) || (month == 7 && day <31)
||(month == 8 && day < 31) || (month == 10 && day < 31))
{
day = day+1; 
}
else if((month == 1 && day == 31) || (month == 3 && day == 31) 
|| (month == 5 && day == 31) || (month == 7 && day == 31)
|| (month == 8 && day == 31) || (month == 10 && day == 31))
{
day = 1;
month = month+1;
}
else if((month == 1 && day >31) || (month == 3 && day >31)
|| (month == 5 && day >31) || (month == 7 && day >31) 
|| (month == 8 && day >31) || (month == 10 && day >31))
{
System.out.println("Day must not be greater 31");
System.exit(0);
}
if((month == 4 && day < 30) || (month == 6 && day < 30) 
|| (month == 9 && day < 30) 
|| (month == 11 && day < 30))
{
day = day+1;
}
else if((month == 4 && day == 30) || (month == 6 && day == 30) 
|| (month == 9 && day == 30) 
|| (month == 11 && day == 30))
{
day = 1;
month = month+1;
}
else if((month == 4 && day > 30) || (month == 6 && day > 30) 
|| (month == 9 && day > 30) 
|| (month == 11 && day > 30))
{
System.out.println("Day must not be greater than 30");
System.exit(0);
}
if(month == 2)
{
if(isLeapYear() == true)
{ 
if(day < 29)
{
day = day+1;
}
else if(day == 29)
{
day = 1;
month = month+1;
}
else if(day >29)
{
System.out.println("In leap year day must not be greater than 29");
System.exit(0);
}
}
else
{
if(day < 28)
{
day = day+1;
}
else if (day == 28)
{
day = 1;
month = month+1;
}
else if(day > 28)
{
System.out.println("Day must not be greater than 28");
System.exit(0);
}
} 
}
if(month == 12 && day < 31 )
{
day = day+1;
}
else if(month == 12 && day == 31)
{
day = 1;
month = 1;
year = year+1;
}
else if(month > 12){
System.out.println("Month must not be greater than 12");
System.exit(0);
}
return new NextDateExample(month,day,year); 
}

public static void main(String[] args) {
NextDateExample date1 = new NextDateExample(); 
int y1 = date1.getYear(); 
NextDateExample date = new NextDateExample(12, 31, 2013);

int y2 = date.getYear(); 
if(y2 < y1 )
{
System.out.println("Date must be greater than or equals to "+date1.getMonth()+"/"+date1.getDay()+"/"+date1.getYear());
}
else
{ 
System.out.println("Date Format : MM/dd/yyyy"); 
date.nextDate(); 
System.out.print("Next Date : ");
System.out.println(date.getMonth()+"/"+date.getDay()+"/"+date.getYear());
}

} 
}

Output

When you will execute the above example you will get the output as follows :

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 18, 2013

Related Tutorials

Ask Questions?    Discuss: Java Find Next Date Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com