Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar problems

 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic