GregorianCalendar problems
Hey, I need to work out the date and day it will be a 100 days from now.
I came up with the following code by looking at the API and a few tutorials but for some reason its wrong, it gives me the result as 2/7/2011 and the day being Tuesday.
First, the date given is not 100 days from now.
Second, the 2nd of July 2011 is a Saturday.
Anyone know what I am doing wrong? Would appreciate the help, thank you.
Code Java:
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Calender
{
public static void main(String Args[])
{
GregorianCalendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_MONTH, 100); //add 100 days to current date
int year = cal.get(Calendar.YEAR);//get year
int month = cal.get(Calendar.MONTH);//get month
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); //get day of month
int weekday = cal.get(Calendar.DAY_OF_WEEK);//gets the day of the week.
String dayweek = "Notset";//day of week
switch(weekday)
{
case 1: dayweek = "Sunday"; break;
case 2: dayweek = "Monday"; break;
case 3: dayweek = "Tuesday"; break;
case 4: dayweek = "Wednesday"; break;
case 5: dayweek = "Thursday"; break;
case 6: dayweek = "Friday"; break;
case 7: dayweek = "Saturday"; break;
default:dayweek = "Invalid day"; break;
}
System.out.println("The date, 100 days from today will be: " + dayOfMonth + "/" + month + "/" + year);
System.out.println("The day, 100 days from today will be: " + dayweek );
}
}
Re: GregorianCalendar problems
Months in the Calendar or 0 based, so you must take this into account when using the results returned by Calendar