How can I write a method that returns the first and last day of a passed month and year?

huangapple go评论87阅读模式
英文:

How can I write a method that returns the first and last day of a passed month and year?

问题

// `dateTimeInherit`类是从`dateTimeAbstract`继承的类,因此必须使用`daysOfAnyMonth`方法。我考虑使用`YearMonth`和`LocalDate`类中的一些类,但不确定接下来该怎么做。

import java.io.IOException;

public class Driver 
{
    public static void main(String[] args) throws IOException 
    {
        DateTimeInherit dateTimeInherit = new DateTimeInherit();
        
        /**
         * 从给定的代码行中,您需要识别类名和必要的构造函数/方法。
         * 在以下方法中,第一个参数是月份,第二个参数是年份。
         * 我们将打印给定年份的任何月份的第一天和最后一天(星期几)。
         * 输出格式:对于 (4, 2020):
         * 在2020年,对于第4个月:第一天是星期三,最后一天是星期四
         */	    
        
        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 
{
    // 需要编写的方法
}
英文:

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.

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
}

答案1

得分: 2

我绝对同意您使用 YearMonthLocalDate 来自现代 Java 日期和时间 API。

将您的月份和年份传递给 YearMonth.of(int, int) 方法。有关文档,请参阅下面的链接。请记住交换参数顺序:先是年份,然后是月份。将获得的 YearMonth 对象存储在一个变量中。使用其 atDayatEndOfMonth 方法获取该月的第一天和最后一天,分别作为 LocalDate 对象。接着,使用 LocalDate.getDayOfWeek() 来获取星期几。

链接

英文:

I absolutely agree with you to use YearMonth and LocalDate from java.time, the modern Java date and time API.

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.

答案2

得分: 0

你可以创建 LocalDate 的实例并使用它的方法 withDayOfMonth / lengthOfMonth

static void daysOfMonth(int month, int year) {
    LocalDate firstDay = LocalDate.of(year, month, 1);
        
    LocalDate lastDay = firstDay.withDayOfMonth(firstDay.lengthOfMonth());
        
    System.out.printf("first day is: %s, last day is: %s", firstDay.getDayOfWeek(), lastDay.getDayOfWeek());
}

测试:

daysOfMonth(9, 2020);

输出:

first day is: TUESDAY, last day is: WEDNESDAY
英文:

You could create instance of LocalDate and use its methods withDayOfMonth / lengthOfMonth:

static void daysOfMonth(int month, int year) {
    LocalDate firstDay = LocalDate.of(year, month, 1);
        
    LocalDate lastDay = firstDay.withDayOfMonth(firstDay.lengthOfMonth());
        
    System.out.printf("first day is: %s, last day is: %s", firstDay.getDayOfWeek(), lastDay.getDayOfWeek());
}

test:

daysOfMonth(9, 2020);

prints:

first day is: TUESDAY, last day is: WEDNESDAY

答案3

得分: 0

以下是您要的翻译内容:

有许多种方法可以使用日期时间 API 来完成。我建议您使用现代日期时间及其相应的格式化 API

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        // 测试
        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("本月的第一天:" + firstDate.getDayOfWeek() + ",本月的最后一天:" + lastDate.getDayOfWeek());
    }
}

输出:

本月的第一天:TUESDAY,本月的最后一天:WEDNESDAY
本月的第一天:THURSDAY,本月的最后一天:SATURDAY
本月的第一天:TUESDAY,本月的最后一天:THURSDAY

下表总结了现代日期时间 API 的方法命名约定

How can I write a method that returns the first and last day of a passed month and year?

注意: 如果您需要以正确的大小写打印星期几的名称,可以按如下方式进行:

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) {
        // 测试
        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("本月的第一天:" + firstDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)
                + ",本月的最后一天:" + lastDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));

        // 或者:通过使用 DateTimeFormatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH);
        System.out.println("本月的第一天:" + firstDate.format(formatter) + ",本月的最后一天:" + lastDate.format(formatter));
        System.out.println();
    }
}

输出:

本月的第一天:Tuesday,本月的最后一天:Wednesday
本月的第一天:Thursday,本月的最后一天:Saturday

本月的第一天:Tuesday,本月的最后一天:Thursday
本月的第一天:Tuesday,本月的最后一天:Thursday

了解更多关于现代日期时间 API 的内容,请参阅**教程:日期时间**。

英文:

There are so many ways to do it using date-time API. I suggest you use the modern date-time and their corresponding formatting API.

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

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() + ", Last day of the month: "
				+ lastDate.getDayOfWeek());
	}
}

Output:

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

The following table summarizes the Method Naming Conventions of the modern date-time API:

How can I write a method that returns the first and last day of a passed month and year?

Note: If you need to print the weekday name in the proper case, you can do so as follows:

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();
	}
}

Output:

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

Learn more about the modern date-time API at Trail: Date Time.

huangapple
  • 本文由 发表于 2020年10月15日 01:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64358862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定