How to retrieve data from firebase where the value of a date is 1 month ago from current date

前提是你 提交于 2021-02-16 14:28:07

问题


 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    Calendar c = Calendar.getInstance();
    String date = sdf.format(c.getTime());

i used this code to get the current time. but i want to get all data from firebase 1 month ago. what i must add the code? i used this code to retrieve all without compare anything

 mDatabasetamu.orderByChild("tglkeluar").equalTo(date).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                Tamu tamu = snapshot.getValue(Tamu.class);
                String nama = tamu.getNama();
                String checkin = tamu.getTglmasuk();
                String checkout = tamu.getTglkeluar();
                String kamar = tamu.getKamar();

                txtnama.append(nama + "\n \n");
                txtcheckin.append(checkin +"\n \n" );
                txtcheckout.append(checkout + "\n \n");
                txtkamar.append(kamar + "\n \n");

            }
        }

thankss


回答1:


First of all, if you are going to store and query your dates as string representations, then you should store them on ISO 8601 standard which makes the lexicographical order of string dates also chronological order. So instead of dd-MM-yyyy HH:mm you should store yyyy-mm-dd HH:mm.

But beware of storing dates as strings as your case doesn't store timezone. Consider either using Firestore, which as a Timestamp data type or if you store it as a string, make sure that you are also storing the a full timestamp format including the timezone: yyyy-mm-ddThh:mm:sszzzz

Having said that, with your current date format it will be difficult for you to query records in the last month since the year comes after and you would get records in old years included in the results.

But if you would store the date as yyyy-mm-dd HH:mm then you would be able to do as follows to get all guest visits from the last month (from one month ago and up until today).

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");

String today = sdf.format(Calendar.getInstance().getTime());
String oneMonthAgo = sdf.format(Calendar.getInstance().add(MONTH, -1).getTime());

mDatabasetamu
  .orderByChild("tglkeluar")
  .startAt(oneMonthAgo )
  .endAt(today)
  .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                Tamu tamu = snapshot.getValue(Tamu.class);
                String nama = tamu.getNama();
                String checkin = tamu.getTglmasuk();
                String checkout = tamu.getTglkeluar();
                String kamar = tamu.getKamar();

                txtnama.append(nama + "\n \n");
                txtcheckin.append(checkin +"\n \n" );
                txtcheckout.append(checkout + "\n \n");
                txtkamar.append(kamar + "\n \n");

            }
        }

Finally, here's some good videos to get started with Firebase if you're coming from an SQL background: The Firebase Database For SQL Developers



来源:https://stackoverflow.com/questions/55206491/how-to-retrieve-data-from-firebase-where-the-value-of-a-date-is-1-month-ago-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!