How to convert the date and time into a text? Android Studio

0

for example: 03/28/2018 10:57:24 and when registering or updating it is appreciated: "A moment ago", "An hour ago" in the xml

    
asked by jeanD 28.03.2018 в 17:59
source

1 answer

2

Maybe this helps you, you must first obtain the current time, then format the time you have in text with SimpleDateFormat (), get the milliseconds of each one and subtract these.

Date actual= Calendar.getInstance().getTime();
    try {
        Date obtenida= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse("28/03/2018 10:57:24");

        //tiempo en horas
        long tiempoTranscurrido = (actual.getTime()-obtenida.getTime())/1000/60/60;
        String texto="hace un momento";

        if(tiempoTranscurrido == 1)
            texto="hace una hora";
        else{
            if(tiempoTranscurrido < 24){
                texto="hace "+ tiempoTranscurrido+" horas";
            }
            else{
                //se puede filtrar como se quiera...dias, semanas...
            }
        }
        //mostrar texto en su textView o lo q desee
        // textView.setText(texto);

    } catch (ParseException e) {
        e.printStackTrace();
    }
    
answered by 28.03.2018 / 23:12
source