I retrieve a Date from the database using the ResultSet rsExpid.
Date joindate = rsExpid.getDate("join_date_ad");
System.out.println(joindate);
int year = joindate.getYear();
System.out.println(year);
int month =joindate.getMonth();
System.out.println(month);
int day = joindate.getDay();
System.out.println(day);
dateTimeJoin4.setDate(year, month, day);
When I print joindate to the console it shows correctly as 2011-08-03, but when I print the year to the console I was amazed to see 111. Similarly printing the month produced 7 and the day resulted in 3.
The variable dateTimeJoin4 is my SWT DateTime. It is not setting any value and it is also not giving any error message. Could please anybody help me on this?
Chances are you haven’t read the documentation for
Date.getYearandDate.getMonth:and
respectively. Likewise
Date.getDayreturns the day of the week, not the day of the month – you wantDate.getDate()for that (or preferrably a different API entirely, eitherCalendaror Joda Time).This has nothing to do with SWT’s
DateTimetype – after all, you only use that in the last line. When something behaves unusually, your first port of call should be the documentation. SWT’sDateTime.setDatemethod is documented to require a year between 1752 and 9999, so 111 will be confusing it. Admittedly it would have been nice if it had thrown an exception, but even so…The fact that you’re calling deprecated methods should have been a hint to you, although
Calendaralso uses 0-11 for its months.Personally I would strongly encourage you to use Joda Time for as much of your date and time work as you can. It’s a far superior date and time API to the one built into Java. It’s not immediately clear whether it’s worth you using it here (if this is all you have to do) but if you’re doing anything at all significant (including parsing, formatting or any kind of arithmetic) you should be using it.