如何从Firebase检索日期值为距当前日期1个月前的数据 [英] How to retrieve data from firebase where the value of a date is 1 month ago from current date

查看:36
本文介绍了如何从Firebase检索日期值为距当前日期1个月前的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我使用此代码获取当前时间.但是我想在1个月前从firebase获取所有数据.我必须添加什么代码?我用这段代码检索了所有内容而没有进行任何比较

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");

            }
        }

谢谢

推荐答案

首先,如果要以字符串表示形式存储和查询日期,则应将它们存储在

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.

但是请注意将日期存储为字符串,因为您的案例不存储时区.考虑使用 Firestore(作为时间戳数据类型),或如果您将其存储为字符串,请确保还存储了完整的时间戳格式,包括时区: yyyy-mm-ddThh:mm:sszzzz

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.

但是,如果您将日期存储为 yyyy-mm-dd HH:mm ,则可以执行以下操作以获取上个月(一个月前和直到今天).

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");

            }
        }

最后,如果您来自SQL背景,以下是一些不错的入门Firebase视频:

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

这篇关于如何从Firebase检索日期值为距当前日期1个月前的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆