Android: Passing Date in putExtra

前端 未结 4 1980
温柔的废话
温柔的废话 2021-01-07 17:34

I\'m launching an activity and would like to pass a Date(Time) value to it. I\'ve passed all my other parameters with i.putExtra(\"noteName\", \"Hello Wor

相关标签:
4条回答
  • 2021-01-07 17:38

    Use date.getTime() and date.setTime() and transfer it as a Long.

    i.putExtra("date", date.getTime());
    
    Date d = new Date();
    d.setTime(i.getLongExtra("date", -1));
    
    0 讨论(0)
  • 2021-01-07 17:53

    Instead of

    i.putExtra("date", date.getTime());
    Date d = new Date();
    d.setTime(i.getLongExtra("date", -1));
    

    USE : if you are using Calendar instead of Date to get the long value

    i.putExtra("date", date.getTime().getTime); 
    Date d = new Date();
    d.setTime(i.getLongExtra("date", -1));
    
    0 讨论(0)
  • 2021-01-07 17:55

    better convert into a long, use putExtra(String name, long value) and recreate then back in the new Activity.

    0 讨论(0)
  • 2021-01-07 17:55

    I've simply used

    i.putExtra("noteDate",myDate);
    

    and then on the activity I used:

    Date dt = new Date(extras.getString("noteDate"));
    

    and it works like a charm!? Is this dangerous? To assume the date will always be parsed correctly on all devices?

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