943,740 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 600
  • Java RSS
Dec 13th, 2008
0

will not remember calendar infor when inserted when creating class

Expand Post »
I have a basic problem i dont know where the error lies.
i can create a class Persoon and fill in all its values before its created .
but for some reason when i call up the information i get the current Date as the birthdate and not what i have entered.

when i enter the birtdate directly into the class via its method after it has been created it will rmember it.

the all the geboorte things are related to the birthDate tho you can probably see by the use of calendar

can anyone see why or tell me while?
maybe i dont understand the calendar api .
but we were forced to use calendar or date
Help with Code Tags

Java Syntax (Toggle Plain Text)
  1. /**
  2.  * de persoon maken, hier word alles gemaakt wat
  3.  * docent en student gemeen hebben.
  4.  */
  5. import java.util.*;
  6.  
  7.  
  8. public class Persoon
  9. {
  10. /**
  11.   *variabelen instantieren
  12.   */
  13. private String naam;//voor methode setNaam
  14. private String geslacht;//voor methode setGeslacht
  15. private int leeftijd;
  16. private int geboorteDag;
  17. private int geboorteMaand;
  18. private int geboorteJaar;
  19. private Calendar geboorteInformatie = new GregorianCalendar();
  20.  
  21. /**
  22.   * Constructor voor objecten van de class persoon
  23.   */
  24. //eigenschappen van persoon construeren
  25. public Persoon(String naam, String geslacht, int geboorteDag, int geboorteMaand, int geboorteJaar )
  26. {
  27. this.naam = naam;
  28. this.geslacht = geslacht;
  29. this.geboorteDag = geboorteDag;
  30. this.geboorteMaand = geboorteMaand;
  31. this.geboorteJaar = geboorteJaar;
  32. }
  33.  
  34. /**
  35.   * methoden voor persoon
  36.   */
  37.  
  38. //methode maken voor namen
  39. public String setNaam(String voornaam, String achternaam)
  40. {
  41. naam = voornaam + " " + achternaam;
  42. return naam;
  43. }
  44.  
  45. //methode maken voor het geslacht
  46. public String setGeslacht(String manOfVrouw)
  47. {
  48. geslacht = manOfVrouw;
  49. return geslacht;
  50. }
  51.  
  52. //methode maken voor geboortedatum
  53. public void setGeboorteDatum(int geboorteDag, int geboorteMaand, int geboorteJaar)
  54. {
  55. geboorteInformatie.set(geboorteJaar,geboorteMaand,geboorteDag);
  56. }
  57.  
  58. private int getGeboorteDag()
  59. {
  60. geboorteDag = geboorteInformatie.get(Calendar.DAY_OF_MONTH);
  61. return geboorteDag;
  62. }
  63.  
  64. private int getGeboorteMaand()
  65. {
  66. geboorteMaand = geboorteInformatie.get(Calendar.MONTH);
  67. return geboorteMaand;
  68. }
  69.  
  70. private int getGeboorteJaar()
  71. {
  72. geboorteJaar = geboorteInformatie.get(Calendar.YEAR);
  73. return geboorteJaar;
  74. }
  75.  
  76. public void print()
  77. {
  78. System.out.println("Jaar: " + geboorteInformatie.get(Calendar.YEAR) +" Dag: "+ geboorteInformatie.get(Calendar.DAY_OF_MONTH)+" Maand: "+ geboorteInformatie.get(Calendar.MONTH));
  79. System.out.println("geslacht: " + geslacht);
  80. System.out.println("Naam: " + naam);
  81. }
  82.  
  83. //checken welke persoon ouder is
  84. public boolean ouderDan(Persoon deAnder)
  85. {
  86. return geboorteInformatie.before(deAnder.geboorteInformatie);
  87. //als de geboorte van de eerste persoon eerder was dan de ander dan zal er true uitkomen
  88.  
  89. }
  90. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sereal_Killer is offline Offline
17 posts
since Nov 2008
Dec 13th, 2008
0

Re: will not remember calendar infor when inserted when creating class

redundant info, delete
    private int geboorteDag;
    private int geboorteMaand;
    private int geboorteJaar;
use geboorteInformatie.get(XXX);

All the data are stored inside Calendar geboorteInformatie;
use constructor :
GregorianCalendar(int year, int month, int dayOfMonth)
Attention, from javadoc: Month value is 0-based. e.g., 0 for January
Make proper corrections to month value.
Last edited by quuba; Dec 13th, 2008 at 8:16 pm. Reason: correction
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 14th, 2008
0

Re: will not remember calendar infor when inserted when creating class

so i dont need all the methods seperatly ?

im trying to understand but im not so good at java it appears .
anyway ill contininue to mess around with what you said and try and figure it out
thanks for the reply
Last edited by Sereal_Killer; Dec 14th, 2008 at 9:07 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sereal_Killer is offline Offline
17 posts
since Nov 2008
Dec 14th, 2008
0

Re: will not remember calendar infor when inserted when creating class

I think the way you're storing things is a little bit odd, and that's tying you in knots. You've declared variables that duplicate one another: on the one hand, you've got the separate 'day', 'month', 'year' variables. And on the other hand, you've got the Calendar.

Start by deciding on ONE way in which you're going to store the date of birth. If you're going to use a Calendar, then don't also have the separate ints, and vice versa. (I'd actually recommend a Date -- Calendar is really designed for calculating things with dates, but not storing an actual date.)

Then. once you've decided on your single way of storing a date, make sure that you always set this field consistently in all the places where you set a date. At the moment, your constructor does one thing, but your setGeboorteDatum() does another thing. (Once you've decided what setGeboorteDatum() will do, why not make your constructor call setGeboorteDatum() -- that way there's no risk of the two pieces of code accidentally evolving into different things later in time.)
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Editing Lines in Java
Next Thread in Java Forum Timeline: Launching Executables within java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC