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

3

0

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
}
Signorelli answered 14/10, 2020 at 17:49 Comment(3)
Are you required to use obsolete Calendar class when implementing this task? Or you can use newer Java Time API?Whorl
@Alex Rudenko No I can use Java Time API, I was just thinking of using the Calendar classSignorelli
@Signorelli - If one of the answers resolved your issue, you can help the community by marking that as accepted. An accepted answer helps future visitors use the solution confidently.Phonolite
L
2

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.

Links

Loft answered 14/10, 2020 at 18:6 Comment(0)
W
0

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
Whorl answered 14/10, 2020 at 18:14 Comment(0)
P
0

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:

enter image description here

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.

Phonolite answered 14/10, 2020 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.