| | |
will not remember calendar infor when inserted when creating class
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
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
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)
/** * de persoon maken, hier word alles gemaakt wat * docent en student gemeen hebben. */ import java.util.*; public class Persoon { /** *variabelen instantieren */ private String naam;//voor methode setNaam private String geslacht;//voor methode setGeslacht private int leeftijd; private int geboorteDag; private int geboorteMaand; private int geboorteJaar; private Calendar geboorteInformatie = new GregorianCalendar(); /** * Constructor voor objecten van de class persoon */ //eigenschappen van persoon construeren public Persoon(String naam, String geslacht, int geboorteDag, int geboorteMaand, int geboorteJaar ) { this.naam = naam; this.geslacht = geslacht; this.geboorteDag = geboorteDag; this.geboorteMaand = geboorteMaand; this.geboorteJaar = geboorteJaar; } /** * methoden voor persoon */ //methode maken voor namen public String setNaam(String voornaam, String achternaam) { naam = voornaam + " " + achternaam; return naam; } //methode maken voor het geslacht public String setGeslacht(String manOfVrouw) { geslacht = manOfVrouw; return geslacht; } //methode maken voor geboortedatum public void setGeboorteDatum(int geboorteDag, int geboorteMaand, int geboorteJaar) { geboorteInformatie.set(geboorteJaar,geboorteMaand,geboorteDag); } private int getGeboorteDag() { geboorteDag = geboorteInformatie.get(Calendar.DAY_OF_MONTH); return geboorteDag; } private int getGeboorteMaand() { geboorteMaand = geboorteInformatie.get(Calendar.MONTH); return geboorteMaand; } private int getGeboorteJaar() { geboorteJaar = geboorteInformatie.get(Calendar.YEAR); return geboorteJaar; } public void print() { System.out.println("Jaar: " + geboorteInformatie.get(Calendar.YEAR) +" Dag: "+ geboorteInformatie.get(Calendar.DAY_OF_MONTH)+" Maand: "+ geboorteInformatie.get(Calendar.MONTH)); System.out.println("geslacht: " + geslacht); System.out.println("Naam: " + naam); } //checken welke persoon ouder is public boolean ouderDan(Persoon deAnder) { return geboorteInformatie.before(deAnder.geboorteInformatie); //als de geboorte van de eerste persoon eerder was dan de ander dan zal er true uitkomen } }
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
redundant info, delete
use geboorteInformatie.get(XXX);
All the data are stored inside Calendar geboorteInformatie;
use constructor :
Attention, from javadoc: Month value is 0-based. e.g., 0 for January
Make proper corrections to month value.
private int geboorteDag;
private int geboorteMaand;
private int geboorteJaar;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
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
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.)
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.)
![]() |
Other Threads in the Java Forum
- Previous Thread: Editing Lines in Java
- Next Thread: Launching Executables within java
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working





