Enter a date for the first day of the year

Source: Internet
Author: User

There are two ways to implement

Method One:

int year,month,date;
int day;
int sum=0;
Boolean flag=false;
do{
Scanner scan=new Scanner (system.in);
System.out.println ("Please enter the Year");
Year=scan.nextint ();
System.out.println ("Please enter the month");
Month=scan.nextint ();
System.out.println ("Please enter Date");
Date=scan.nextint ();
if (year<0| | month<0| | month>12| | date<0| | DATE&GT;31)
{
SYSTEM.OUT.PRINTLN ("Input error, please re-enter");
Flag=true;
}
}while (flag);
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;
Sum=sum+day;break;
Case 4:
Case 6:
Case 9:
Case 11:day=30;
Sum=sum+day;break;
Case 2:
if ((year%400==0) && (year%4==0) && (year%100!=0)) {
day=29;
Sum=sum+day;break;

}else{
day=28;
Sum=sum+day;break;
}
}


}
Sum=sum+date;
SYSTEM.OUT.PRINTLN (year+ "year" +month+ "month" +date+ "Day is the first" +sum+ "Day of this year. ");
}

Method Two:

Scanner sd=new Scanner (system.in);
System.out.println ("Please enter the Year:");
int Year=sd.nextint ();
System.out.println ("Please enter the month:");
int Month=sd.nextint () -1;//month is calculated starting from 0, so here-1
System.out.println ("Please enter the day:");
int Date=sd.nextint ();
Calendar calendar=calendar.getinstance ();//Generate a Calendar object
Calendar.set (year,month,date);//Set Calendar month Day
int Temp=calendar.get (calendar.day_of_year);//Get the first day of the year
System.out.println ("The date you entered is the year" +temp+ "Day");

Enter a date for the first day of the year

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.