Good morning. I am writing a calendar function, where there are 2 arguments a month and a year. The function returns a 2-dimensional array and builds a calendar for a specific year. I compare localDate with the key if the return value is the same. How can I transfer it to build my calendar correctly?

package lang; import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class GetFunWithJavaLang { public static int[][] Cal(int month, int year) throws IOException { LocalDate localDate = LocalDate.of(year, month, 1); System.out.println(localDate.getDayOfWeek()); HashMap<String , Integer> map = new HashMap<String , Integer>(); map.put("SUNDAY", 7); map.put("MONDAY", 1); map.put("TUESDAY", 2); map.put("WEDNESDAY", 3); map.put("THURSDAY", 4); map.put("FRIDAY", 5); map.put("SATURDAY", 6); for(Entry entry: map.entrySet()) { String key = (String) entry.getKey(); if (key.equals(localDate)) { int value = (int) entry.getValue(); } } int[] days = {31,28,31,30,31,30,31,31,30,31,30,31}; int[][] calen = new int[6][7]; int k=0; for (int i = 0 ; i<calen.length ; i++) { for (int j=0 ; j<calen.length ; j++) { if (days[month-1] > k) { k++; calen[i][j] = k; } } } for (int i = 0; i < calen.length; i++, System.out.println(" ")) { for (int j = 0; j < calen[i].length; j++) { System.out.print(calen[i][j] + " "); } } return calen; } public static void main(String[] args) { Cal(1, 1998); } } 
  • one
    ideone.com/R1jVH7 returns 5, that is, the 5th day of the week - Senior Pomidor
  • @SeniorAutomator now, as usual, it turns out that standard classes cannot be used ... - GreyGoblin
  • not understood. that is, you cannot use standard classes right now or will it turn out like this? - Senior Pomidor

1 answer 1

If I understand correctly, then you want to get the day of the week for the specified date. Namely, for the beginning of the month. This is done very simply if you use the new API for dates:

 LocalDate localDate = LocalDate.of(1998, 1, 1); System.out.println(localDate.getDayOfWeek()); 

The output will be:

THURSDAY