Saving and retrieving date in Firebase

后端 未结 5 1341
失恋的感觉
失恋的感觉 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: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..

提交回复
热议问题