In this post we will compute the number of hours in the specified month and year. This could be used to determine the cost for a feature if enabled every day for a specified month and year.
If one rounds up all months to have a fixed set of days, the result would not be correct. For example, a year has 12 months. Most months have a fixed set of days e.g., Jan, Mar, May, Jul, Aug, Oct, and Dec always have 31 days while Apr, Jun, Aug and Nov always have 30 days. This is not the case with Feb which on regular years it holds 28 days but on leap years it holds 29 days.
What we need is some function or method that given as arguments the month and the year it would return the number of days. Once the number of days is known, we may multiply the days by 24 to get the number of hours in the month.
If interested please take a few minutes to research and experiment with a solution.
The code in this post was developed in Java using the VSCode IDE from Microsoft. I need to disclose that at this time I work for Microsoft and use Visual Studio and VSCode.
main >>> month [1..12]: 1 main <<< month: 1 main >>> year: 2023 main <<< year: 2023 main <<< # of days in Jan, 2023: 31 main <<< # of hours in Jan, 2023: 744
Let’s start by specifying January of 2023. It seems that it has 30 days with 24 hours per day for a total of 744 hours in the month. The month of January will always have 31 days and 744 hours independent of the year.
main >>> month [1..12]: 4 main <<< month: 4 main >>> year: 2023 main <<< year: 2023 main <<< # of days in Apr, 2023: 30 main <<< # of hours in Apr, 2023: 720
Let’s now take a look at the month of April. April always has 30 days. The number of hours is 30 * 24 = 724 hours.
main >>> month [1..12]: 2 main <<< month: 2 main >>> year: 2023 main <<< year: 2023 main <<< # of days in Feb, 2023: 28 main <<< # of hours in Feb, 2023: 672
In this example we look at February in a non-leap year. The month will have 28 days * 24 hours = 672 hours.
main >>> month [1..12]: 2 main <<< month: 2 main >>> year: 2024 main <<< year: 2024 main <<< # of days in Feb, 2024: 29 main <<< # of hours in Feb, 2024: 696
In this last example we revisit the month of February but in this case it belongs in a leap year. The number of days is 29 which produces 696 hours.
As you can tell, in order to provide accurate customer billings we need to determine the number of hours in each month to figure out how to bill the customer. In addition, we need to implement a mechanism that provides an accurate count of hours in which the feature was used by the customer. Think about an IoT system which is receiving a relatively fixed rate data indicating if the feature is being used.
/** * Test scaffold * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // **** initialization **** String[] monthNames = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; // **** open a buffered reader **** BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // **** get m number (1 == Jan, ..., 12 == Dec) **** System.out.print("main >>> month [1..12]: "); int m = Integer.parseInt(br.readLine().trim()); if (m < 1 || m > 12) { System.err.println("main <<< unexpected month: " + m); System.exit(-1); } // ???? ???? System.out.println("main <<< month: " + m); // **** get the year **** System.out.print("main >>> year: "); int y = Integer.parseInt(br.readLine().trim()); // ???? ???? System.out.println("main <<< year: " + y); // **** close the buffered reader **** br.close(); // **** compute the number of days in the specified month of the specified year **** int days = Days(m, y); // **** display number of days in the specified month of the specified year **** System.out.println("main <<< # of days in " + monthNames[m - 1] + ", " + y + ": " + days); // **** compute and display the number of hours in the specified month of the specified year **** int hours = days * 24; System.out.println("main <<< # of hours in " + monthNames[m - 1] + ", " + y + ": " + hours); }
The test scaffold for our project starts by initializing an array of months which can be used to map the number associated with the month to a string holding the first three letters of each month.
We then use a buffered reader to read the month and year of interest. The buffered reader is then closed.
We make use of the Days() method to compute the number of days associated with the specified month and year. The number of days is then displayed.
We then multiply the number of days by 24 hours per day to get the number of hours associated with the specified month and year.
/** * Compute the number of days in the specified month of the specified year. * @param m * @param y * @return */ static int Days(int m, int y) { // **** initialization **** int days = 0; // **** these months hold 31 days **** if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { days = 31; } // **** these months hold 30 days **** else if (m == 4 || m == 6 || m == 9 || m == 11) { days = 30; } // **** feb may hold 28 or 29 days **** if (m == 2) { if (y % 400 == 0 && y % 100 == 0) { days = 29; } else { if (y % 4 == 0 && y % 100 != 0) { days = 29; } else { days = 28; } } } // **** return the number of days in the specified month of the specified year **** return days; }
The function of interest follows.
It associated the months with 31 days with such value. The months with 30 days are also associated with the proper value.
The remaining task is to take care of the number of days in the month of February. If the year is leap, the number of days is set to 29; otherwise it is set to 28. The proper number of days is then returned.
To learn more about leap years you can read about it here.
Hope you enjoyed this post. You never know when you will need to make use of the number of hours in a month.
Keep on reading and experimenting. It is the best way to learn.
If interested, the code in this blog may be found here.
Enjoy;
John