【问题标题】:How do i get the first day of the month by using java calendar?如何使用 java 日历获取每月的第一天?
【发布时间】:2018-09-11 23:01:45
【问题描述】:

我编写了一个程序,它接收用户输入的年份和月份,并试图打印出月份。我可以打印月份并且我的间距正在工作。但是,我无法得到工作的日子。这个月的第一天适合 2018 年 1 月,但当我在另一年或更晚的月份做这件事时就不适合了。我必须使用 java 包日历。我在下面打印了我的代码我的代码有问题吗?有什么办法可以解决吗?

import java.util.Calendar;
import.java.util.Scanner;
public class MonthCalendar {
  public static void main(String[] args) {
    int year; // year
    int startDayOfMonth;
    int spaces;
    int month;

    //Creates a new Scanner
    Scanner scan = new Scanner(System.in);

    //Prompts user to enter year
    System.out.println("Enter a year: ");
    year = scan.nextInt();

    //Prompts user to enter month
    System.out.println("Enter the number of the month: ");
    month = scan.nextInt();


    //Calculates the 1st day of that month
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, 1);
    int day = cal.get(Calendar.DAY_OF_WEEK) - 1;

    // months[i] = name of month i
    String[] months = {
      " ",
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
    };

    // days[i] = number of days in month i
    int[] days = {
      0,
      31,
      28,
      31,
      30,
      31,
      30,
      31,
      31,
      30,
      31,
      30,
      31
    };


    // check for leap year
    if ((((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) && month == 2)
      days[month] = 29;


    // print calendar header
    // Display the month and year
    System.out.println("              " + months[month] + " " + year);

    // Display the lines
    System.out.println("___________________________________________");
    System.out.println("  Sun   Mon   Tue   Wed   Thu   Fri   Sat");

    // spaces required
    spaces = (days[month + 1] + day) % 7;

    // print the calendar
    for (int i = 0; i < spaces; i++)
      System.out.print("      ");
    for (int i = 1; i <= days[month]; i++) {
      System.out.printf(" %4d ", i);
      if (((i + spaces) % 7 == 0) || (i == days[month])) System.out.println();
    }

    System.out.println();

  }

【问题讨论】:

  • days[month] = 29; 应该是days[2] = 29;
  • 还有spaces = day;
  • @JohnnyMopp days[month] == days[2] 因为在 if 条件下,检查了 month == 2
  • @clinomaniac 好的,错过了。无论哪种方式都有效。
  • 看来你需要学习使用调试器了。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请随时回来提出更具体的问题。

标签: java calendar dayofweek java-calendar


【解决方案1】:

正如 cmets 中指出的那样,您的问题不在于日期计算本身,而在于您最初设置空格的方式:

spaces = (days[month+1] + day )%7;

应该是:

spaces = day;

您只需要知道您所在的工作日,就知道您在第一周必须移动多远的空间。因此,如果您在星期日,则提前 0 个空格,但如果您在星期二,则希望提前 2 个空格,依此类推。最后,您前进的空间与您的起始工作日一样多,这就是 day 变量所持有的。

Take a look at the code giving the proper output for February 2018 in Ideone

产生以下输出:

Enter a year: 
2018
Enter the number of the month: 
2
              February 2018
___________________________________________
  Sun   Mon   Tue   Wed   Thu   Fri   Sat
                            1     2     3 
    4     5     6     7     8     9    10 
   11    12    13    14    15    16    17 
   18    19    20    21    22    23    24 
   25    26    27    28 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多