1

I have a DatePicker in my activity. I want to store the selected date when the user clicks a button like this.

Date selectedDate=new Date(dp.getYear(), dp.getMonth(), dp.getDayOfMonth());

the problem is when the selected date is like today's: Feb 15 2010

the date returned from the

selectedDate

is 3910-02-15, so the year is 3910 instead of 2011

what is wrong with this ?

Thanks

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
  • I know this is old, but the constructor I'm using here is deprecated, we can use the new Date(long date) instead: https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html#Date(long) – Mina Wissa Feb 11 '16 at 09:31

3 Answers3

2

Code is looking fine, but i think when you are trying to display date in the format Feb 15 2010, at that time problem is being raised.

I suggest check the format that you are using to display your selectedDate.

So you can try as given below:

 txtViewDate.setText("Today is " + android.text.format.DateFormat.getLongDateFormat(this).format(new Date())); 
// in your case selectedDate instead of new Date()
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Thanks for the info, I realized that the constructor of the Date class requires that the year number minus 1900. – Mina Wissa Feb 15 '11 at 13:55
1

Please take a look at the example

Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
0

I got it, the problem is in the conversion of the selected year value to the date object then displaying it as a string. The date object constructor require the year value minus 1900.

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158