Click to See Complete Forum and Search --> : question about a calendar program


brandongottier
04-06-2005, 11:37 PM
I am doing this assigment for a class. So far, I am really close to finishing. Basically, I need to have the name of the month and year print above the calendar. Now it just says "1" instead of January. The code is below. Thanks for the help

import java.util.*;


public class Main {
public static void main(String[] args)
{
int startingyear = 0;
int startingmonth = 0;
int endingyear = 0;
int endingmonth = 0;
int yearnow = 0;
int monthnow = 0;

Scanner in = new Scanner(System.in);
boolean theday = false;
//Get first input
while (theday == false)
{
System.out.print("\n");
System.out.print("What is the starting year? ");
startingyear = in.nextInt();
if (startingyear >= 1582 && startingyear <= 3300)
{
theday = true;
}
else
{
System.out.print("Please choose a year between 1582 and 3300.");
System.out.print("\n");
}
}
theday = false;
yearnow = startingyear;
while (theday == false)
{
System.out.print("\n");
System.out.print("What is the starting month? ");
startingmonth = in.nextInt();
if (startingmonth >= 1 && startingmonth <= 12)
{
theday = true;
}
else
{
System.out.print("Please choose a month between 1 and 12.");
System.out.print("\n");
}
}
theday = false;
monthnow = startingmonth;
startingmonth = startingmonth - 1;
while (theday == false)
{
System.out.print("\n");
System.out.print("What is the endinging year? ");
endingyear = in.nextInt();
if (endingyear >= 1582 && endingyear <= 3300 &&
startingyear <= endingyear)
{
theday = true;
}
else
{
System.out.print("Please choose a year between 1582 and 3300. That is greater then the starting year");
System.out.print("\n");
}
}
theday = false;
while (theday == false)
{
System.out.print("\n");
System.out.print("What is the ending month? ");
endingmonth = in.nextInt();
if (endingmonth >= 1 && endingmonth <= 12)
{
theday = true;
}
else
{
System.out.print("Please choose a month between 1 and 12.");
System.out.print("\n");
}
}
theday = false;
endingmonth = endingmonth - 1;


// construct d as current date
GregorianCalendar d = new GregorianCalendar();

// set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);
d.set(Calendar.MONTH, startingmonth);
d.set(Calendar.YEAR, startingyear);

int weekday = d.get(Calendar.DAY_OF_WEEK);

do
{

do
{

// print day
int day = d.get(Calendar.DAY_OF_MONTH);
if (startingmonth == 12)
{
startingmonth = 0;
startingyear += 1;
}
if (day == 1)
{
System.out.println();
startingmonth = startingmonth + 1;
System.out.printf("%13s",startingmonth);
System.out.println();
System.out.printf("%15s",startingyear);
System.out.print("\n");
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int i = Calendar.SUNDAY; i < weekday; i++ )
System.out.print(" ");
}
System.out.printf("%3d", day);
System.out.print(" ");

// start a new line after every Saturday
if (weekday == Calendar.SATURDAY)
System.out.println();

// advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);

}
while (startingmonth == endingmonth);
// the loop exits when d is day 1 of the next month

}
while (d.get(Calendar.YEAR) <= endingyear);
// print final end of line if necessary
if (weekday != Calendar.SUNDAY)
{
System.out.println();
System.out.println();
}
}
}

ray326
04-07-2005, 12:30 AM
Here's what you're looking for.
java.text
Class SimpleDateFormat

java.lang.Object
extended byjava.text.Format
extended byjava.text.DateFormat
extended byjava.text.SimpleDateFormat

brandongottier
04-07-2005, 02:56 AM
where do I put that

buntine
04-07-2005, 07:11 AM
Its not actually code to be implemented. But rather just a class you should look into. It is used to format times and dates.

See the docs: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html\

Regards.