Convert date and time to milliseconds in Android

前端 未结 4 1847
孤城傲影
孤城傲影 2021-01-03 20:31

I have Date and Time from DatePicker and TimePicker. Now i want to change the s

4条回答
  •  暖寄归人
    2021-01-03 20:53

    Merge the two strings together, and parse them using SimpleDateFormat.

    Something like this:

    String toParse = myDate + " " + myTime; // Results in "2-5-2012 20:43"
    SimpleDateFormat formatter = new SimpleDateFormat("d-M-yyyy hh:mm"); // I assume d-M, you may refer to M-d for month-day instead.
    Date date = formatter.parse(toParse); // You will need try/catch around this
    long millis = date.getTime();
    

    Sample on IDEOne: http://ideone.com/nOJYQ4

提交回复
热议问题