Comparison of JAVA dates

Asked

Viewed 122 times

1

Good people I have a array of strings with dates, coming from a query with Firebase database. I want to organize it increasingly.

Ex: 05/09/2016 - 06/09/2016 - 11/11/2016

Dates are entered in my array by method populateViewHolder:

protected void populateViewHolder(RoomViewHolder viewHolder, EscapeRoom model, int position)
        {
            String [] datasBancoDeDados = new String[] {model.getData()} ;
        }

At each execution of the method it adds a date to my array.

My goal is when organizing this array with dates, move to a Activity the values from the nodes corresponding to these dates.

For example the date 05/09/2016 would be before the date 06/09/2016 and so pass the values of the database to build a RecyclerView correctly.

I do not know if I explained very well, I will leave the prints of the app, what I have for now to improve the visualization.

prints do app

Thank you in advance.

1 answer

1


I don’t know if I got it right, but you seem to have an array of Strings containing dates and want to sort this array.

If I have understood your question correctly, how about doing so?

public static String[] ordenarDatas(String[] entrada) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    sdf.setLenient(false);
    Date[] dates = new Date[entrada.length];
    for (int i = 0; i < entrada.length; i++) {
        try {
            dates[i] = sdf.parse(entrada[i]);
        } catch (ParseException e) {
            throw new IllegalArgumentException(entrada[i], e);
        }
    }
    Arrays.sort(dates);
    String[] resultado = new String[entrada.length];
    for (int i = 0; i < entrada.length; i++) {
        resultado[i] = sdf.format(dates[i]);
    }
    return resultado;
}

The imports necessary are those:

import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;
import java.text.ParseException;
import java.text.SimpleDateFormat;

And then you could use it like this:

protected void populateViewHolder(RoomViewHolder viewHolder, EscapeRoom model, int position) {
    String[] datasBancoDeDados = ordenarDatas(new String[] {model.getData()});
}

See here an example working on ideone.

The given example creates a new array of String and returns it, without changing the original array. However, if what you wanted was to change the original array, and not create a new array, then just change it:

    Arrays.sort(dates);
    String[] resultado = new String[entrada.length];
    for (int i = 0; i < entrada.length; i++) {
        resultado[i] = sdf.format(dates[i]);
    }
    return resultado;

That’s why:

    Arrays.sort(dates);
    for (int i = 0; i < entrada.length; i++) {
        entrada[i] = sdf.format(dates[i]);
    }
    return entrada;

And it will override the given array and return the same received array in the parameter. See here this working on ideone.

  • Exactly that, so this code takes my array and makes it organized, right? So I get the positions corresponding to the correct dates?

  • @Lucassoares It will leave the dates of the array in ascending order. Whether this suits you or not, there you have to say.

  • Thank you I will do here, especially what I want to organize the dates within the thank array

Browser other questions tagged

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