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

问题: SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm"); Calendar c = Calendar.getInstance(); String date = sdf.format(c.getTime()); i used this code to...

问题:

 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

  • 发表于 2019-03-19 01:51
  • 阅读 ( 226 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除