Reputation: 2505
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?
Upvotes: 1
Views: 2426
Reputation: 339837
In modern Java, use java.time classes. Never use the terribly-flawed legacy called such as Date
, Calendar
, and SimpleDateFormat
.
java.time.LocalDate
For a date-only value, without time-of-day, and without time zone or offset, use Java class LocalDate
. In SQL, define your column as a data type akin to the SQL standard type DATE
.
Retrieval:
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
Writing:
myPreparedStatement.setObject( … , ld ) ;
I do not know SWT. But according to the Javadoc for the SWT widget DateTime
, we can pass the year, month, and day as you showed in the Question.
We need to extract the year, month, and day from our LocalDate
object. The extraction is easy, by calling getter methods.
mySwtDateTime.setDate( ld.getYear() , ld.getMonthValue() , ld.getDayOfMonth() ) ;
Upvotes: 1
Reputation: 2505
I tried using this method and it worked correctly so this method can be useful to other.
Calendar startCal = Calendar.getInstance();
startCal.setTime(joindate);
int months= startCal.get(Calendar.MONTH);
int years= startCal.get(Calendar.YEAR);
int days= startCal.get(Calendar.DAY_OF_MONTH);
System.out.println(months+1);
System.out.println(years);
System.out.println(days);
dateTimeJoin4.setDate(years, months+1, days);
Upvotes: 2
Reputation: 1503080
Chances are you haven't read the documentation for Date.getYear
and Date.getMonth
:
Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
and
Returns a number representing the month that contains or begins with the instant in time represented by this Date object. The value returned is between 0 and 11, with the value 0 representing January.
respectively. Likewise Date.getDay
returns the day of the week, not the day of the month - you want Date.getDate()
for that (or preferrably a different API entirely, either Calendar
or Joda Time).
This has nothing to do with SWT's DateTime
type - after all, you only use that in the last line. When something behaves unusually, your first port of call should be the documentation. SWT's DateTime.setDate
method 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 Calendar
also 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.
Upvotes: 7
Reputation: 30855
getDay() return the position of the day not the date like suppose today is Monday it will return 1, it start from the sunday with 0 value.
as well as the getMonth() return the position as starting value is 0 for january at now you getting the 7 instead of 8.
for getting date you can use the SimpleDateFormat or DateFormat by passing the format string in that you can get it
example links
http://www.roseindia.net/java/javadate/date-format.shtml
http://www.java-examples.com/java-simpledateformat-class-example
Upvotes: 0