• Post Reply Bookmark Topic Watch Topic
  • New Topic

USING Dates.... HELP!  RSS feed

 
neal graham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to assign an instance field in an object a date. NOT today's date, a date passed by parameter
i.e. GregorianCalendar(int years, int month, int date)
Once this is created, HOW do I retrieve the YEAR MONTH and DATE (day) and then come up with something like
January 5, 1999 OR 1-5-1999 or whatever.
I know there is some DateFormat stuff lurking out there that when passed a MONTH it returns the name of the month. Same with the DATE (day) (WHY they call it DATE when it is the DAY makes no sense to me)
When I create the object by calling
GregorianCalendar newDate = new GregorianCalendar(1999,10,23);
and then try and access the YEAR MONTH or DATE instance variable of the GregorianCalendar object it says these variables don't exist in the GregorianCalendar class.
PLEASE point me in the direction of an example or tell me to use a different class (Calendar?). I'm pulling out what little hair I have left..... TIA!
[This message has been edited by neal graham (edited October 29, 2001).]
 
neal graham
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by neal graham:
I'm pulling out what little hair I have left.....

OK, I figured this out, BUT I STILL WANT TO KNOW if there is a BETTER, SIMPLER, more STANDARD way of SAVING a user supplied date as part of an object and then being able to recall its value and use it. Here are my get and set methods.
//create and set value of date object, month is stored as month-1
public void setDate(int year, int month, int day)
{
GregorianCalendar acqDate = new GregorianCalendar(year, month-1, day);
this.acqDate = acqDate;
}

//date specific accessor methods
public int getYear() { return acqDate.get(acqDate.YEAR); } //returns year of date
public int getMonth() { return acqDate.get(acqDate.MONTH)+1; }//returns real month of date
public int getDay() { return acqDate.get(acqDate.DATE); } //returns day of date
public String getMonthDate()
{
String[] months = new DateFormatSymbols().getMonths(); //get month names
return months[acqDate.get(acqDate.MONTH)] + " " + acqDate.get(acqDate.DATE)
+ ", " + acqDate.get(acqDate.YEAR);
}
public String getDayDate()
{
String[] months = new DateFormatSymbols().getMonths(); //get month names
String[] weekdays = new DateFormatSymbols().getWeekdays(); //get day names
return weekdays[acqDate.get(acqDate.DATE)] + ", " +months[acqDate.get(acqDate.MONTH)]
+ " " + acqDate.get(acqDate.DATE) + ", " + acqDate.get(acqDate.YEAR);
}

[This message has been edited by neal graham (edited October 29, 2001).]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!