• Post Reply Bookmark Topic Watch Topic
  • New Topic

testing a date class  RSS feed

 
ally tan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help
[ August 31, 2008: Message edited by: ally tan ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please find the Code button below the message box; it makes your code much easier to read.
This looks like a beginner's problem to me.

You are passing a Calendar object to three methods to get information back from it (declare the type of the parameter as Calendar not GregorianCalendar); these three methods neither alter the state of your object nor take information from your object, and could beneficially be declared static. Also you must remember that JANUARY is 0 and DECEMBER is 11.

You don't need two Readers in your main() method. Actually the java.util.Scanner class is much easier to use than Readers. You can pass a String to your constructor and split it using the String#split method and pass the three parts to your constructor.
The SimpleDateFormat#parse method returns a Date object which you can pass to the constructor of a GregorianCalendar object.
You can simplify your isEarlier method greatly; it can be reduced to a single statement. Similarly isLater. Similarly isEqual.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like a school project - otherwise I can see the meaning of trying to code you own Date class.

you should changes these two lines to the right methods used to 'format' a String to a java.util.Date from the SimpleDateFormat class.

when this is done then use them to set the input String date to the constructor of your class Date that can have a GregorianCalendar as input - on a GregorianCalendar you can set a Date with 'setTime()'.

What more do you need??
[ August 30, 2008: Message edited by: Rene Larsen ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rene Larsen:
. . .a GregorianCalendar you can set a Date with 'setTime()'.
Yes, you are right and I was mistaken; you pass the result of SimpleDateFormat#parse to the setTime method of Calendar, not the constructor.

Sorry about that.
 
ally tan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not really a project but it was a lab question i'm trying to solve as there's no lab solution given. I dunno how to use Scanner thats why i used bufferReader instead.. could anyone please give me some advice as i'm having my exam soon and i really wan to understand my lab question... i dun need the whole solution but i just need to know how to pass the user input from bufferedReader to my methods cos i have a error IOExeption when i try to caste it to :



thanks a million if possible 1 method example will be good too ..
[ August 30, 2008: Message edited by: ally tan ]
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you have a working sample - note that the pattern for month in 'SimpleDateFormat' is in uppercase:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that your own class is called "Date", the same as the Date class in the Java package java.util. This is going to mean trouble if you want to use the SimpleDateFormat class.

SimpleDateFormat works only with the standard java.util.Date class, and not with your own class also named Date.

It's better to name your class something else than "Date" to prevent confusing errors.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
Note that your own class is called "Date", the same as the Date class in the Java package java.util. This is going to mean trouble if you want to use the SimpleDateFormat class.

SimpleDateFormat works only with the standard java.util.Date class, and not with your own class also named Date.

It's better to name your class something else than "Date" to prevent confusing errors.
I did that once and had to write java.util.Date every time I used the original Date class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!