Help with GregorianCalendar


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Help with GregorianCalendar

  1. #1
    Join Date
    Sep 2006
    Posts
    9

    Question Help with GregorianCalendar

    I been trying to learn Java and I been using a book call Java Concepts by Cay Horstmann and i'm having a bit of trouble. I'm doing the programming projects and it askes to construct a gregorianCalender object from a year,mnth, and day of the month. So far I got


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


    public class PP21
    {

    public static void main (String[] args)
    {

    GregorianCalendar cal = new GregorianCalendar();
    GregorianCalendar eckertsBirthday = new GregorianCalendar();

    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);



    System.out.println("The month is: " + month);
    System.out.println("The day is: " + weekday);
    System.out.println("The year is: " + year);

    }

    }

    Now the problem I am having is that I'm suppose to do the date and weekday that is 100 days from today. I believe it is something like cal.add or some sort, but I been trying for the last hour with no lucks. Any help would be great.

    Thanks in advance

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    If you look at the API for the GregorianCalendar class, you'll see that the call should be:

    cal.add( Calendar.DATE, 100 );

    You then can query the instance for its day of the week and the date.

    You can access the API at http://java.sun.com/j2se/1.5.0/docs/api/
    even better, you can download the API and have a shortcut on your desktop so it is always available at the (double) click of your mouse
    Last edited by nspils; 09-21-2006 at 01:59 AM.

  3. #3
    Join Date
    Sep 2006
    Posts
    9
    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);


    So I would have to replace the cal.get with cal.get( Calendar.DATE, 100 ); ?

    I'm still kinda confused. A little more help would be great.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    If you call cal.add( Calendar.DATE, 100 );
    the current date of cal will be 100 days after the date which is cal's "current" date. Now that you have that current date, you get day of the month, month, year, and day_of_week for that new date.

Similar Threads

  1. subtracting days from GregorianCalendar
    By ashiers in forum Java
    Replies: 1
    Last Post: 06-25-2005, 05:55 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center