Results 1 to 3 of 3
  1. #1
    nanderson05 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Date printing program school project

    Hello I have this program I had to make for a class to just print out dates etc, heres my code but it isnt launching properly.

    Here is the desired output

    Checking increment
    Date object constructor for date 11/27/2011
    Incremented Date:11/28/2011
    Incremented Date:11/29/2011
    Incremented Date:11/30/2011
    Day 31 invalid. Set to day 1.
    Incremented Date:12/1/2011
    Incremented Date:12/2/2011
    ...
    Incremented Date:12/30/2011
    Incremented Date:12/31/2011
    Day 32 invalid. Set to day 1.
    Incremented Date:1/1/2012
    Incremented Date:1/2/2012
    Incremented Date:1/3/2012
    Incremented Date:1/4/2012
    Incremented Date:1/5/2012
    Incremented Date:1/6/2012

    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class Date {
    
    	private int day;
    	private int month;
    	private int year;
    	String str;
    	
    	
    	public int getDay()
    	{
    	if (day >=1 && day <=31){
    		str = JOptionPane.showInputDialog("Enter Day");
    		day = Integer.parseInt(str);
    		}
    	else { 
    		System.err.println("Incorrect input");
    	}
    	return day;
    	}
    	
    
    public int getMonth()
    {
    	if (month >=1 && month <=12){
    		str = JOptionPane.showInputDialog("Enter Month");
    		month = Integer.parseInt(str);
    	}
    else {
    	System.err.println("Incorrect input");
    }
    return month;
    }
    
    public int getYear()
    {
    	if (year >= 1900 && year <=2222){
    		str = JOptionPane.showInputDialog("Enter Year");
    		year = Integer.parseInt(str);
    		
    	}
    	else {
    		year = 1900;
    	}
    	return year;
    	}
    
    public void nextDay()
    {
    	int[] day_In_Months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	
    	day ++;
    	
    	if (month == 0 && day == 0){
    		day = 1;
    		month ++;
    	}
    	
    	if (month == 1 && day == 31){
    		day = 1;
    		month ++;
    		
    	}
    	if (month == 2 && day == 28){
    		day = 1;
    		month ++;
    		
    	}
    	if (month == 3 && day == 31){
    		day =1;
    		month ++;
    	}
    	if (month == 4 && day == 30){
    		day =1;
    		month ++;
    	}
    	if (month == 5 && day == 31){
    		day =1;
    		month++;
    	}
    	if (month == 6 && day == 30){
    		day =1;
    		month++;
    	}
    	if (month == 7 && day == 31){
    		day =1;
    		month++;
    	}
    	if (month== 8 && day == 31){
    		day =1;
    		month++;
    	}
    	if (month== 9 && day == 30){
    		day =1;
    		month++;
    	}
    	if (month == 10 && day == 31){
    		day =1;
    		month++;
    	}
    	if (month == 11 && day == 30){
    		day =1;
    		month++;
    	}
    	if (month == 12 && day == 31){
    		day =1;
    		month =1;
    		year ++;
    	}
    	if (day == 29 && month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))){
    		day ++;	
    	}
    	else {day =1;}
    	
    	}
    public void toDateString(int day, int month, int year){
    	System.out.println("The date is : " + day + "/" + month + 
    			"/" + year);
    	
    }
    }
    Java Code:
    import javax.swing.JOptionPane;
    
    public class DateTest extends Date {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String month = JOptionPane.showInputDialog("Enter the numerical Month: ");
    		String day = JOptionPane.showInputDialog("Enter the numerical Day: ");
    		String year = JOptionPane.showInputDialog("Enter the numerical Year: ");
    		
    		Date testDate= new Date(Integer.parseInt(month), Integer.parseInt(day), Integer.parseInt(year));
    		
    	}{
    	for (int counter = 0; counter < 40; counter++)
    	{
    	testDate.nextDay();
    	System.out.println( "Incremented Date:" + testDate.toDateString() );
    	}
    }
    }
    Any help would be greatly appretiated :)

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Date printing program school project

    You could easily increment a date if you are using the java.util.Calendar object. There is an add() method that you can use the add some number of day, month or year to the calendar. When you specified the number in negative value then it will subtract the calendar. The snippet look like below:

    Java Code:
    ...
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    calendar.add(Calendar.YEAR, 1);
    calendar.add(Calendar.MONTH, -10);
    
    Date date = calendar.getTime();
    ...

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Date printing program school project

    Help us to help you. What specific problems are you having? Compiler errors? Runtime errors? Incorrect behaviour? If so then provide a sample of what output you are getting. Try and narrow the problem down as much yourself before expecting us to debug your code for you.

Similar Threads

  1. Need help with school project
    By camaro01 in forum New To Java
    Replies: 16
    Last Post: 02-20-2011, 06:34 PM
  2. School project help!
    By Addez in forum Advanced Java
    Replies: 5
    Last Post: 09-17-2010, 10:37 PM
  3. Need help with school project!
    By jaxxx in forum New To Java
    Replies: 5
    Last Post: 03-25-2010, 08:55 PM
  4. School project: network program via java any ideas?
    By Sernomicus in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:46 PM
  5. school project help
    By justin8790 in forum New To Java
    Replies: 5
    Last Post: 03-22-2009, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •