Java Perpetual Calendar

Source: Internet
Author: User

Two consecutive articles starting with this article will introduce how to use Java basic knowledge to create the 10 thousand keystore console program.

Let's take a look.

Let's analyze the calendar business:

1. the user enters the information --> determines whether the information meets the requirements)

2. calculate the total number of days from January 1, January 1, 1900 to the current day based on Monday January 1, 1900.

(1) calculate the total number of days from January 1, 1900 to (the year entered by the user-1) --> pay attention to the calculation of the point of the year

(2) calculate the number of days from January 1, January (month-1) of the year entered by the user.

3. Calculate the day of the week on the first day of the input month.

4. format the output

Follow these steps to parse the code

1. Use the do-while loop to receive user input information and use the IF-else statement for judgment.

Code:
  1. Int year;
  2. Int month;
  3. Boolean xn = false;
  4. Do
  5. {
  6. System. Out. println ("Enter the year :");
  7. Year = input. nextint ();
  8. System. Out. println ("Enter the month :");
  9. Month = input. nextint ();
  10. // Use a Boolean expression to judge input information
  11. Xn = (month <1) | (month> 12) | (year <1 );
  12. If (Xn)
  13. {
  14. System. Out. println ("incorrect input information. Please try again! ");
  15. }
  16. } While (Xn );

2. Determine the Ping-leap year and calculate the total number of days from January 1, 1900 to (user input year-1)

Code:
  1. Int everyyearday = 0; // number of days per year
  2. Int totalyearsdays = 0; // calculates the number of days in a year.
  3. Int inputyearday = 0 // records the number of days of the user input year
  4. Boolean YN = false; // identifies a peat year.
  5. // Use the for loop to calculate the number of days
  6. For (INT I = 1900; I <= year; I ++)
  7. {
  8. If (I % 4 = 0) & (I % 100! = 0) | (I % 400 = 0) // condition for determining a leap year
  9. {
  10. YN = true;
  11. Every yyearday = 366;
  12. }
  13. Else
  14. {
  15. YN = false;
  16. Every yyearday = 365;
  17. }
  18. // If the year in the cycle is less than the year entered by the user, the cumulative number of days
  19. If (I <year)
  20. {
  21. Totalyearsdays = totalyearsdays + everyyearday;
  22. }
  23. Else
  24. {
  25. Inputyearday = everyyearday;
  26. System. Out. println (Year + "years total:" + inputyearday + "day ");
  27. }
  28. }

3. determine the number of days of the month and calculate the number of days from January 1, January to (month-1) of the current year.

Code:
  1. Int everymonthday = 0; // records the number of days per month
  2. Int totalmonthsdays = 0; // Total Number of days
  3. Int inputmonthday = 0; // records the number of days of the month input by the user in the year input by the user
  4. // Use the for loop to calculate the number of days
  5. For (INT I = 1; I <= month; I ++)
  6. {
  7. Switch (I)
  8. {
  9. Case 4:
  10. Case 6:
  11. Case 9:
  12. Case 11:
  13. Everymonthday = 30;
  14. Break;
  15. Case 2:
  16. If (Xn) // XN is a Boolean variable used to record the year of a leap.
  17. {
  18. Everymonthday = 29;
  19. }
  20. Else
  21. {
  22. Everymonthday = 28;
  23. }
  24. Break;
  25. Default:
  26. Everymonthday = 31;
  27. Break;
  28. }
  29. If (I <month)
  30. {
  31. Totalmonthsdays = totalmonthsdays + everymonthday;
  32. }
  33. Else
  34. {
  35. Inputmonthday = everymonthday;
  36. System. Out. println (month + "total months:" + inputmonthday + "day ");
  37. }
  38. }

4. calculate the total number of days, and calculate the day of the first day of the week of the month entered by the user.

Code:
  1. Int Total = totalmonthsdays + totalyearsdays; // calculates the total number of days.
  2. Int temp = (total + 1) % 7; // determines the day of the week on the first day of the input month.

5. format the output

Code:
  1. // Because our input format is
  2. // Sunday, Monday, Wednesday, Friday, Saturday
  3. // When it is Sunday, we can output it directly, but when
  4. // When the first day is Monday, we must print a space first
  5. // Then output the date to match the number with the day of the week.
  6. // Print space
  7. For (INT spaceno = 0; spaceno <temp; spaceno ++)
  8. {
  9. System. Out. Print ("/t ");
  10. }
  11. // Print numbers in order
  12. For (INT I = 1; I <= inputmonthday; I ++)
  13. {
  14. If (total + I) % 7) // determines if the line feed is correct.
  15. {
  16. System. Out. println (I );
  17. }
  18. Else
  19. {
  20. System. Out. Print (I + "/t ");
  21. }
  22. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.