Java Perpetual calendar

Source: Internet
Author: User

Import Java.util.Scanner;

public class Perpetualcalendar {

public static void Main (string[] args) {

TODO auto-generated Method Stub

int year;//Year

int month;//Month

int day=0;//Days

Boolean ren;//is a leap year to true, not false

int totaldays=0;//1900 years to present total number of days

System.out.println ("***************** calendar *****************");

Scanner input = new Scanner (system.in);

System.out.println ("Please enter the Year:");

Year = Input.nextint ();

System.out.println ("Please enter the month:");

month = Input.nextint ();

if ((year%400==0) | | | (year%4==0&&year%100!=0)) {

ren = true; }

else {ren = false;

}

Calculate the total number of days from 1900 to this year

for (int i = 1900;i<year;i++) {

if (ren) {

Totaldays = totaldays+366;

}else {

Totaldays = totaldays+365;

}

}

for (int i = 1;i<=month;i++) {

switch (i) {

Case 1:

Case 3:

Case 5:

Case 7:

Case 8:

Case 10:

Case 12:

Day = 31;

Break

Case 4:

Case 6:

Case 9:

Case 11:

day=30;

Break

Case 2:

if (ren) {

Day = 29;

}

else{

Day = 28;

}

Break

Default

System.out.println ("Incorrect month input");

}

To make day a value equal to the number of days to find a month, it is not added to the total number of days

if (i!=month) {

Totaldays = number of days in total years of totaldays+day;//plus number of days before month of lookup month

}

}

Ask for the day of the week, the number of the front \ t

int befordays;

Befordays = 1 + totaldays%7;

if (befordays = = 7) {

Befordays = 0;//stands for Sunday, 0 \ t

}

System.out.println ("Sunday \ t Monday \ t Tuesday \ t Wednesday \ Thursday \ t Friday \ t Saturday");

First print \ t

for (int i=0;i<befordays;i++) {

System.out.print ("\ t");

}

for (int i=1;i<=day;i++) {

System.out.print (i+ "\ t");

Full Seven line breaks

if ((i+befordays)%7==0) {

System.out.println ();

}

}

System.out.println (month+ "Month has" +day+ "Day");

}

}

Java Perpetual calendar

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.