Enummeration/Loop help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javahelpneeded
    New Member
    • Oct 2006
    • 1

    #1

    Enummeration/Loop help

    I am needing help writing a program in which every month of the year is enumerated. It needs to run so that the user can input a day of the year in the format mm/dd/yyyy and the output be the day number in the year (the 3 of feb would be the 31 of jan plus 3 = 34), the date in the form dd/mmm/yyyy, and the year, and still being able to factor in leap years.

    I'm just starting out in java and our teacher really hasn't taught us this stuff at all, and the book bites. Help would be greatly appreciated.
  • vampire03
    New Member
    • Oct 2006
    • 2

    #2
    Originally posted by javahelpneeded
    I am needing help writing a program in which every month of the year is enumerated. It needs to run so that the user can input a day of the year in the format mm/dd/yyyy and the output be the day number in the year (the 3 of feb would be the 31 of jan plus 3 = 34), the date in the form dd/mmm/yyyy, and the year, and still being able to factor in leap years.

    I'm just starting out in java and our teacher really hasn't taught us this stuff at all, and the book bites. Help would be greatly appreciated.

    Maybe I can help u... I will try...

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Code:
      import javax.swing.*;
      import java.util.*;
      public class Dates {
      	public static void main (String[] args) {
      		String date = JOptionPane.showInputDialog("Date in the format dd/mm/yyyy: ");
      
      		int year = Integer.parseInt(date.substring(6, 10));
      		int month = Integer.parseInt(date.substring(3, 5));
      		int day = Integer.parseInt(date.substring(0, 2));
      		GregorianCalendar a = new GregorianCalendar(year, month, day);
      		int[] days = new int[12];
      		for(int i = 0; i < 7;i++) {
      			if(i%2 == 0) {
      				days[i] = 31;
      			}
      			else {
      				days[i] = 30;
      			}
      		}
      		for(int i = 7; i < 12;i++) {
      			if(i%2 == 0) {
      				days[i] = 30;
      			}
      			else {
      				days[i] = 31;
      			}
      		}
      		if(isLeapYear(year)) {
      			days[1] = 29;
      		}
      		else {
      			days[1] = 28;
      		}
      		//finish it off
      
      	}
      
      	public boolean isLeapYear(int year) {
      		//write code to test for leap year here
      		return true;
      	}
      }

      Comment

      Working...