需要帮助,编写返回过去的月份和年份的第一天和最后一天的方法 [英] Need help writing a method that returns the first and last day of a passed month and year
问题描述
任何帮助将不胜感激. dateTimeInherit类是从dateTimeAbstract继承的类,因此它必须使用daysOfAnyMonth方法.我当时在考虑使用YearMonth和LocalDate类中的一些类,但是不确定从那里开始.谢谢!
Any help would be greatly appreciated. The dateTimeInherit class is an inherited class from dateTimeAbstract so it must use the daysOfAnyMonth method. I was thinking of using some classes from the YearMonth and LocalDate classes but was not sure where to go from there. Thanks!
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日期和时间API java.time中的YearMonth
和LocalDate
.
I absolutely agree with you to use YearMonth
and LocalDate
from java.time, the modern Java date and time API.
将您的月份和年份传递到YearMonth.of(int, int)
.有关文档,请参见下面的链接.记住要交换论点:年份是第一位.将获得的YearMonth
对象存储到变量中.使用其atDay
和atEndOfMonth
方法获取每月的第一天和最后一天作为LocalDate
对象.依次使用LocalDate.getDayOfWeek()
获取星期几.现在,您已经获得了将打印语句放在一起所需的所有便利条件.所以,继续...
Pass your month number and year to YearMonth.of(int, int)
. See the link below for the documentation. Remember to swap the arguments: the year goes first. Store the obtained YearMonth
object into a variable. Use its atDay
and atEndOfMonth
methods for obtaining the first and the last day of the month as LocalDate
objects. In turn use LocalDate.getDayOfWeek()
for getting the day of the week. Now you have got all the piexes you need for putting the print statement together. So go ahead …
快乐的编码.
- Oracle教程:日期时间解释了如何使用java.time. /li>
- 文档:
- Oracle tutorial: Date Time explaining how to use java.time.
- Documentation:
YearMonth.of(int, int)
YearMonth.atDay()
YearMonth.atEndOfMonth()
LocalDate.getDayOfWeek()
这篇关于需要帮助,编写返回过去的月份和年份的第一天和最后一天的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!