Java million Almanac, get the calendar table _android

Source: Internet
Author: User
Tags first row

This article applies java technology to enter the year and month to get the calendar, the following is a piece of code to show you:

Enter the year and month and print out the calendar for this month
1.19 years, January 1, Monday.
2. Calculate input year distance 1900 how many days before the month of January 1
3. Total days%7 come from the beginning of the week
Note: The computer has a minimum of time until 1900, and the UNIX system considers January 1, 1970 0 o'clock to be the time era.
So, in this program did not test the year 1900 years ago. Interested can be studied under their own.

Import Java.util.Scanner;
 Class calender{public static void Main (string[] args) {print ();
 ///print output public static void print () {Scanner sc = new Scanner (system.in);
 System.out.println ("Please enter Year:");
 int year = Sc.nextint ();
 System.out.println ("Please enter the month (1~12):");
 int month = Sc.nextint (); int day = GetDays (year, month);//getdays method details please look down//days+1:day is the total number of days, the total number of days to enter the month is only the number of days before this month,//plus 1 becomes the first day of this month int week = days
 %7==0?1:days%7+1;//the first day of the week is the number of weeks System.out.println ("day \ t two \ t three \ t four \ t five \ t six");
 Output first row (first week) empty part for (int i=1; i<=week; i++) {System.out.print ("T");
 //Output The first line (first week) for each day for (int i=1; i<=7-week; i++) {System.out.print (i+ "\ t");
 } System.out.println ();
 Month days of January-December int monthday = 0;
   Switch (month) {case 2:if (year%4==0&&year%100!=0 | | year%400==0) {monthday=29;
   }else{monthday=28;
  } break;
   Case 4:case 6:case 9:case 11:monthday=30;
  Break
   default:monthday=31;
  Break //Output The remaining date, starting from the second week, so is 8-week for (int i=8-week; i<=monthday;
  i++) {System.out.print (i+ "T");
  Every seven days, when the date is divisible by 7, the line if ((I+week)%7==0) {System.out.println () is changed; }/* To calculate the total number of days from 1900 to 1.1 for the current month./public static int getdays (int years, int month) {//Judge the year is a leap years or excepting, get the total number of years int day1=0
 , day2=0;
  for (int i=1900; i<year; i++) {if (i%4==0&&i%100!=0 | | i%400==0) {day1+=366;
  }else{day1+=365;  The total number of days for the month for (int i=1; i<month; i++) {switch (i) {case 2:if (year%4==0&&year%100!=0 | | year%400==0
   ) {day2+=29;
   }else{day2+=28;
  } break;
   Case 4:case 6:case 9:case 11:day2+=30;
  Break
   default:day2+=31;
  Break
 } return day1+day2;

 }
 }

The above is the Calendar code implementation process, I hope to help you.

Related Article

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.