Saving and retrieving date in Firebase

后端 未结 5 1340
失恋的感觉
失恋的感觉 2020-12-09 04:12

I have a model with the following structure

public class OfferModel {
    private String mImageUrl;
    private String mOfferCode;
    private String mOffe         


        
相关标签:
5条回答
  • 2020-12-09 04:36

    Use ISO 8601 date format:

    SimpleDateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sss'Z'");
    
    String now = ISO_8601_FORMAT.format(new Date());
    

    Representing Date as a String has two greater advantages:

    • It is human readable ("2018-03-30T13:18:39.516Z" vs 1522415925281)
    • At the browser side, you can simply invokes new Date("2018-03-30T13:18:39.516Z")

    This is quite useful if you have Android and browsers both consuming Firebase data.

    0 讨论(0)
  • 2020-12-09 04:42

    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.

    0 讨论(0)
  • 2020-12-09 04:44

    you can set a date object on Firebase

    yourReference.setValue(new Date());
    

    It works, But

    I noticed other PROBLEM with the date, when i changed the month manually from the db, when retrieving it the value stays as before

    0 讨论(0)
  • 2020-12-09 04:55

    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:

    Hope it helps..

    0 讨论(0)
  • 2020-12-09 04:55

    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.

    0 讨论(0)
提交回复
热议问题