Sri Harsha Damarla
Sri Harsha Damarla

Reputation: 67

How to Retrieve Only Year in servlets while having resultset from the database

I have to retrieve the Year(datepaid) from the database. What method should I use to get Year(datepaid)

I have tried using the method getDate("column name"), it is showing error because it is looking for date format from the database but here I'm retrieving only Year 2020 which is not in the date format.

resultSet.getDate("column name") is not working, what method should I use now?

Upvotes: 0

Views: 301

Answers (2)

haoyu wang
haoyu wang

Reputation: 1377

There is no special method to retrive YEAR type column. You can just use resultSet.getString or resultSet.getInt to get the value.

Upvotes: 1

farftech19
farftech19

Reputation: 179

Make sure that in the Database your column is of TYPE DATE/TIMESTAMP. And getDate should retrieve the complete date. So in java you can use the next code to properly access the year:

Calendar calendar = new GregorianCalendar();
calendar.setTime(date); //date = complete date from the DB
int year = calendar.get(Calendar.YEAR);

If in your DB the column of the date just have the year (Ex: 2020, 2019), could be an integer or a string. So resultSet.getString or resultSet.getInt would work.

Upvotes: 0

Related Questions