Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GregorianCalendar problems

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

     
    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 );
    	}
    }
    Last edited by Crixus; April 23rd, 2011 at 09:27 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: GregorianCalendar problems

    Months in the Calendar or 0 based, so you must take this into account when using the results returned by Calendar

Similar Threads

  1. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 02:51 PM
  2. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  3. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  4. [SOLVED] GregorianCalendar
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 30th, 2009, 04:30 AM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM