CoT
CoT

Reputation: 157

save current date into database

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.

Upvotes: 2

Views: 855

Answers (2)

SimonSays
SimonSays

Reputation: 10977

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

Upvotes: 1

Cat
Cat

Reputation: 67502

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;

Upvotes: 3

Related Questions