How do I manipulate a date in an Edittext to add up to 1 day?

Asked

Viewed 51 times

0

I receive in an Edittext a date and I need that when pressing the renewal button this date is increased 1 day.

package br.edu.unp.bibliotecavirtual.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.Toast;

import java.util.ArrayList;

import br.edu.unp.bibliotecavirtual.R;
import br.edu.unp.bibliotecavirtual.config.CustomAdapterLivro;
import br.edu.unp.bibliotecavirtual.config.DatabaseHelper;
import br.edu.unp.bibliotecavirtual.model.RenovacaoModelo;

public class ListarEmprestimosActivity extends AppCompatActivity {

private EditText empTitulo, empDataemp, empDatadevol; //, 
etRegistroLivro;
private Button btnRenovado, btnDevolvido;
private RenovacaoModelo renovacaoModelo;
//private View view;
// private ListView listView;
private ArrayList<RenovacaoModelo> renovacaoModeloArrayList;
private CustomAdapterLivro customAdapterlivro;
private DatabaseHelper databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_renovates_give_back);
    Intent intent = getIntent();

    renovacaoModelo = (RenovacaoModelo) 
  intent.getSerializableExtra("user");
    databaseHelper = new DatabaseHelper(this);

//RESGATA VALORES PARA OS TEXTVIEW VINDOS DOS GETTER E SETTERS e dos ArrayLists

//displays in textview

    empTitulo = findViewById(R.id.empTitulo);
    empDataemp = findViewById(R.id.empDataemp);
    empDatadevol = findViewById(R.id.empDatadevol);

    empTitulo.setText(renovacaoModelo.getTitulo());
    empDataemp.setText(renovacaoModelo.getDataemp());
    empDatadevol.setText(renovacaoModelo.getDatadevol());


  // BOTOES DE AÇÃO DO FORMULÁRIO
    btnRenovado = findViewById(R.id.btnRenovado);
    btnDevolvido = findViewById(R.id.btnDevolvido);

  // função dos botoes renovar entregar
    btnRenovado.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //, etRegistro.getText().toString()
            databaseHelper.renovaLivro(renovacaoModelo.getId(),
                    empTitulo.getText().toString(), 
 empDataemp.getText().toString(), empDatadevol.getText().toString());// , 
 etRegistroLivro.getText().toString());
            Toast.makeText(ListarEmprestimosActivity.this, "Livro 
 Renovado com Sucesso!", Toast.LENGTH_SHORT).show();
 // MainActivity = tela principal
            Intent intent = new Intent(ListarEmprestimosActivity.this, 
 MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | 
 Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });

    btnDevolvido.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
 //renovacaoModelo
            databaseHelper.devolverLivro(renovacaoModelo.getId());
            Toast.makeText(ListarEmprestimosActivity.this, "Livro 
 Devolvido com Sucesso!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(ListarEmprestimosActivity.this, 
 MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | 
 Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });


}
}

the class with the function that will update the database:

 public int renovaLivro(int id, String titulo, String dataempre, String 
 datadevol) {
        SQLiteDatabase db = this.getWritableDatabase();
        //Criando valores do content
        ContentValues values = new ContentValues();
        values.put(KEY_EMP_ID, id);
        values.put(KEY_TITULOEMPRE, titulo);
        values.put(KEY_DATAATUAL, dataempre);
        values.put(KEY_DATADEVOLUCAO, datadevol); // adiciona mais 10 
dias na data final
//--->   //
       return  db.update(TABLE_EMPRESTIMO, values, KEY_EMP_ID + " = ?",
                  new String[]{String.valueOf(id)});
    }
// fim função emprestimo

Renovação e Devolução

1 answer

1


Hello,

Try as follows, get the value of your EditText that will come in the form of a String. Then we turn that String in a type object Date

String dateValue = seuEditText.getText().
   .toString()
   .trim();

SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
Date date = ft.parse(dateValue);

Then increment the date by one day using Calendar

Calendar c = Calendar.getInstance(); 
c.setTime(date); 
c.add(Calendar.DATE, 1);

And soon after, retrieve the date and save on your object

date = c.getTime();

Putting it all together

 String dateValue = seuEditText.getText().
   .toString()
   .trim();

SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
Date date = ft.parse(dateValue);
 Calendar c = Calendar.getInstance(); 
          c.setTime(date); 
          c.add(Calendar.DATE, 1);
date = c.getTime();

EDIT: To display the new date, do the following

String strDate = ft.formart(date);
Toast.makeText(this, "A nova data de entrega é em " + strDate, Toast.LENGTH_SHORT)
  .show();

It is important that the value of your EditText is in format dd/MM/yyyy. I hope it works!

  • This part where I get my date released in Edittext was not very clear to me, could explain?

  • Pronto @user3873165, I added an edit to my reply to make it clear how to do this

  • right, now I understand better, I’ll try to adapt, but I see that it would take a new Edittext to manipulate the data transparently and deliver the final date only in the bank and also in a messenger style Oast, because if the user does nothing there will be no transformations in these elements.

  • If I understand correctly, you don’t need another EditText. Just use what you already have

  • but I will not load this new date into it, only after the new database query, however I want to show in a Toast style message. 'Toast.makeText(Listremprestimosactivity.this, "Successfully Renewed Book! The new renewed date will be: "date.gettext().toString(), Toast.LENGTH_SHORT). show();'

  • I edited again so it’s possible to show the date as String

  • By performing some tests I was able to print the expected value in an editText, but after successive tests it gave an error: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null Object Reference This is due to Date = null; when it circles the parse with Try catch?

  • You should probably enter because this behavior is happening

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.