Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

Age Calculator

Problem :

How I can calculate future and past dates? How i can compare two dates?

Programs :

Case1 - Add days in current date

Step 1-  Create a text file c:\sunilos\AddDays.java  and copy below contents.

import java.util.Calendar;

/*
 * A program that displays Calendar important methods
 */

public class AddDays {

public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

now = Calendar.getInstance();
now.add(Calendar.DATE, 10);
System.out.println("date before 10 days : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
}
}
 
Step 2 - Deployment
  1. Create a folder 'c:\sunilos'. 
  2. Create or copy AddDays.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile AddDays.java with help of 
    • javac AddDays.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.
Step 3 - Testing
  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java AddDays command.

Output

Current date : 9-14-2009
date before 10 days : 9-24-2009

Case2 - Subtract days from current date

Step 1 Create a text file c:\sunilos\SubDays.java  and copy below contents.
 
import java.util.Calendar;

/*
 * A program that displays Calendar important methods
 */

public class SubDays {

public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

now = Calendar.getInstance();
now.add(Calendar.DATE, -10);
System.out.println("date before 10 days : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
}
}

 

Step 2 - Deployment
  1. Create a folder 'c:\sunilos'. 
  2. Create or copy SubDays.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile SubDays.java  with help of 
    • javac SubDays.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.
Step 3 - Testing
  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java SubDays command.
Output
 
Current date : 9-14-2009
date before 10 days : 9-4-2009

Case 3 - Add months in current date

 
Step 1-  Create a text file c:\sunilos\AddMonths.java  and copy below contents.
 
import java.util.Calendar;
/*
 * A program that displays Calendar important methods
 */

public class AddMonths {

public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

now = Calendar.getInstance();
now.add(Calendar.MONTH, 5);
System.out.println("date after 5 months : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
}
}

Step 2 - Deployment
  1. Create a folder 'c:\sunilos'. 
  2. Create or copy AddMonths.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile AddMonths.java  with help of 
    • javac AddMonths.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.
Step 3 - Testing
  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java AddMonths command.
Output

Current date : 9-14-2009
date after 5 months : 2-14- 2010
 

Case 4 - Subtract months from current date

 
Step 1-  Create a text file c:\sunilos\Main.java  and copy below contents.
 
import java.util.Calendar;

/*
*A program that displays Calendar important methods

*/
public class SubMonths {

public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

now = Calendar.getInstance();
now.add(Calendar.MONTH, -5);
System.out.println("date before 5 months : " + (now.get(Calendar.MONTH) + 1) + "-"+ now.get(Calendar.DATE) + "- + now.get(Calendar.YEAR));
}
}
 
Step 2 - Deployment
  1. Create a folder 'c:\sunilos'. 
  2. Create or copy SubMonths.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile SubMonths.java  with help of 
    • javac SubMonths.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.
Step 3 - Testing
  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java SubMonths command.
Output

Current date : 9-14-2009
date before 5 months : 4-14-2009
 

Case 5 - Add years in current date

Step 1-  Create a text file c:\sunilos\Main.java  and copy below contents.

import java.util.Calendar;

/*
 * A program that displays Calendar important methods
 */
public class AddYears{
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();

System.out.println("Today : " + cal.getTime());

// Subtract 1 year from the calendar
cal.add(Calendar.YEAR, 10);
System.out.println("10 years after " + cal.getTime());
}
}
 
Step 2 - Deployment
  1. Create a folder 'c:\sunilos'. 
  2. Create or copy AddYears.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile AddYears.javawith help of 
    • javac AddYears.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.
Step 3 - Testing
  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java AddYears command.
Output

Today : Mon Sep 14 12:09:57 IST 2009
10 years after Sat Sep 14 12:09:57 IST 2019
 

Case 6 - Subtract years from current date

Step 1-  Create a text file c:\sunilos\Main.java  and copy below contents.

import java.util.Calendar;

/*
 * A program that displays Calendar important methods
 */
public class SubYears {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();

System.out.println("Today : " + cal.getTime());

// Subtract 1 year from the calendar
cal.add(Calendar.YEAR, -10);
System.out.println("10 year ago: " + cal.getTime());
}
}
 
Step 2 - Deployment
  1. Create a folder 'c:\sunilos'. 
  2. Create or copy SubYears.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile SubYears.java with help of 
    • javac SubYears.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.
Step 3 - Testing
  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java SubYears command.
Output

Today : Mon Sep 14 12:16:04 IST 2009
10 year ago: Tue Sep 14 12:16:04 IST 1999
 
 

Case 7 - If Start Date is less then End Date  

// Comparing StartDate with EndDate

Step 1-  Create a text file c:\sunilos\CompareDates.java  and copy below contents.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
* A program that displays Calendar important methods
*/

public class CompareDates {
public static void main(String[] args) throws ParseException {

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

// Get Date 1
Date startdate = df.parse("2000-02-01");

// Get Date 2
Date enddate = df.parse("2001-03-02");

String relation;
if (startdate.equals(enddate))
relation = "the same date as";
else if (startdate.before(enddate))
relation = "before";
else
relation = "after";
System.out.println(startdate + " is " + relation + ' ' + enddate);
}
}

Step 2 - Deployment
  1. Create a folder 'c:\sunilos'. 
  2. Create or copy CompareDates.javainto 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile CompareDates.java with help of 
    • javac CompareDates.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.
Step 3 - Testing
  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java CompareDates command.
Output
Tue Feb 01 00:00:00 IST 2000 is before Fri Mar 02 00:00:00 IST 2001

<<Previous| Next>>
ċ
AddDays(2).java
(1k)
Mahima Hardia,
Aug 19, 2011, 2:14 AM
ċ
AddMonths.java
(1k)
Mahima Hardia,
Aug 19, 2011, 2:15 AM
ċ
AddYears.java
(1k)
Mahima Hardia,
Aug 19, 2011, 2:17 AM
ċ
CompareDates.java
(1k)
Mahima Hardia,
Aug 19, 2011, 2:19 AM
ċ
SubDays.java
(1k)
Mahima Hardia,
Aug 19, 2011, 2:20 AM
ċ
SubMonths.java
(1k)
Mahima Hardia,
Aug 19, 2011, 2:21 AM
ċ
SubYears.java
(1k)
Mahima Hardia,
Aug 19, 2011, 2:22 AM
Comments