Java 需要帮助编写一个返回过去月份和年份的第一天和最后一天的方法吗

Java 需要帮助编写一个返回过去月份和年份的第一天和最后一天的方法吗,java,date,inheritance,calendar,Java,Date,Inheritance,Calendar,任何帮助都将不胜感激。dateTimeInherit类是从dateTimeAbstract继承的类,因此它必须使用daysOfAnyMonth方法。我曾考虑使用YearMonth和LocalDate类中的一些类,但不确定从哪里开始。谢谢 import java.io.IOException; public class Driver { public static void main(String[] args) throws IOException { DateT

任何帮助都将不胜感激。dateTimeInherit类是从dateTimeAbstract继承的类,因此它必须使用daysOfAnyMonth方法。我曾考虑使用YearMonth和LocalDate类中的一些类,但不确定从哪里开始。谢谢

import java.io.IOException;
public class Driver 
{
    public static void main(String[] args) throws IOException 
    {
      DateTimeInherit dateTimeInherit = new DateTimeInherit();
        
         /**
         * From the given line of codes you have to identify the class name and necessary 
         constructors/methods
         * In the following method, the first parameter is the month and the second parameter is the 
         year.
         * We are going to print the first day and the last day (day of the week) of any given month of 
         the year.
         * Output format: for (4, 2020):
         * In the year 2020, for the 4th month: the first day is WEDNESDAY and the last day is 
         THURSDAY       
         */     
        
        dateTimeInherit.daysOfAnyMonth(9, 2020);
        dateTimeInherit.daysOfAnyMonth(10, 2020);
        dateTimeInherit.daysOfAnyMonth(12, 2020);
        System.out.print("\n");




public abstract class DateTimeAbstract 
{
abstract void daysOfAnyMonth(int monthOfYear, int theYear);
}

import java.util.Calendar;
public class DateTimeInherit extends DateTimeAbstract 
{
  // Method that needs to be written
}

我完全同意您使用java.time中的
YearMonth
LocalDate
,这是现代java日期和时间API

将您的月号和年份传递给
YearMonth.of(int,int)
。有关文档,请参阅下面的链接。记住交换论点:今年是第一年。将获取的
YearMonth
对象存储到变量中。使用其
atDay
atEndOfMonth
方法获取月份的第一天和最后一天作为
LocalDate
对象。依次使用
LocalDate.getDayOfWeek()
获取一周中的哪一天。现在,您已经获得了将打印报表组合在一起所需的所有馅饼。所以说吧

快乐编码

链接
  • 解释如何使用java.time
  • 文件:

我完全同意您使用java.time中的
YearMonth
LocalDate
,这是现代java日期和时间API

将您的月号和年份传递给
YearMonth.of(int,int)
。有关文档,请参阅下面的链接。记住交换论点:今年是第一年。将获取的
YearMonth
对象存储到变量中。使用其
atDay
atEndOfMonth
方法获取月份的第一天和最后一天作为
LocalDate
对象。依次使用
LocalDate.getDayOfWeek()
获取一周中的哪一天。现在,您已经获得了将打印报表组合在一起所需的所有馅饼。所以说吧

快乐编码

链接
  • 解释如何使用java.time
  • 文件:

您可以创建
LocalDate
的实例,并在DayOfMonth/lengthOfMonth中使用其方法

static void daysOfMonth(整数月,整数年){
LocalDate firstDay=LocalDate.of(年、月、1);
LocalDate lastDay=firstDay.withDayOfMonth(firstDay.lengthOfMonth());
System.out.printf(“第一天是:%s,最后一天是:%s”,firstDay.getDayOfWeek(),lastDay.getDayOfWeek());
}
测试:

daysOfMonth(2020年9月);
印刷品:

first day is: TUESDAY, last day is: WEDNESDAY

您可以创建
LocalDate
的实例,并在DayOfMonth/lengthOfMonth
中使用其方法

static void daysOfMonth(整数月,整数年){
LocalDate firstDay=LocalDate.of(年、月、1);
LocalDate lastDay=firstDay.withDayOfMonth(firstDay.lengthOfMonth());
System.out.printf(“第一天是:%s,最后一天是:%s”,firstDay.getDayOfWeek(),lastDay.getDayOfWeek());
}
测试:

daysOfMonth(2020年9月);
印刷品:

first day is: TUESDAY, last day is: WEDNESDAY

有很多方法可以使用日期时间API来实现。我建议你用这个

输出:

First day of the month: TUESDAY, Last day of the month: WEDNESDAY
First day of the month: THURSDAY, Last day of the month: SATURDAY
First day of the month: TUESDAY, Last day of the month: THURSDAY
First day of the month: Tuesday, Last day of the month: Wednesday
First day of the month: Tuesday, Last day of the month: Wednesday

First day of the month: Thursday, Last day of the month: Saturday
First day of the month: Thursday, Last day of the month: Saturday

First day of the month: Tuesday, Last day of the month: Thursday
First day of the month: Tuesday, Last day of the month: Thursday
下表总结了现代日期时间API的功能:

注意:如果您需要以适当的大小写打印工作日名称,您可以按如下操作:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Tests
        daysOfAnyMonth(9, 2020);
        daysOfAnyMonth(10, 2020);
        daysOfAnyMonth(12, 2020);
    }

    static void daysOfAnyMonth(int month, int year) {
        LocalDate firstDate = LocalDate.of(year, month, 1);
        LocalDate lastDate = firstDate.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("First day of the month: "
                + firstDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH) + ", Last day of the month: "
                + lastDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));

        // Alternatively: By using DateTimeFormatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH);
        System.out.println("First day of the month: " + firstDate.format(formatter) + ", Last day of the month: "
                + lastDate.format(formatter));
        System.out.println();
    }
}
输出:

First day of the month: TUESDAY, Last day of the month: WEDNESDAY
First day of the month: THURSDAY, Last day of the month: SATURDAY
First day of the month: TUESDAY, Last day of the month: THURSDAY
First day of the month: Tuesday, Last day of the month: Wednesday
First day of the month: Tuesday, Last day of the month: Wednesday

First day of the month: Thursday, Last day of the month: Saturday
First day of the month: Thursday, Last day of the month: Saturday

First day of the month: Tuesday, Last day of the month: Thursday
First day of the month: Tuesday, Last day of the month: Thursday

了解有关现代日期时间API的更多信息,请访问

使用日期时间API有很多方法。我建议你用这个

输出:

First day of the month: TUESDAY, Last day of the month: WEDNESDAY
First day of the month: THURSDAY, Last day of the month: SATURDAY
First day of the month: TUESDAY, Last day of the month: THURSDAY
First day of the month: Tuesday, Last day of the month: Wednesday
First day of the month: Tuesday, Last day of the month: Wednesday

First day of the month: Thursday, Last day of the month: Saturday
First day of the month: Thursday, Last day of the month: Saturday

First day of the month: Tuesday, Last day of the month: Thursday
First day of the month: Tuesday, Last day of the month: Thursday
下表总结了现代日期时间API的功能:

注意:如果您需要以适当的大小写打印工作日名称,您可以按如下操作:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Tests
        daysOfAnyMonth(9, 2020);
        daysOfAnyMonth(10, 2020);
        daysOfAnyMonth(12, 2020);
    }

    static void daysOfAnyMonth(int month, int year) {
        LocalDate firstDate = LocalDate.of(year, month, 1);
        LocalDate lastDate = firstDate.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("First day of the month: "
                + firstDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH) + ", Last day of the month: "
                + lastDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));

        // Alternatively: By using DateTimeFormatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH);
        System.out.println("First day of the month: " + firstDate.format(formatter) + ", Last day of the month: "
                + lastDate.format(formatter));
        System.out.println();
    }
}
输出:

First day of the month: TUESDAY, Last day of the month: WEDNESDAY
First day of the month: THURSDAY, Last day of the month: SATURDAY
First day of the month: TUESDAY, Last day of the month: THURSDAY
First day of the month: Tuesday, Last day of the month: Wednesday
First day of the month: Tuesday, Last day of the month: Wednesday

First day of the month: Thursday, Last day of the month: Saturday
First day of the month: Thursday, Last day of the month: Saturday

First day of the month: Tuesday, Last day of the month: Thursday
First day of the month: Tuesday, Last day of the month: Thursday

了解有关现代日期时间API的更多信息在执行此任务时是否需要使用过时的
日历类?或者你可以使用更新的?@Alex Rudenko不,我可以使用Java时间API,我只是想使用日历class@acc13241-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。接受的答案有助于未来访问者自信地使用解决方案。在执行此任务时,您是否需要使用过时的
日历类?或者你可以使用更新的?@Alex Rudenko不,我可以使用Java时间API,我只是想使用日历class@acc13241-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。公认的答案有助于未来的访问者自信地使用解决方案。