2

I want to save system date on my database, but it returns : 22/0/2013. here my code :

Calendar cal = Calendar.getInstance(); 

               int year = cal.get(Calendar.YEAR);
               int month_ = cal.get(Calendar.MONTH);
               int day_ = cal.get(Calendar.DATE);

               String  FullDate = (""+day_+"/"+month_+"/"+year);

                String text_Rate=(String.valueOf(FullDate));                                
                Log.d("System Date show", text_Rate);

where i'm doing wrong.

CoT
  • 157
  • 2
  • 13
  • There is a good discussion of working with dates in database here:http://stackoverflow.com/questions/11653818/how-to-get-day-month-and-year-from-datepickerdialog/11674998#11674998 – DrA Jan 22 '13 at 21:17

2 Answers2

3

This is the correct behavior. The Java Calendar month is 0-based (January is 0, December is 11).

If you really want to store it as 22/1/2013, simply add +1 to your month:

int month_ = cal.get(Calendar.MONTH) + 1;
Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
1

you should save dates as long to sqlite. that is easier and less prone to errors. see here for calendar to long.

SimonSays
  • 10,867
  • 7
  • 44
  • 59