home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-06-19 | 1.8 KB | 60 lines |
- // this statement includes the java I/O libraries
- // (more on this later)
- import java.io.*;
-
- // this program calculates the number of days from a given
- // date to the beginning of the year
- class App1_2
- {
- // execution starts here
- public static void main(String args[])
- {
- // read in the month, day and year
- int nMonth = Integer.parseInt(args[0]);
- int nDay = Integer.parseInt(args[1]);
- int nYear = Integer.parseInt(args[2]);
-
- // initialize accumulator to zero
- int nDayInYear = 0;
-
- // now loop through the months, adding
- // in the number of days in each month
- for (int nM = 1; nM < nMonth; nM++)
- {
- switch(nM)
- {
- // the following months have 30 days
- case 4: case 6: case 9: case 11:
- nDayInYear += 30;
- break;
-
- // in February, gotta' consider leap year
- case 2:
- nDayInYear += 28;
-
- // if it's leap year...
- if (((nYear % 4) == 0) && ((nYear % 100) != 0))
- {
- // ...kick in an extra day
- nDayInYear++;
- }
- break;
-
- // all the rest have 31 days
- default:
- nDayInYear += 31;
- }
- }
-
- // now add in the day of the current month
- nDayInYear += nDay;
-
- // print out the result
- // print out the results
- System.out.print (nMonth + "-" + nDay + "-" + nYear);
- System.out.println(" is day number "
- + nDayInYear
- + " in the year");
- }
- }
-