This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Calendar problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar problems" Watch "Calendar problems" New topic
Author

Calendar problems

Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
I'm wondering where I'm going wrong here:
To test a theory I wrote this little test class:


Which as expected outputted 23 6 1977 great!!
However, when I try to implement the same theory this way in a method....



What is printed is not what is passed into the method.
Can anyone explain why? All help would be most appreciated.

[This message has been edited by Angela Poynton (edited November 01, 2001).]


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
Hi Angela,
What theory are you testing?
The following code prints the same result
import java.util.*;
public class Test
{
public static void main( String args[] )
{
getDate( 1977, 6, 23 );
}
public static void getDate( int aYear , int aMonth , int aDay )
{
Calendar newDate = new GregorianCalendar( aYear , aMonth , aDay );
System.out.println( newDate.get( Calendar.DAY_OF_MONTH ) + " " + newDate.get( Calendar.MONTH ) + " " + newDate.get( Calendar.YEAR ) );
}
}
Hope that you are trying to do the same (code). Give more info about your need...
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
OK I'm writing a Calendar utility. Maybe I'm going mad but I'm not getting the expected results.

Put both of these classes in the same package and try running both.



Why is the Test implementation not producing the same results?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

(drumroll...) Because the code doesn't print the parts in the same order...
Take a look at it again. ("doh!"). Don't feel too bad, it took me almost 5 minutes to figure it out. With the help of VAJ's code formatter, it became a little bit clearer.
<pre>
public CalendarUtil(int aYear, int aMonth, int aDate) {
calendar = new GregorianCalendar(aYear, aMonth, aDate);
//If this constructor is called from a different class the following line
//does not print the correct numbers.
System.out.println(
"CalendarUtil() "
+ calendar.get(Calendar.MONTH)
+ " "
+ calendar.get(Calendar.DAY_OF_MONTH)

+ " "
+ calendar.get(Calendar.YEAR));
}

// vs.
public static void main(String[] args) {
Calendar newdate = new GregorianCalendar(1977, 6, 23);
System.out.println(
"main().newdate "
+ newdate.get(Calendar.DAY_OF_MONTH)
+ " "
+ newdate.get(Calendar.MONTH)
+ " "
+ newdate.get(Calendar.YEAR));

int year = 1977;
int month = 6;
int day = 23;
CalendarUtil test = new CalendarUtil(year, month, day);
//This prints the corret numbers.
System.out.println(
"main().test "
+ test.calendar.get(Calendar.DAY_OF_MONTH)
+ " "
+ test.calendar.get(Calendar.MONTH)
+ " "
+ test.calendar.get(Calendar.YEAR));

}
</pre>
------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform


Junilu - [How to Ask Questions] [How to Answer Questions]
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
Doh! Actually my alternative version of the Test class which calls CalenderUtil in the same way was printing out numbers that had NEVER been entered, they were entirely different.
I'm going to try adjusting things slightly.
Thanks for the observation though. I hadn't realised I'd done that!
 
Consider Paul's rocket mass heater.
 
subject: Calendar problems
 
Similar Threads
add a day to the date
getting the start date for a week number
add one day to current date
Tommorows date
date validation