Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Gregorian Calendar

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Post Gregorian Calendar

    Hi All!
    I am having difficulties with GregorianCalendar class, working with a java book that asks to implement program that prints these results:
    1) the date and weekday that is hundred days from today
    2) the weekday of my birthday
    3) the date that is 10,000 days from my bday.
    So far, I have this:
    Java Code:
    import java.util.GregorianCalendar;
    
    public class Calendar {
    
    	private static int DAY_OF_MONTH;
    	private static int MONTH;
    	private static int YEAR;
    	private static int DAY_OF_WEEK;
    	private static int JUNE;
    
    	/**
    	 * @param args
    	 */
    
    	public static void main(String[] args) {
    		
    		GregorianCalendar cal = new GregorianCalendar(); //today's date
    		GregorianCalendar mybday = new GregorianCalendar(1987, Calendar.JUNE, 9); //my birthday
    		
    		
    		// now cal is a hundred days from today
    		cal.add(Calendar.DAY_OF_MONTH, 100);
    		
    		int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
    		int month = cal.get(Calendar.MONTH);
    		int year = cal.get(Calendar.YEAR);
    		int weekday = cal.get(Calendar.DAY_OF_WEEK);
    		//1 is Sunday, 2 is Monday, ..., 7 is Saturday
    		
    		//The date and weekday that is 100 days from today
    		System.out.println("The date and weekday that is 100 days from today");
    		System.out.println("MONTH: " + month);
    		System.out.println("YEAR: " + year);
    		System.out.println("DAY OF MONTH: "+ dayOfMonth);
    		System.out.println("DAY OF WEEK: "+ weekday);
    		
    		
    		
    		//My birthday's weekday
    		int dayOfMonth2 = mybday.get(Calendar.DAY_OF_MONTH);
    		int month2 = mybday.get(Calendar.MONTH);
    		int year2 = mybday.get(Calendar.YEAR);
    		int weekday2 = mybday.get(DAY_OF_WEEK);
    		System.out.println("MY BIRTHDAY'S WEEKDAY IS " + weekday2);
    		
    		//The date that is 10,000 days from my birthday
    		System.out.println("The date that is 10,000 days from my birthday");
    		mybday.add(Calendar.DAY_OF_MONTH, 10000);
    		
    		System.out.println("MONTH: " + month2);
    		System.out.println("YEAR: " + year2);
    		System.out.println("DAY OF MONTH: "+ dayOfMonth2);
    		System.out.println("DAY OF WEEK: "+ weekday2);
    		
    
    	}
    
    	
    
    }
    on console windows it gives me:
    The date and weekday that is 100 days from today
    MONTH: 1
    YEAR: 1
    DAY OF MONTH: 1
    DAY OF WEEK: 1
    MY BIRTHDAY'S WEEKDAY IS 1
    The date that is 10,000 days from my birthday
    MONTH: 1
    YEAR: 1
    DAY OF MONTH: 1
    DAY OF WEEK: 1

    which is of course not correct...any ideas why it is giving me wrong results? thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Gregorian Calendar

    Big suggestion: don't give the class you've created the same name as a core Java class, and then give it constants (that are all 0 by default) that shadow the true Calendar class's constants and do nothing but confuse your program. Rename your program and import the true Calendar class and use *its* useful constants.
    FOX427 likes this.

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default Re: Gregorian Calendar

    oh shoot! that works! thank you for your suggestions! :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Sorting Gregorian Calendar?
    By JohnDas in forum New To Java
    Replies: 10
    Last Post: 01-25-2011, 01:24 PM
  2. help with Gregorian Calendar
    By fezman1337 in forum JCreator
    Replies: 3
    Last Post: 10-26-2010, 04:01 AM
  3. Gregorian Calendar
    By bindhuuk4 in forum New To Java
    Replies: 1
    Last Post: 08-07-2009, 12:00 PM
  4. Help with gregorian calendar
    By osval in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 11:21 PM
  5. Gregorian calendar issue
    By orchid in forum New To Java
    Replies: 1
    Last Post: 05-16-2007, 06:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •