通过年份输入和第一天输入获得一个月的第一天

时间:2015-03-23 09:16:26

标签: java date java-8

到目前为止,这是我的代码。我想我一切都错了。 帮助将不胜感激:

年度投入是2013年; 日输入为2(表示星期二)

public class firstMonthDay {

  public static void main(String[] args) {

    Scanner input= new Scanner(System.in);

    System.out.print("Enter Year:");        
    int year = input.nextInt();

    System.out.print("Enter day of the week:");        
    int inputDay = input.nextInt();

    int firstday=0;        
    int daysInMonth=0;        
    int month =0;

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

      switch(daysInMonth){

      case 1: daysInMonth += 31 ;
      case 2: daysInMonth += 28  ;
      case 3: daysInMonth += 31  ;  
      case 4: daysInMonth += 30  ;  
      case 5: daysInMonth += 31  ;  
      case 6: daysInMonth += 30  ;  
      case 7: daysInMonth += 31  ;  
      case 8: daysInMonth += 31  ;  
      case 9: daysInMonth += 30  ;  
      case 10: daysInMonth += 31 ;  
      case 11: daysInMonth += 30 ;  
      case 12: daysInMonth += 31 ;
        break;
      default:


        switch( firstday=daysInMonth-inputDay%7){
        case 1:System.out.print("Monday");break;
        case 2:System.out.print("Tuesday");break;
        case 3:System.out.print("Wednesday");break;
        case 4:System.out.print("Thursday");break;
        case 5:System.out.print("Friday");break;
        case 6:System.out.print("Saturday");break;
        case 7:System.out.print("Sunday");break;
        default:

          while (month<12){ 
            month++;
            switch(month){
            case 1: System.out.println("January");break;
            case 2: System.out.println("Febuary");break;
            case 3: System.out.println("March"); break;
            case 4: System.out.println("April");break;
            case 5: System.out.println("May");break;
            case 6: System.out.println("June");break;
            case 7: System.out.println("july");break;
            case 8: System.out.println("August");break;
            case 9: System.out.println("September");break;
            case 10: System.out.println("October");break;
            case 11: System.out.println("November");break;
            case 12: System.out.println("December");break;
            default:

            }
          }
        }
      }
      System.out.println("the first day of"+month+"is:"+firstday);
    }
  }      
}

结果应该是

"the first day of january is tuesday"
......
"the first day of december is Sunday"

4 个答案:

答案 0 :(得分:2)

你的问题对我来说并不清楚。 您可以使用以下代码,输入日期将为您提供当天。

   String input_date="23/03/2015";
   SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
   Date dt1=format1.parse(input_date);
   DateFormat format2=new SimpleDateFormat("EEEE"); 
   String finalDay=format2.format(dt1);
   System.out.println(finalDay);

答案 1 :(得分:2)

Calendar是你的朋友。

Scanner input= new Scanner(System.in);
System.out.print("Enter Year:");
int year = input.nextInt();
Calendar cal = Calendar.getInstance();
for (int i=0; i<12; i++) {
    cal.set(year, i, 1);
    System.out.printf( "the first day of %s is %s\n",
            cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()),
            cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()) );
}//end for i    

答案 2 :(得分:1)

另外,这个

"the first day of january is tuesday"

永远不会发生,因为在

System.out.println("the first day of"+month+"is:"+firstday);

firstday被声明为int。

另外,请简要说明您要完成的工作。

答案 3 :(得分:1)

无需从头开始编写。使用Java 8中添加的java.time.Monthjava.time.LocalDate

import java.time.Month;
import java.util.Scanner;
import java.time.LocalDate;

public class firstMonthDay {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Year:");
    int year = input.nextInt();
    input.close();

    Month month = Month.JANUARY;
    do {
      System.out.println("The first day of " + month + " is " + LocalDate.of(year, month, 1).getDayOfWeek());
      month = month.plus(1);
    }
    while (!month.equals(Month.JANUARY));
  }
}

输出是:

Enter Year:2015
The first day of JANUARY is THURSDAY
The first day of FEBRUARY is SUNDAY
The first day of MARCH is SUNDAY
The first day of APRIL is WEDNESDAY
The first day of MAY is FRIDAY
The first day of JUNE is MONDAY
The first day of JULY is WEDNESDAY
The first day of AUGUST is SATURDAY
The first day of SEPTEMBER is TUESDAY
The first day of OCTOBER is THURSDAY
The first day of NOVEMBER is SUNDAY
The first day of DECEMBER is TUESDAY