Question

In my activity I have 2 Datepicker with which I choose the starting date (for example 01/01/2014) and end date (for example 12/01/2014). then I have to insert 12 records at the same time with their dates, that is, in my date field there will be the following lines:

01/01/2014
01/02/2014
01/03/2014 
01/04/2014 
01/05/2014
01/06/2014
01/07/2014 
01/08/2014
01/09/2014
01/10/2014
01/11/2014 
01/12/2014 

until now have only been able to calculate the difference between the starting date and the ending date, but I do not know exactly how to put all those records together. Thanks for your help.

my code to calculate the difference between dates (JodaTime)

public void diff_date(View v){
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
    String date_in = sdf.format( dateAndTime.getTime() );
    String date_out = sdf.format( dateAndTime1.getTime() );
     int differenza_date = Days.daysBetween(new DateTime(date_in), new DateTime(date_out)).getDays(); 
Was it helpful?

Solution

As in SQL you can insert more than row you can making your string like that

INSERT INTO 'tablename' ('column1') VALUES
('val1'),
('val2'),
('val2'),
('val2');

and here implementation of insertion code :

public void diff_date(View v){
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
    String insertRows = "INSERT INTO 'tablename' ('column1') VALUES";
     String date_in = sdf.format( dateAndTime .getTime() );
     String date_out = sdf.format( dateAndTime1.getTime() );
     int differenza_date = Days.daysBetween(new DateTime(date_in), new DateTime(date_out)).getDays();
     for(int i=1 ; i < differenza_date ; i++)
     {
         // increase i day each time and save it in string 
         insertRows += "("+increasedDate+"),"
     }

     // and here insert into database 
     db.execSQL(insertRows)
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top