How to get a time range comparing two dates in Android?

0

Good afternoon friends.

What I want to do is get a time range, comparing 2 dates for example:

//Ejemplo simple
String fechaInicial: "03/09/2016 12:00"
String fechaActual: "03/09/2017 12:00"

And what I want to get is for example, this is not accurate Dias: 365, mes(es): 12, Año(s): 1, Hora(s): 8760

It would be great to be able to get the days, for each month.

    
asked by Luis Rene Mas Mas 04.09.2017 в 00:49
source

2 answers

2
DateTimeUtils obj = new DateTimeUtils();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/M/yyyy 
hh:mm:ss");

try {
            Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
            Date date2 = simpleDateFormat.parse("13/10/2013 
            20:35:55");obj.printDifference(date1, date2);

} catch (ParseException e) {
            e.printStackTrace();
}

//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate) { 
//milliseconds
        long different = endDate.getTime() - startDate.getTime();

        System.out.println("startDate : " + startDate);
        System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        System.out.printf(
            "%d days, %d hours, %d minutes, %d seconds%n", 
            elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
    }

Out put:

startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds

The method is from this page where you performed the function: click

Remember to have the JodaTime library.

    
answered by 04.09.2017 в 09:15
1
String fechaInicial ="03/09/2016 12:00";
String fechaActual ="03/09/2017 12:00";

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
    Calendar calInicial = Calendar.getInstance();
    Calendar calActual = Calendar.getInstance();

    try{
        Date inicialDate = formatter.parse(fechaInicial);
        calInicial.setTime(inicialDate);
        Date actualDate = formatter.parse(fechaActual);
        calActual.setTime(actualDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    long inicial = calInicial.getTimeInMillis();
    long actual = calActual.getTimeInMillis();

    int y1 = calInicial.get(Calendar.YEAR);
    int y2 = calActual.get(Calendar.YEAR);

    long difEnMillis = actual - inicial;
    int difEnAños = y2 - y1;
    long difEnSemanas = difEnMillis / (7 * 24 * 60 * 60 * 1000);
    long difEnDias = difEnMillis / (24 * 60 * 60 * 1000);
    long difEnHoras = difEnMillis / (60 * 60 * 1000);
    long difEnMinutos = difEnMillis / (60 * 1000);
    long difEnMeses = difEnDias / (30);

    //Para obtener los días de un mes en específico: (en este caso Febrero del 2016 -año bisiesto)
    calInicial.set(Calendar.YEAR, 2016);   //Hay que indicar el año y el mes
    calInicial.set(Calendar.MONTH, 1);     //0 = enero, 1 = febrero ...
    //calInicial.set(2016,1,1);           //Tambien puede ser de esta otra forma(año, mes, día)
    int numDias = calInicial.getActualMaximum(Calendar.DATE);

    //Lo muestras en TextViews:
    tv1.setText(""+difEnMillis);
    tv2.setText(String.valueOf(difEnAños));
    tv3.setText(""+difEnMeses);
    tv4.setText(""+difEnSemanas);
    tv5.setText(""+difEnDias);
    tv6.setText(""+difEnHoras);
    tv7.setText(""+difEnMinutos);
    tv8.setText("Febrero 2016 tiene: "+String.valueOf(numDias)+"días");
    
answered by 05.09.2017 в 05:04