Results 1 to 3 of 3
  1. #1
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Exclamation help with program please!

    this is a program i am working on to display all dates in a given year.
    I am new to Java. Was wondering if anyone could provide a simpler eay to do it .
    Also I need help with how to make the month February to change from 28 days to 29 when there is a leap year.
    here is my code: Sorry its a little messy
    **************************************************

    public class exerciseDate
    {
    public static void main(String[] arguments)
    {

    int year = 2008;
    int month = 1;
    int day = 1;

    //January
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //February
    while(day <= 29)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //March
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //April
    while(day <= 30)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //May
    while(day <= 30)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //June
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //July
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //August
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //September
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //October
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //November
    while(day <= 30)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;
    day = 0;
    //December
    while(day <= 31)
    {

    System.out.println(month + "/" + day + "/" + year);
    day++;


    }
    month++;




    }
    }

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Use a Calendar.

    Java Code:
    int year = 2008;
    
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DATE, 1);
    
    // for each day in the year
    while (calendar.get(Calendar.YEAR) == year) {
    	// e.g. 01/01/2008
    	System.out.printf("%1$tm/%1$te/%1$tY%n", calendar);
    
    	// go to next day
    	// accounts for the number of days in a month (including leap years)
    	// (automatically "wraps", for example, January 32 is February 1)
    	calendar.add(Calendar.DATE, 1);
    }
    Last edited by CodesAway; 12-01-2009 at 02:05 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

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