在 Android 中将日期和时间转换为毫秒 [英] Convert date and time to milliseconds in Android

查看:133
本文介绍了在 Android 中将日期和时间转换为毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自 DatePickerTimePickerDateTime.现在我想将选定的日期和时间更改为毫秒.我该怎么做???

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???

例如,我选择了 Date 2-5-2012Time20:43

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

DateTimeInMilliseconds = 1234567890

推荐答案

您可以使用 DatePicker 和 TimePicker 中的值创建日历对象:

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();

这篇关于在 Android 中将日期和时间转换为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆