Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:32:09+00:00 2026-06-12T09:32:09+00:00

Okay, so I have this code, it prompts a month and a year from

  • 0

Okay, so I have this code, it prompts a month and a year from the user and prints the calendar for that month. I’m having some problems though.

  1. the HTML font editing only affects the month.
  2. the days of the week are not aligned in columns correctly.

Thanks!

package calendar_program;

import javax.swing.JOptionPane;

public class Calendar {

public static void main(String[] args) {
    StringBuilder result=new StringBuilder();

    // read input from user
    int year=getYear();
    int month=getMonth();

    String[] allMonths={
            "", "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
    };

    int[] numOfDays= {0,31,28,31,30,31,30,31,31,30,31,30,31};

    if (month == 2 && isLeapYear(year)) numOfDays[month]=29;

    result.append("    "+allMonths[month]+ "  "+ year+"\n"+" S  M  Tu  W Th  F  S"+"\n");

    int d= getstartday(month, 1, year);

    for (int i=0; i<d; i++){
        result.append("    ");
                // prints spaces until the start day
    }
    for (int i=1; i<=numOfDays[month];i++){
        String daysSpace=String.format("%4d", i);
        result.append(daysSpace);
        if (((i+d) % 7==0) || (i==numOfDays[month])) result.append("\n");
    }
    //format the final result string
    String finalresult= "<html><font face='Arial'>"+result; 
    JOptionPane.showMessageDialog(null, finalresult);
}

//prompts the user for a year
public static int getYear(){
    int year=0;
    int option=0;
    while(option==JOptionPane.YES_OPTION){
        //Read the next data String data
        String aString = JOptionPane.showInputDialog("Enter the year (YYYY) :");
        year=Integer.parseInt(aString);
        option=JOptionPane.NO_OPTION;
    }
    return year;
}

//prompts the user for a month
public static int getMonth(){
    int month=0;
    int option=0;
    while(option==JOptionPane.YES_OPTION){
        //Read the next data String data
        String aString = JOptionPane.showInputDialog("Enter the month (MM) :");
        month=Integer.parseInt(aString);
        option=JOptionPane.NO_OPTION;
    }
    return month;
}

//This is an equation I found that gives you the start day of each month
public static int getstartday(int m, int d, int y){
    int year = y - (14 - m) / 12;
    int x = year + year/4 - year/100 + year/400;
    int month = m + 12 * ( (14 - m) / 12 ) - 2;
    int num = ( d + x + (31*month)/12) % 7;
    return num;
}

//sees if the year entered is a leap year, false if not, true if yes
public static boolean isLeapYear (int year){
    if ((year % 4 == 0) && (year % 100 != 0)) return true;
    if (year % 400 == 0) return true;
    return false;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:32:11+00:00Added an answer on June 12, 2026 at 9:32 am

    Here’s a rather stupid idea.

    Rather then formatting your results using spaces, which may be affected by variance in the individual font widths of a variable width font…use a HTML table instead, or a JTable, or JXMonthView from the SwingX project

    HTML Table

    enter image description here

    String dayNames[] = {"S", "M", "Tu", "W", "Th", "F", "S"};
    result.append("<html><font face='Arial'>");
    result.append("<table>");
    result.append("<tr>");
    for (String dayName : dayNames) {
        result.append("<td align='right'>").append(dayName).append("</td>");
    }
    result.append("</tr>");
    result.append("<tr>");
    for (int i = 0; i < d; i++) {
        result.append("<td></td>");
    }
    for (int i = 0; i < numOfDays[month]; i++) {
        if (((i + d) % 7 == 0)) {
            result.append("</tr><tr>");
        }
        result.append("<td align='right'>").append(i + 1).append("</td>");
    }
    result.append("</tr>");
    result.append("</table>");
    
    result.append("</html>");
    

    JTable Example

    enter image description here

    MyModel model = new MyModel();
    
    List<String> lstRow = new ArrayList<String>(7);
    for (int i = 0; i < d; i++) {
        lstRow.add("");
    }
    for (int i = 0; i < numOfDays[month]; i++) {
        if (((i + d) % 7 == 0)) {
            model.addRow(lstRow);
            lstRow = new ArrayList<String>(7);
        }
        lstRow.add(Integer.toString(i + 1));
    }
    
    if (lstRow.size() > 0) {
        while (lstRow.size() < 7) {
            lstRow.add("");
        }
        model.addRow(lstRow);
    }
    
    JTable table = new JTable(model);
    // Kleopatra is so going to kill me for this :(
    Dimension size = table.getPreferredScrollableViewportSize();
    size.height = table.getRowCount() * table.getRowHeight();
    table.setPreferredScrollableViewportSize(size);
    
    JOptionPane.showMessageDialog(null, new JScrollPane(table));
    
    public static class MyModel extends AbstractTableModel {
    
        public static final String[] DAY_NAMES = {"S", "M", "Tu", "W", "Th", "F", "S"};
        private List<List<String>> lstRowValues;
    
        public MyModel() {
            lstRowValues = new ArrayList<List<String>>(25);
        }
    
        @Override
        public int getRowCount() {
            return lstRowValues.size();
        }
    
        @Override
        public String getColumnName(int column) {
            return DAY_NAMES[column];
        }
    
        @Override
        public int getColumnCount() {
            return 7;
        }
    
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            List<String> rowData = lstRowValues.get(rowIndex);
            return rowData.get(columnIndex);
        }
    
        public void addRow(List<String> lstValues) {
            lstRowValues.add(lstValues);
    
            fireTableRowsInserted(getRowCount(), getRowCount());
        }
    }
    

    Or you can go have a look at JXMonthView

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this code in python that writes some values to a Dictionary
I have this value from database: '2009-1-1 00:00:00', okay, let me paste my code:
I have this piece of code I found on some blog, that's supposed to
Okay so i have this code: if (isset($_GET['book'])) { $query= SELECT book_id, title, authors.`author`
Okay, I have this code: <script language=javascript type=text/javascript> <!-- function requestPage(page) { var request
Okay, so i have this code i wrote: class Connection { public static StreamWriter
Okay, so I have this form that is set to preload a DB record
Okay, so this is weird I have this code - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I have this code, and for some reason my $.post function fires twice in
Okay so my question is this. Say I have a simple C++ code: #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.