Reputation: 171
I am creating an app and a main feature of the app is that a user will be able to plan trips in the future and the app will notify him when to start navigating there. I want to store and sort upcoming trips based on date and time (the closest one on the current date being first).
I found some answers in StackOverflow about the date issue but all of the answers were about getting the current time from the system using ServerValue.TIMESTAMP
(and storing it using timestamp in the Firebase Realtime Database).
This only works for storing the current date and time. I am interested to get the timestamp for future dates.
The trips are currently stored by the order they are added on my database as you can see on the image below:
I currently write data for the trip in the database as a Trip object, like this:
//Store date,time and destination on a new Trip.
datePassed = dateText.getText().toString();
timePassed = timeText.getText().toString();
trip = new Trip(destinationPassed, datePassed, timePassed);
//Get uId of the Firebase User.
String uId = currentFirebaseUser.getUid();
//Create a unique Hash Key for the Trip. (Used to Distinguish trips)
String tripId = Integer.toString(trip.getTripId(destinationPassed, datePassed, timePassed));
//Store the trip on Firebase RealTime Database.
DatabaseReference childReff = dbRef.child(uId).child("trips").child(tripId);
childReff.setValue(trip);
and I retrieve the data like this:
//Get uId of the user
final String uId = currentFirebaseUser.getUid();
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child(uId).child("trips").getChildren()) {
//Here I access each child separately (destination, date, time)
date = child.child("date").getValue().toString();
destination = child.child("destination").getValue().toString();
time = child.child("time").getValue().toString();
My question is how can I achieve to store and trips in ascending order (the closest one on the current date being first) for trips that are set in the future?
Upvotes: 1
Views: 1831
Reputation: 171
Ok so I figured out the answer. I will post what I did in case somebody has the same question in the future.
I created a method that transforms String dates into Long(Milliseconds). (This is what the firebase database uses for time and date.)
public Long toMilli(String dateIn) throws ParseException {
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = (Date) formatter.parse(dateIn);
long output = date.getTime() / 1000L;
String str = Long.toString(output);
long timestamp = Long.parseLong(str) * 1000;
return timestamp;
}
I transform the date by calling the method:
//Store date,time and destination on a new Trip.
datePassed = dateText.getText().toString();
timePassed = timeText.getText().toString();
String dateSelected = dateText.getText().toString() + " " + timeText.getText().toString();
Long timestamp = toMilli(dateSelected);
I created a new construction for Trip and store the new timestamp(Long) there and stored the Trip in the database.
public Trip(String destination, Long timestamp) {
this.destination = destination;
this.timestamp = timestamp;
}
To read the data from the firebase:
//Get uId of the user
final String uId = currentFirebaseUser.getUid();
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child(uId).child("trips").getChildren()) {
String destination = child.child("destination").getValue().toString();
Long timestamp = child.child("timestamp").getValue(Long.class);
The next step is to transform Long to String, I created a new method for that.
public String convertTime(long time){
Date date = new Date(time);
Format format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
return format.format(date);
}
And finally call the method:
String time=convertTime(timeLong);
Before reading the data I created a new ArrayList I then create a new object - Trip in my case and passed in the values. Everytime I was reading a child trip - with the for loop I was adding it to the list.
At the end I sorted the list before displaying it to the user. You have to override CompareTo in order for this to work.
Upvotes: 1