java - 如何使用java日历获取该月的第一天?

标签 java calendar dayofweek java-calendar

我编写了一个程序,该程序接受用户输入的年份和月份,并尝试打印出月份。我可以打印月份,并且我的间距有效。然而,我却无法抽出时间去工作。该月的第一天对于 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();

  }

最佳答案

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

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 

关于java - 如何使用java日历获取该月的第一天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49616176/

相关文章:

python - 从 Pandas 时间序列生成星期几箱线图的最佳方法

Java继承: How to remove items from an ArrayList depending on a property which is not owned by all of the items?

java - 使用 Azure java sdk 更新用户和组属性

c# - 一个窗口中有 25 个 WPF 日历,打开窗口需要 5 秒

java:日历没有获得正确的日期/时间?

java - 将日历集 DAY_OF_WEEK 转换为使用 TemporalAdjusters

java - 我的自定义 Log4j2 XML 配置文件 Java 中的记录时间不正确

java - try-with-resources 中的 catch 是否覆盖了括号中的代码?

python - 使用 Selenium 和 Python 的日历选择器

Java DayOfWeek 将日期名称(字符串)转换为日期编号