29

I have Date and Time from DatePicker and TimePicker. Now i want to change the selected date and time into milliseconds. How can I do this???

For Example I have Date selected 2-5-2012 and Time is 20:43

Now I have to convert this Date Time into milliseconds something like

DateTimeInMilliseconds = 1234567890

Ahmad Abbasi
  • 1,776
  • 6
  • 29
  • 43

4 Answers4

51

You can create a Calendar object with the values from your DatePicker and TimePicker:

Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
             timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
long startTime = calendar.getTimeInMillis();
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
Sam
  • 86,580
  • 20
  • 181
  • 179
  • I like your approach (+1) but I personally avoid the try-catch block overhead whenever possible. Will `"d-M-yyyy"` work for double digit dates, like "12-10-2012"? – Sam Nov 04 '12 at 21:50
  • But he just needs the time in milliseconds, nothing more. So there is overhead because first you get the actual time, then you reset it and only after that you get your milliseconds while there is a Date object that has the getTime() method that returns the milliseconds immediatly. – fonZ Nov 04 '12 at 21:52
  • @Sam Yes. `d` accepts "1-31", `dd` accepts "01-31", same applies for `M` and `MM`. – Cat Nov 04 '12 at 21:52
  • @JonathanCruz Yes, there is a small waste there. But where are you getting your Date object from? For a reason I don't know, there is no direct method to get a Date value from a DatePicker. – Sam Nov 04 '12 at 22:00
  • @krunalshah Don't make such useless edits. Adding the `TimePicker` does not improve the answer one bit, that just adds information which has nothing to do with the answer itself. I already rolled your edit back. – Xaver Kapeller May 22 '15 at 10:35
  • @XaverKapeller, right but i was targeting fresher in android, they become confused with timepicker reference like how it come in declare code and how to initialize so i did. – krunal shah May 25 '15 at 05:32
11

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

Cat
  • 66,919
  • 24
  • 133
  • 141
6

You can use this code

String string_date = "12-December-2012";

SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
Date d = f.parse(string_date);
long milliseconds = d.getTime();
Dawood Ahmed
  • 1,734
  • 4
  • 23
  • 36
4

The getTime() method of the Date class returns a long with the time in milliseconds.

http://developer.android.com/reference/java/util/Date.html

So if you have a Date object somewhere like:

Date date;

You can do:

System.out.println(date.getTime());

And it will print out the time in milliseconds.

fonZ
  • 2,428
  • 4
  • 21
  • 40