Results 1 to 12 of 12
  1. #1
    bars225 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Help creating a calendar in Java

    Hey guys, first post here so greatly appreciate any help given!

    Im learning java with this book and at the end of the chapter its given me a project to create a gregorian calendar and output some values.

    The first task is to add 100 days to the current date and then output the date and weekday.


    Java Code:
    import java.util.GregorianCalendar;
    import java.util.Calendar;
    
    public class Calendarproject {
    	
    	public static void main(String args[]){
    		
    		//todays date
    		GregorianCalendar cal = new GregorianCalendar();
    		
    		//My Bday
    		GregorianCalendar bday = new GregorianCalendar(1988, Calendar.NOVEMBER, 19);
    		
    		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);
    		
    		cal.add(Calendar.DAY_OF_MONTH, 100);
    		cal.add(Calendar.DAY_OF_WEEK, 100);
    
    		
    	}
    
    }
    This is as far as I've got, Im not sure which get methods to use to display the month and day of the week!! its really stressing me out.

    I know its probably something simple!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    to display the month and day of the week
    How do you want to display them?
    Have you looked at the API doc for the GregorianCalendar class? It has lots of examples.
    Which ones have you tried?

  3. #3
    bars225 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    basically, I need to add 100 days to cal and the print the result into the console window.


    I have looked at both the calendar and gregorian calendar API. but still cant figure it out. I'll write out the whole explanation it gives in the book

    Project 2.1. The GregorianCalendar class describes a point in time,
    as measured by the Gregorian calendar, the standard calendar that is
    commonly used throughout the world today. You construct a
    GregorianCalendar object from a year, month, and day of the
    month, like this:
    Java Code:
    GregorianCalendar cal = new GregorianCalendar();
    // Today's date
    GregorianCalendar eckertsBirthday = new
    GregorianCalendar(1919,
    Calendar.APRIL, 9);
    Use constants Calendar.JANUARY . . . Calendar.DECEMBER
    to specify the month.

    The add method can be used to add a number of days to a
    GregorianCalendar object:

    Java Code:
    cal.add(Calendar.DAY_OF_MONTH, 10); // Now cal is ten
    days from today
    This is a mutator method—it changes the cal object.
    The get method can be used to query a given GregorianCalendar
    object:

    Java Code:
    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
    Your task is to write a program that prints the following information:
    • The date and weekday that is 100 days from today
    • The weekday of your birthday
    • The date that is 10,000 days from your birthday

    I have tried System.out.print(dayOfMonth); but that gives todays date. and doesnt change after using the add methods in my first post.

    I have tried a whole bunch of things but have not been able to find the correct method!!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Where are you printing out the results so you can see what the code is doing?
    Add some printlns to show the values.

  5. #5
    bars225 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Well, I know the code doesnt have the println's to show but thats because i dont really know what to write for the println's

    I purposly left it blank as i have no idea what to put inside the println() method....

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    An example:
    Java Code:
          System.out.println("Today: dayOfMonth=" + dayOfMonth + ", weekday=" + weekday+", year=" + year);

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bars225 View Post
    Well, I know the code doesnt have the println's to show but thats because i dont really know what to write for the println's

    I purposly left it blank as i have no idea what to put inside the println() method....
    The GregorianCalendar class extends the Calendar class so it gets (inherits) some methods of the Calendar class. Read the description of the setLenient( ... ) method in that class, it might interest you ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    bars225 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    An example:
    Java Code:
          System.out.println("Today: dayOfMonth=" + dayOfMonth + ", weekday=" + weekday+", year=" + year);
    I've tried that along time ago but what happens is that it doesnt give me the updated values of dayOfMonth and weekday after applying the
    Java Code:
     cal.add(Calendar.DAY_OF_MONTH, 200);
     cal.add(Calendar.DAY_OF_WEEK, 200);
    methods this is the part im stuck on, otherwise i can output todays date only, i cannot change it.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Please show your code. You need to get new values for those variables printed everytime you change the contents of the calendar object. The code you posted gets the values before they calendar object is changed. Get them again after you have changed the calendar object.

  10. #10
    bars225 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The GregorianCalendar class extends the Calendar class so it gets (inherits) some methods of the Calendar class. Read the description of the setLenient( ... ) method in that class, it might interest you ...

    kind regards,

    Jos
    thanks for pointing that out but im still only on the 2nd chapter of the book out of about 15 chapters, the stuff im learning is really basic and i dont want to start using more complex methods to solve my problem when im guessing there is a rather straight forward way to do it, if you know what I mean? the books told me which methods i need to use so im trying to do it with only what has been said in the book.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bars225 View Post
    thanks for pointing that out but im still only on the 2nd chapter of the book out of about 15 chapters, the stuff im learning is really basic and i dont want to start using more complex methods to solve my problem when im guessing there is a rather straight forward way to do it, if you know what I mean? the books told me which methods i need to use so im trying to do it with only what has been said in the book.
    Have you read it? The description also mentioned that a Calendar object is set to the lenient mode by deafault which implies that you can set, say, the day of a date to 101 or similar and the calendar automatically figures out the correct day; you don't even have to use that method. That's why I suggested you to read that description.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    bars225 is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Lol i think i know what the problem was...... I added the cal.add methods BEFORE the int variables... that seems to have fixed it and I think I understand why.
    As cal has no parameters it uses calendar info from my OS. When I was adding 100 to DAY_OF_MONTH and DAY_OF_WEEK, the methods were still pulling the data from the un-updated parameters. I dont know if that makes sense to you guys but I know where the problem is now!

Similar Threads

  1. im a newbie creating a calendar planner
    By abbotjava in forum New To Java
    Replies: 3
    Last Post: 08-23-2009, 03:28 PM
  2. Create Calendar with Java
    By neoseeker191 in forum New To Java
    Replies: 4
    Last Post: 10-17-2008, 05:41 AM
  3. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  4. Creating Calendar in Applet
    By wco5002 in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:21 AM
  5. java Date and Calendar
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 03-30-2008, 06:49 PM

Posting Permissions

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