Storing Date input values on Firebase

前端 未结 2 708
自闭症患者
自闭症患者 2021-01-29 05:07

So this is how my code looks like

cropref.child(mycrop.name).push({
                          cropname:mycrop.name,
                          croplocation:mycro         


        
相关标签:
2条回答
  • 2021-01-29 05:30

    Two options

    1) Store the date in a more generic, but human readable format, so right now would be Sunday November 27 at 09:13:38

    20161127091338
    

    2) Store the date as a unix timestamp (in milliseconds) using

    (new Date).getTime() / 1000
    

    There are a lot of variants to #2 so do some research to see which is best for your use case.

    You can save either answer as a string but #1 would be more easily searchable since queries wouldn't require any kind of conversions - to see todays events

    queryStartingAt("20161127") and queryEndingAt("20161127")
    
    0 讨论(0)
  • 2021-01-29 05:31

    You need to convert the date first in your Format. You can use SimpleDateFormatFormat for that.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Now you can easily format your date to this format

    String mynewdate = sdf.format(mycrop.plantdate.getTime());
    

    The Output for today would be:

    2016-11-27
    

    Of course you can reverse that back to a Calendar. I do it this way:

     public static Calendar fromStringtoCalendar(String datestring){
        int year =  Integer.valueOf(datestring.substring(0, 4));
        int month =  Integer.valueOf(datestring.substring(5, 7)) -1;
        int day =  Integer.valueOf(datestring.substring(8, 10));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        return calendar;
    }
    
    0 讨论(0)
提交回复
热议问题