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 12 of 12

Thread: GregorianClaendar Class

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy GregorianClaendar Class

    I have having trouble with a program my teacher gave me and I was wondering if someone can point me in the right direction. I have to write a program that prints the following information: The date and weekday that is 100 days from today, the weekday of your birthday, and the date that is 10,000 days from your birthday. I am just working on the first one: the date and weekday that is 100 days from today.

    My teacher didn't show us how to do this in class so I looked in the book and been using Java Platform SE 7. But still can't figure out how to use these methods. here is what i have so far.

    package homework.pkg3part.pkg4;

    import java.util.Calendar;
    import java.util.GregorianCalendar;



    public class HomeWork3Part4
    {

    public static void main(String[] args)
    {

    GregorianCalendar cal = new GregorianCalendar();

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
    int weekDay = cal.get(Calendar.DAY_OF_WEEK);

    System.out.println("Year: " + year + ", Month: " + month + ", day of month: " + dayOfMonth + ", weekday: " + weekDay );

    // I tried doing this but doesn't work.... cal.add(weekDay, 100); but add is an abstract void so i don't understand how to print it if it doesn't return anything

    }
    }

    Now i know there is an add method but it doesn't return anything so i don't under why / how to use it. I can't assign it to a variable or print out. I also don't understand what the point of using the constructor like so, GregorianCalender cal = new GregorianCalendar(2012, Calendar.MONTH, 12); when I can't store cal in a variable and print or manipulate that date.

    Any help would be greatly appreciated. I'm not looking for someone to do this code just push me in the right direction, I really want to understand whats going on.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: GregorianClaendar Class

    add is an abstract void
    Maybe you were looking at Calendar? GregorianCalendar's add is not abstract:
    GregorianCalendar (Java Platform SE 7 )

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: GregorianClaendar Class

    Ya i looked at that also but still no luck. I don't understand if I used cal.add(dayOfMonth, 2); but i can't assign it to a variable and I can't print it out so what can i do with it. Sorry I am really new with OOP.

    Thank you for your reply.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: GregorianClaendar Class

    Sorry I am really new with OOP
    It can take a little bit of looking up in the API. add is abstract in the abstract class (not instantiable) Calendar. GregorianCalendar is a "concrete subclass" (API doc) of Calendar which *overrides* Calendar's add method. The add method changes the specified calendar field - it's a mutator (changes the state of the object) method. To get the state (that is, the date) of the Calendar, you need to look through the list of methods in Calendar and GregorianCalendar to find a method which returns something that looks like a date. The Java API doc for subclasses list only those methods that override ones in the superclass or are new methods not present in the superclass, so it's not unusual to have to look through two or more API doc pages to find what you're looking for.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: GregorianClaendar Class

    Thank you. I been looking up and down GregorianCalendar and the calendar API. But i don't see anything that will get the information from my constructor.

    I'm using my constructor to declare my information instead of using all them get methods. but I still don't see a method to let me retrieve this information.

    package homework.pkg3part.pkg4;


    import java.util.Calendar;
    import java.util.GregorianCalendar;



    public class HomeWork3Part4
    {

    public static void main(String[] args)
    {

    GregorianCalendar cal = new GregorianCalendar(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);

    // can't find a method to let me get this information from my constructor.
    }
    }

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: GregorianClaendar Class

    The utility 'date' object in Java is java.util.Date. One of the methods in java.util.Calendar returns a Date object. It isn't very helpfully named. java.util.Calendar also has integer constants defined in it for use with its get() method, one of which is called DAY_OF_WEEK. Isn't that what you're looking for?

    All that the constructor does in Java is to instantiate (create) an object and returns a reference to the new object. 'cal' in your code is a reference to the GregorianCalendar object you create. The constants you use in the constructor are not meant to be used like that. For example today is new GregorianCalendar(2012, 2, 12). Try instantiating cal like that and then print it with System.out.println(cal). For a quick joyride through the Java API, find out what class System.out is and why printing cal will actually print the String returned by Calendar's 'toString()' method.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: GregorianClaendar Class

    I think I been asking the wrong question. I knew how to get the information obviously since I did it in my first post. Sorry about that. but is there a way to convert that into a "date type" where I can add to that date (lets say 100 days) and it will update the month, day, and year or do i have to do that manually by using the if statement?

    messing around with the program I realize you can't just dayOfMonth + 100

    public static void main(String[] args)
    {

    GregorianCalendar cal = new GregorianCalendar();


    int month = cal.get(Calendar.MONTH); // gets the month
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); // gets the day of month
    int year = cal.get(Calendar.YEAR); // gets the year

    int weekDay = cal.get(Calendar.DAY_OF_WEEK); // returns a number that represents a day of the week. Starting with Sunday which is represented as 1.

    System.out.println("Today is: " + weekDay); // should show 1 for sunday.
    System.out.println("The date is: " + (month + 1) + " / " + dayOfMonth + " / " + year + "."); // month + 1 because Jan. starts at 0

    System.out.println("Add 100 days: " + (dayOfMonth + 100));


    }

    So sorry it took me this long to realize what I was actually trying to ask.

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: GregorianClaendar Class

    Quote Originally Posted by thatguy View Post
    I realize you can't just dayOfMonth + 100
    That's what the add method does. DAY_OF_MONTH is the 'field', 100 is the amount.

    I feel we're tantalisingly close to the answer

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: GregorianClaendar Class

    O.K so i tried three different things

    cal.add(Calendar.DAY_OF_MONTH, 100); // nothing
    cal.add(dayOfMonth, 100); // nothing

    System.out.println("Add 100 days: " + cal.add(dayOfMonth,100)); // error

    Nothing worked but I did remember my teacher talking about after you throw something you have to caught it so I'm guessing there is a method I can use. But that kind of doesn't make sense because add is public void add(int field, int amount) and if i remember correctly in c++ void doesn't return anything it just does it.

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: GregorianClaendar Class

    I think i figured it out

    cal.set(year, month, dayOfMonth);
    System.out.println("Date: " + cal.getTime().toString());
    cal.add(Calendar.DAY_OF_MONTH, 100);
    System.out.println("Date: " + cal.getTime().toString());

    Thank you so much for all your help.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: GregorianClaendar Class

    Now I'm confused about something else.

    This is the right answer.
    GregorianCalendar birthday = new GregorianCalendar(2012, 5, 7);
    int birthdayWeekday = birthday.get(Calendar.DAY_OF_WEEK);

    or

    GregorianCalendar birthday = new GregorianCalendar(2012, Calendar.JUNE, 7);
    int birthdayWeekday = birthday.get(Calendar.DAY_OF_WEEK);

    Answer: Thursday

    -----------------------------------------------------------------------------

    GregorianCalendar birthday = new GregorianCalendar(2012, Calendar.MONTH, 7);
    int birthdayWeekday = birthday.get(Calendar.DAY_OF_WEEK);


    as I was typing this in i see why its wrong but it is still wrong output, it should be tuseday?
    Calendar.MONTH will give me feb. thats why its not giving me the right answer for 6/7/1991 but if its feb it should give me Tuesday not wednesday.


    GregorianCalendar birthday = new GregorianCalendar(Calendar.YEAR, Calendar.JUNE, 7);
    int birthdayWeekday = birthday.get(Calendar.DAY_OF_WEEK);

    I use Calendar.YEAR that should give me 2012 but the answer is Tuesday and not Thursday which is the right answer.

    So if you could clean this up for me I would greatly appreciated.

    Also thank you again for all your time.

  12. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: GregorianClaendar Class

    Solve, complete code.
    package homework.pkg3part.pkg4;
     
     
    import java.util.Calendar;
    import java.util.GregorianCalendar;
     
    /*
     * 1) The date and weekday that is 100 days from today.
     * 2) The weekday of your birthday
     * 3) Thae date that is 10,000 days from your birthday. 
     */
     
     
    public class HomeWork3Part4 
    {
     
        public static void main(String[] args) 
        {
     
     
            GregorianCalendar cal = new GregorianCalendar(); // this object is used for 1. 
            GregorianCalendar birthday = new GregorianCalendar(2012, Calendar.JUNE, 7); // this object is used for 2.
            GregorianCalendar future = new GregorianCalendar(); // This object is used for 3.
     
     
     
            // Start question #1
            int month = cal.get(Calendar.MONTH); // gets the month 
            int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); // gets the day of month 
            int year = cal.get(Calendar.YEAR); // gets the year 
     
     
     
            cal.set(year, month, dayOfMonth); // sets the calender date 
            System.out.println("Today's date: " + cal.getTime().toString());
            cal.add(Calendar.DAY_OF_MONTH, 100); // adds 100 days to the current day 
            System.out.println("100 from today: " + cal.getTime().toString());
     
     
     
            // start question #2
            int birthdayWeekday = birthday.get(Calendar.DAY_OF_WEEK); 
            String weekdayName = null;
     
            if (birthdayWeekday == 1)
            {
                weekdayName = "Sunday";
            } 
            else if (birthdayWeekday == 2)
            {
                weekdayName = "Monday";
            }
            else if (birthdayWeekday == 3)
            {
                weekdayName = "Tuesday";
            }
            else if (birthdayWeekday == 4)
            {
                weekdayName = "Wednesday";
            } 
            else if (birthdayWeekday == 5)
            {
                weekdayName = "Thursday";
            }
            else if (birthdayWeekday == 6)
            {
                weekdayName = "Firday";
            }
            else if (birthdayWeekday == 7)
            {
                weekdayName = "Saturday";
            }
     
            System.out.println("week day of birthday: " + weekdayName);
     
     
            // start question #3
            future.set(2012, 5, 7); // set birthday 
            System.out.println("Birthday: " + future.getTime().toString());
            future.add(Calendar.JUNE, 10000); // add 10,000 days to birthday. 
            System.out.println("10,000 days from my birthday: " + future.getTime().toString());
     
     
        }
    }

Similar Threads

  1. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  2. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM
  3. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  4. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  5. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM

Tags for this Thread