Show Time SQLite Android

0

I have this code, as you can see the name of a worker is registered, but I do not know how to show that time in textView , because if I do this, tvHora.setText(date); does not show me anything and the app closes .

private String getDate() {             
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE dd/MM/yyyy hh:mm  a", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);

    }

    public void guardar(){

        String Nombre = text.getText().toString();

        SQLite base = new SQLite( this,"SQLite",null,1 );
        SQLiteDatabase bd = base.getWritableDatabase();
        ContentValues contenedor = new ContentValues();
        contenedor.put("NOMBRE", Nombre );
        contenedor.put( "FECHA",getDate() );

        try {
            bd.insertOrThrow("TRABAJADOR",null,contenedor);
            Toast.makeText( getApplicationContext(),"Ingresado Correctamente",Toast.LENGTH_SHORT ).show();


        }catch (SQLException e){
            Toast.makeText( getApplicationContext(),"No Ingresado",Toast.LENGTH_SHORT ).show();


        }
        bd.close();

    }
    
asked by German Silva 22.10.2018 в 04:17
source

1 answer

0

With this you see the entire String (EEEE dd / MM / yyyy hh: mm a):

tvHora.setText(getDate());

In this other way you would see, sometimes yes, in others not, only the time (hh: mm a):

tvHora.setText(getDate().substring(17, 25));

This is because they will vary the total of the characters (Monday is 5, Wednesday 9 ...). In this case, first put the time when saving it in SQlite (hh: mm to EEEE dd / MM / yyyy) and thus the substrings would be 0-11 (including the pm / am points).

Another way is to use a new SimpleDateFormat with the new format you want (hh: mm a) using Calendar within a try / catch:

SimpleDateFormat formato = new SimpleDateFormat("hh:mm a");  // el nuevo formato que quieres
Calendar cal = Calendar.getInstance();                       // instancias el Calendar
String fecha = getDate();  // o la columna de tu base de datos
try {
      cal.setTime(formato.parse(fecha));                    
      tvHora.setText(formato.format(cal.getTime()));      // el textView dentro del try

} catch (ParseException e) {
      e.printStackTrace();
    }
    
answered by 23.10.2018 / 01:31
source