Re: GregorianClaendar Class
Quote:
add is an abstract void
Maybe you were looking at Calendar? GregorianCalendar's add is not abstract:
GregorianCalendar (Java Platform SE 7 )
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.
Re: GregorianClaendar Class
Quote:
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.
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.
}
}
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.
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.
Re: GregorianClaendar Class
Quote:
Originally Posted by
thatguy
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 :D
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.
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.
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.
Re: GregorianClaendar Class
Solve, complete code.
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());
}
}