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>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