0

I was trying to build a beginner calendar exercise project and all I want is if last date of January ends on Sunday then start next month from Monday. Here is my code;

 public static void main(String[] args) 
    {

        String[] months= {"January","Febuary","March","April","May","June"
                   ,"July","August","september","October","November"
                   ,"December"
                 }; 
        String[] weekdays= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

        int x;

        for(int k=0;k<12;k++) 
          { 
            if(months[k]=="April" ||months[k]=="june" ||months[k]=="September" ||months[k]=="November")
                {x=30;}
            else if(months[k]=="Febuary") 
                {x=28;}
            else {x=31;}

            System.out.print(months[k]+"\n");


           for(int i=0;i<weekdays.length;i++) 
             {    
              System.out.print("\t"+weekdays[i]);
             }System.out.println();

           for(int m=1;m<=x;m++) {

               if(((m-1) %7) == 0)  //line break after 7 characters
               {
                   System.out.println();
               }
               System.out.print("\t"+m);
           }
           System.out.println();
    }
  }

enter image description here

judy
  • 325
  • 3
  • 15
  • 1
    I can't understand your output – Rcordoval Dec 17 '18 at 03:35
  • https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Thiyagu Dec 17 '18 at 03:48
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. – GhostCat Dec 17 '18 at 07:47

2 Answers2

1

I think you use not correct approach. Working with dated relates to working with Locale, because all these names like name for the month, weekdays in different format are already in the JVM. You should use it:

public static void printCalendar(LocalDate date, Locale locale) {
    DateFormatSymbols symbols = new DateFormatSymbols(locale);
    WeekFields weekFields = WeekFields.of(locale);

    printMonthName(symbols, date);
    printWeekdayNames(symbols, weekFields);
    printWeekdays(weekFields, date);
}

private static void printMonthName(DateFormatSymbols symbols, LocalDate date) {
    System.out.println(symbols.getMonths()[date.getMonthValue() - 1]);
}

private static void printWeekdayNames(DateFormatSymbols symbols, WeekFields weekFields) {
    String[] weekdays = symbols.getShortWeekdays();
    DayOfWeek firstDayOfWeek = weekFields.getFirstDayOfWeek();
    int offs = firstDayOfWeek == DayOfWeek.SUNDAY ? 1 : firstDayOfWeek.ordinal() + 2;

    for (int i = 0; i < 7; i++)
        System.out.print('\t' + (offs + i >= weekdays.length ? weekdays[(offs + i) % 7] : weekdays[offs + i]));

    System.out.println();
}

private static void printWeekdays(WeekFields weekFields, LocalDate date) {
    LocalDate cur = date.withDayOfMonth(1).with(weekFields.dayOfWeek(), 1);
    boolean stop = false;

    do {
        if (cur.getMonthValue() == date.getMonthValue())
            System.out.format("\t%2d", cur.getDayOfMonth());
        else
            System.out.format("\t  ");

        cur = cur.plusDays(1);

        if (cur.getDayOfWeek() == weekFields.getFirstDayOfWeek()) {
            System.out.println();
            stop = cur.getMonthValue() != date.getMonthValue();
        }
    } while (!stop);
}

Test1:

printCalendar(LocalDate.of(2018, Month.DECEMBER, 17), Locale.US);

December
    Sun Mon Tue Wed Thu Fri Sat
                             1
     2   3   4   5   6   7   8
     9  10  11  12  13  14  15
    16  17  18  19  20  21  22
    23  24  25  26  27  28  29
    30  31

Test2:

printCalendar(LocalDate.of(2018, Month.DECEMBER, 17), Locale.ITALIAN);

dicembre
    lun mar mer gio ven sab dom
                         1   2
     3   4   5   6   7   8   9
    10  11  12  13  14  15  16
    17  18  19  20  21  22  23
    24  25  26  27  28  29  30
    31
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

Your program still needs some adjustments: 1. The first day of the year can be sunday or other day of the week. 2. If the last day of the of the month is sunday, you should continue the order and not starting with sunday from the beginning.

I will propose you to adjust you current code, or you can use an existing library https://www.javatpoint.com/java-util-calendar.

user3675515
  • 155
  • 2
  • 10
  • hi, thank you. please see the code again which i edited and please tell me how to fix it. – judy Dec 17 '18 at 06:03