[Solved] How to Sort Firebase data by date stored as Value

[ad_1]

you should store the data in miliseconds, is not that hard, and then you can bring the data and parse it in Date , look at this snippet

   try{

         HttpClient httpclient = new DefaultHttpClient();
                        HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
                        StatusLine statusLine = response.getStatusLine();
                        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                            DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
                            dateStr = response.getFirstHeader("Date").getValue();
                            Date startDate = df.parse(dateStr);
                            dateStr = String.valueOf(startDate.getTime() / 1000);

   } else {
                    //Closes the connection.
                    response.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }
            } catch (ClientProtocolException e) {
                Log.d("Response", e.getMessage());
            } catch (IOException e) {
                Log.d("Response", e.getMessage());
            } catch (ParseException e) {
                e.printStackTrace();
            }

Here you can get a server time in miliseconds in google

or if you want to get the time from the phone

Long tsLong = System.currentTimeMillis() / 1000;

and you can convert those miliseconds into date

 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            String myDate = formatter.format(new Date(Long.parseLong(HERE_YOUR_MILIS_DATE) * 1000L));

hope it helps

1

[ad_2]

solved How to Sort Firebase data by date stored as Value