Search code examples
androiddatetimedatetimepicker

Convert a combined Date and Time String to long value?


I am trying to get long value of a string (date & time string), but it is not working. What I am trying to do is:

  • Choose date form datepicker and store it in a String

  • Choose time from timepicker and store it in a String

Then I concatenate these two strings and get long value from that string.

I have tried a few Date formatters but I am unable to get this done. The format of my string is dd-MM-yyy h:mm a. Please help me out of this. Provide any utility available for this.


Solution

  • Forget about strings, and go directly with the values:

    DatePicker dp = (DatePicker) findViewById...
    TimePicker tp = (TimePicker) findViewById...
    
    Date timeStamp = new Date( dp.getYear(), dp.getMonth(), dp.getDay(), tp.getHour(), tp.getMinute(), 0 );
    
    long longTime = timeStamp.getTime();