-
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
-
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.
-
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.
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks