Displaying calendars

Displaying calendars

Write a program that prompts the user to enter the year and first day of the year, and displays the calendar table for the year on the console. For example, if the user entered the year 2005, and 6 for Saturday, January 1, 2005, your program should display the calendar for each month in the year, as follows:

January 2005
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
...



December 2005
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
View Answers

July 5, 2010 at 1:18 PM

Hi Friend,

Try the following code:

import java.util.*;
public class DisplayCalendar{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter year: ");
int year = input.nextInt();
System.out.print("Enter first day of the year(0-6): ");
int day = input.nextInt();
for (int k=1; k<=12; k++) {
String month="";
int numOfDays=0;
switch (k)
{
case 1:
month="January ";
numOfDays=31;
break;
case 2:
month="February ";
if ((year%4 == 0 && year%100 != 0) || year%400 == 0 )
numOfDays=29;
else numOfDays=28;
break;
case 3:
month="March ";
numOfDays=31;
break;
case 4:
month="April ";
numOfDays=30;
break;
case 5:
month="May ";
numOfDays=31;
break;
case 6:
month="June ";
numOfDays=30;
break;
case 7:
month="July ";
numOfDays=31;
break;
case 8:
month="August ";
numOfDays=31;
break;
case 9:
month="September ";
numOfDays=30;
break;
case 10:
month="October ";
numOfDays=31;
break;
case 11:
month="November ";
numOfDays=30;
break;
case 12:
month="December ";
numOfDays=31;
break;
}
System.out.println(month + year);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int i=1; i<=day; i++)
System.out.print(" ");
for (int j=1; j<=numOfDays; j++){
if (day%7==0 && day!=0)
System.out.println();
System.out.printf("%3d ", j);
day+=1;
}
day%=7;
System.out.print("\n\n");
}
}
}

Thanks









Related Tutorials/Questions & Answers:
Displaying calendars - Java Interview Questions
Displaying calendars  Write a program that prompts the user to enter the year and first day of the year, and displays the calendar table for the year on the console. For example, if the user entered the year 2005, and 6
ModuleNotFoundError: No module named 'connector-calendars'
ModuleNotFoundError: No module named 'connector-calendars'  Hi, My... named 'connector-calendars' How to remove the ModuleNotFoundError: No module named 'connector-calendars' error? Thanks   Hi
Advertisements
ModuleNotFoundError: No module named 'connector-calendars'
ModuleNotFoundError: No module named 'connector-calendars'  Hi, My... named 'connector-calendars' How to remove the ModuleNotFoundError: No module named 'connector-calendars' error? Thanks   Hi
ModuleNotFoundError: No module named 'market-calendars'
ModuleNotFoundError: No module named 'market-calendars'  Hi, My... named 'market-calendars' How to remove the ModuleNotFoundError: No module named 'market-calendars' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'pinax-calendars'
ModuleNotFoundError: No module named 'pinax-calendars'  Hi, My... named 'pinax-calendars' How to remove the ModuleNotFoundError: No module named 'pinax-calendars' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'trading-calendars'
ModuleNotFoundError: No module named 'trading-calendars'  Hi, My... named 'trading-calendars' How to remove the ModuleNotFoundError: No module named 'trading-calendars' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'pandas-market-calendars'
ModuleNotFoundError: No module named 'pandas-market-calendars'  Hi...: No module named 'pandas-market-calendars' How to remove the ModuleNotFoundError: No module named 'pandas-market-calendars' error? Thanks   
ModuleNotFoundError: No module named 'ib-trading-calendars'
ModuleNotFoundError: No module named 'ib-trading-calendars'  Hi...: No module named 'ib-trading-calendars' How to remove the ModuleNotFoundError: No module named 'ib-trading-calendars' error? Thanks   Hi
ModuleNotFoundError: No module named 'quantrocket-trading-calendars'
ModuleNotFoundError: No module named 'quantrocket-trading-calendars'  ...: No module named 'quantrocket-trading-calendars' How to remove the ModuleNotFoundError: No module named 'quantrocket-trading-calendars' error
displaying data
displaying data   how to display data from database on jsp using struts2
Image is not displaying
Image is not displaying  Hii i am using spring javamail with html template to send mail,i am facing that image displaying prob when mail have to cm in gmail,in template just i am giving my current location of image which
How to add two calendars on the same html page
How to add two calendars on the same html page  I have used the same code as on this site http://www.roseindia.net/javascript/javascript... use single calendar for a single html page.but while implementing two calendars
displaying image
displaying image  how to upload image and retrieve it form database mysql in php?   Here is an example of uploading image using php. 1)form.html: (adsbygoogle = window.adsbygoogle || []).push({}); <
PHP Displaying URL Content
PHP Displaying URL Content  In my PHP application form, on submitting the form details it always displaying url content. Can anyone tell me what is the reason and how to restrict it from displaying
codes for displaying in calendar
codes for displaying in calendar  can i get jsp codes for displaying comments, when the pointer is placed over the particular date in calendar
Displaying images - JDBC
Displaying images  How to display multiple images on a single jsp from MySql database
Displaying images - JDBC
Displaying images  How to display multiple images on a single jsp from MySql database
image displaying in java
image displaying in java  how to display an image by using load image button in applet viewer
(Displaying a calendar in GUI
(Displaying a calendar in GUI  (Displaying a calendar) Write a program that displays the calendar for the current month, as Use labels, and set texts on the labels to display the calendar."The Calendar and GregorianCalendar
autocomplete displaying multiple fields
autocomplete displaying multiple fields  How can I create an autocomplete with three field (name, id, phone). Placing the phone field in the appropriate input field but selecting the name field into the 'calling' input box
xml displaying a drives data.....
xml displaying a drives data.....  Hi all, I need a solution for displaying content of a drive(Ex: c , d , e ) in the browser using the XML... all, I need a solution for displaying content of a drive(Ex: c , d , e
Displaying date in the preciding code
Displaying date in the preciding code  while deploying the Bill date field on this page are empty though it should be displayed String userName = request.getParmeter("userName"); String custId = request.getParameter("customerId
displaying in ajax - Ajax
displaying in ajax  hi.. I have an Ajax page ,request gone to server ana details are bought... the response should be displayed in the same request page not in the other page... Thank u.. I will be waiting
Displaying Date in jsp - JDBC
Displaying Date in jsp  I want to insert Date of birth of a person in a database.i am getting input value from HTML and i use jsp application to interact with database.My JSP code for inserting Date is below: String dateStr
Displaying file from database
Displaying file from database  I have list of files in my database. I want to display these files in browser according to the input. My need is like google search result. when i click on the link it should display
Displaying error on the Same form
Displaying error on the Same form  I want to display the errors on the same form(Top of the form) after validation. I am used Div element and Table to display the error messages. It is working properly. But those error messages
Displaying files on selection of date.
Displaying files on selection of date.  Hi, I am developing a GUI, where i select from and to date. On selection of from and to date the GUI should show the particular txt files of the selected date. I want the java logic
Displaying Rows - JSP-Servlet
Displaying Rows  Hi, I need your support on how to display data from ms sql 2000 database into an html form text box and text area, using java servlet or jsp  Hi friend, This is form code, display data
Displaying image using jsp and spring.
Displaying image using jsp and spring.  how to display an image stored in WEB-INF/images folder on the browser using jsp and spring
Data displaying with limited records in jsp
Data displaying with limited records in jsp  How to display table with limited 10 records , after clicking next button another 10 records from database upto last record please help me
Displaying Error pages in results tree in jmeter
Displaying Error pages in results tree in jmeter   Displaying Error pages in results tree in jmeter
Uploading a software and displaying it on the jsp page
Uploading a software and displaying it on the jsp page  I have a Downloads page in my website, I want it to display the name of softwares as soon as it is uploaded in the backend by the administrator. And the admin may be a non
Displaying database values in pdf format
Displaying database values in pdf format  Hi All, I am developing a struts application.I am having one registration form when i am submitting the form the values are stored in database,the database name is registration
displaying a physical webpage with frames in iframe
displaying a physical webpage with frames in iframe  i have iframe in my webpage, i have done the coding part for browsing the folders and viewing...="sss"><%=s%></a> <% } %> mycode for displaying
JSP - Problem of displaying result at webpage
JSP - Problem of displaying result at webpage  Can anyone please help to solve my problem below. My webpage has problem in displaying result position. The result should be display below my title. How do i solve it? Thank you so
reading and displaying svg file through java
reading and displaying svg file through java  sample program for reading a svg file
displaying image in awt - Java Beginners
displaying image in awt  Hi All, I have downloaded the code to display image using awt from here and when I execute the code I am getting error like: D:\netbeans\myjavaappl\src\ImageDemo.java:21: incompatible types
web page altering while displaying servlet response
web page altering while displaying servlet response  in my application one index.jsp and one servlet is there and the servlet contains some db code...(request, response); i have used this code but it is not displaying the page
Displaying data from combobox on the web page
Displaying data from combobox on the web page  Sir, I have to make a project in which a data from the database is to be displayed in combo box and depending on the choice made in combo box the data pertaining to it from
displaying long size arrow line in a jsp
displaying long size arrow line in a jsp  Hi all, How can we display an arrow mark with a specified height and width in a jsp. Please help me in resolving this problem. Thanks, Suresh (adsbygoogle
Displaying data from combobox on the web page
Displaying data from combobox on the web page  Sir, I have to make a project in which a data from the database is to be displayed in combo box and depending on the choice made in combo box the data pertaining to it from
Displaying data from combobox on the web page
Displaying data from combobox on the web page  Sir, I have to make a project in which a data from the database is to be displayed in combo box and depending on the choice made in combo box the data pertaining to it from
Displaying data from combobox on the web page
Displaying data from combobox on the web page  Sir, I have to make a project in which a data from the database is to be displayed in combo box and depending on the choice made in combo box the data pertaining to it from
Displaying data from combobox on the web page
Displaying data from combobox on the web page  Sir, I have to make a project in which a data from the database is to be displayed in combo box and depending on the choice made in combo box the data pertaining to it from
Displaying java.util.Calendar using JSP struts tags
Displaying java.util.Calendar using JSP struts tags  My question is in regard to displaying a date whose source is java.util.Calendar and not java.util.Date. In all the examples I have found they only refer to the java.util.Date
displaying - Ajax
Displaying different portions of a page subsequently on the basis of action
Displaying different portions of a page subsequently on the basis of action  Suppose in Report.jsp there are two text fields From Date: and To Date: with js calender beside these and a SEARCH Button(When the page initially loads
JFree 3D Bar Chart not displaying in JSP - Java3D
JFree 3D Bar Chart not displaying in JSP  Hi I am trying to run JFree 3D bar chart written in your site under link: http://www.roseindia.net/chartgraphs/3Dbarchart-in-jsppage.shtml This is not working. PNG file
displaying employee records and their images problem - JSP-Servlet
displaying employee records and their images problem  hi, Thanks for your reply to my question. The code you sent to me yesterday was not working. it doesn't display any record and image. Please, help me out urgent
Java Cosole not Displaying Greek letters - WebSevices
Java Cosole not Displaying Greek letters  Hello Team, I developed a webservice which reads data from xml string and stores in the data base, i got a new requirement to read Greek and Arabic characters from the xml and store

Ads