PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Calendar/Date Probleme



nul
29-01-2007, 21:09
Es geht um folgende Methode:

private Date getBirthday() {
try {
int year = Integer.parseInt(this.getYear());
int month = Integer.parseInt(this.getMonth());
int date = Integer.parseInt(this.getDay());

/*String[] ssIDs = TimeZone.getAvailableIDs();
for( int i=0; i<ssIDs.length; i++ )
System.out.println( ssIDs[i] );*/

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Europe/Vienna"));
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, date);

return calendar.getTime();
} catch (NumberFormatException e) {
e.printStackTrace();
return null;
}
}
Eingaben fuer das Datum sind:
1983
12
22
Die Ausgabe aber:
Sun Jan 22 22:07:54 GMT 1984

Kann mir jemand sagen warum?

mfg
nul

eule
29-01-2007, 22:35
Ich weiß jetzt nicht wie dein Methode für getMonth() ist. Aber du muss beim einlesen eins abziehen, da Januar den intwert 0 hat. Folglich hat Dezember ( 12. Monat) die zahl 11.
Also rechnet er bei dir einen Monat drauf, was man ja an deinem Ergebnis sieht.

Die api sagt das dazu:
http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#MONTH

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

nul
31-01-2007, 01:05
Habs zwar mittlerweile selbst rausgefunden, aber danke noch mal!

mfg
nul