Ahm*_*asi 28 android datepicker datetimepicker timepicker
我有日期和时间的DatePicker和TimePicker.现在我想将选定的日期和时间更改为毫秒.我怎样才能做到这一点???
例如,我选择了日期2-5-2012,时间是20:43
现在我有这个日期时间转换成毫秒像
DateTimeInMilliseconds =1234567890
Sam*_*Sam 49
您可以使用DatePicker和TimePicker中的值创建Calendar对象:
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
long startTime = calendar.getTimeInMillis();
Run Code Online (Sandbox Code Playgroud)
Eri*_*ric 11
将两个字符串合并在一起,并使用它们进行解析SimpleDateFormat.
像这样的东西:
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();
Run Code Online (Sandbox Code Playgroud)
IDEOne上的示例:http://ideone.com/nOJYQ4
您可以使用此代码
String string_date = "12-December-2012";
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
Date d = f.parse(string_date);
long milliseconds = d.getTime();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43164 次 |
| 最近记录: |