Saving and retrieving date in Firebase

I have a model with the following structure

public class OfferModel {
    private String mImageUrl;
    private String mOfferCode;
    private String mOfferTitle;
    private String mOfferDescription;
    private boolean mIsRunning;
    private String mCreatorUid;
    private Date mStartDate;
}

Everything else works fine on saving. It saves in Firebase Realtime database as

startDate
    date: 22
    day: 3
    hours: 23
    minutes: 20
    month: 5
    seconds: 50
    time: 1466617850476
    timezoneOffset: -330
    year: 116

But when I try to retrieve it, the date gives the following error -

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.Date
at com.localvine.models.OfferModel.<init>(OfferModel.java:37)
at com.localvine.managers.OfferManager$1.onDataChange(OfferManager.java:62)
at com.google.android.gms.internal.zzafp.zza(Unknown Source)
at com.google.android.gms.internal.zzagp.zzSu(Unknown Source)
at com.google.android.gms.internal.zzags$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:615)

I understand that Firebase doesn't support Java Date object, but since it's saving them in a map, how can I get back the date from that map? Is there any proper way of saving and retrieving dates in Firebase Android?


You can store the date as an epoch date. It's a long that you can get using your system time System.currentTimeMillis(); or by using the Firebase server time with their ServerValue.TIMESTAMP . The thing with the first option is that it changes with timezones and system settings. So if you store the date as a long, you just have to change your OfferModel field mStartDate to a long and then use new Date(long) to get the corresponding Date when retrieving the object.


This is how I managed to store Date on Firebase by using the SimpleDateFormat to convert it to only a date without seconds, hours or milliseconds.

public void storeDatetoFirebase() {

    handler = new Handler();

    runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
            try {
                Date date = new Date();
                Date newDate = new Date(date.getTime() + (604800000L * 2) + (24 * 60 * 60));
                SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
                String stringdate = dt.format(newDate);

                System.out.println("Submission Date: " + stringdate);
                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("My_Date");
                databaseReference.child("init_date").setValue(stringdate);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable, 1 * 1000);
}

and this is how it appears on Firebase:

日期在Firebase

Hope it helps..


Although firebase does support Date format datatype, I would prefer to save date-time in a string format, because by storing it as a string it becomes quite easy to retrieve it.

To store current system date-time in a string format just below code will do.

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String strDate = dateFormat.format(date).toString();
        myRef.child("datetime").setValue(strDate);   

Now one can use the strDate to store in the firebase or do whatever one want to do with that string.

链接地址: http://www.djcxy.com/p/46660.html

上一篇: 根据用户设置将JSON日期解析为NSDate

下一篇: 在Firebase中保存和检索日期