Use calendar to output the annual calendar of a specified year

Source: Internet
Author: User
Tags gcal
  1. /**
  2. * @ Author bzwm
  3. *
  4. */
  5. Import java. Io. bufferedreader;
  6. Import java. Io. ioexception;
  7. Import java. Io. inputstreamreader;
  8. Import java. util. calendar;
  9. Import java. util. gregoriancalendar;
  10. Public class calendartest {
  11. Public static void main (string [] ARGs) throws ioexception {
  12. Bufferedreader in = new bufferedreader (New inputstreamreader (system. In ));
  13. System. Out. Print ("enter a year :");
  14. String years = in. Readline ();
  15. Calendarprinter CP = new calendarprinter (years );
  16. CP. printcal ();
  17. }
  18. }
  19. Class calendarprinter {
  20. Private int year;
  21. Private Static final int monthcount = 12;
  22. Public calendarprinter (string years ){
  23. If (! Years. Matches ("// d {4 }")){
  24. System. Out. println ("year that inputted is illagel .");
  25. Return;
  26. }
  27. Year = integer. parseint (years );
  28. }
  29. Public void printcal (){
  30. // Construct D as current date
  31. Gregoriancalendar Gcal = new gregoriancalendar ();
  32. // Set year
  33. Gcal. Set (calendar. year, year );
  34. For (INT month = 0; month <monthcount; month ++ ){
  35. Gcal. Set (calendar. Month, month );
  36. Printout (Gcal );
  37. }
  38. }
  39. Private void printout (calendar Cal ){
  40. Int month = Cal. Get (calendar. month );
  41. // Set Cal to start date of the month
  42. Cal. Set (calendar. day_of_month, 1 );
  43. Int weekday = Cal. Get (calendar. day_of_week );
  44. // Print heading
  45. System. Out. println ("Sun mon Tue wed Thu Fri Sat ");
  46. // Indent first line of calendar
  47. For (INT I = calendar. Sunday; I <weekday; I ++)
  48. System. Out. Print ("");
  49. Do {
  50. // Print day
  51. Int day = Cal. Get (calendar. day_of_month );
  52. If (day> 0)
  53. System. Out. Print ("" + day + "");
  54. Else
  55. System. Out. Print ("" + day + "");
  56. // Start a new line after every Saturday
  57. If (weekday = calendar. Saturday)
  58. System. Out. println ();
  59. // Advance D to the next day
  60. Cal. Add (calendar. day_of_month, 1 );
  61. Weekday = Cal. Get (calendar. day_of_week );
  62. } While (Cal. Get (calendar. month) = month );
  63. // The loop exits when D is day 1 of the next month
  64. // Print final end of line if necessary
  65. If (weekday! = Calendar. Sunday)
  66. System. Out. println ();
  67. }
  68. }

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.