Java Date - Android 时间选择器
我在这里无法获得正确的格式。我正在尝试从我的 android 日期选择器中获取正确的日期以将其推入日期对象中。
例如:
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
这些事件处理程序将给我:2011, 7, 5 10,30(如果日期是 2011 年 8 月 5 日,时间是 10:30) 在 Android 中从哪里获取 Am/Pm?
我正在这样做,但它工作不正常:
Date date = new Date(year,monthOfYear,dayOfMonth,hourOfDay,minute);
我需要完成此黑莓代码的 Android 等效项。黑莓日期选择器慷慨地提供了一个日期对象(而不是原始整数):
public void run() {
DateTimePicker datePicker = DateTimePicker.createInstance();
// Set the max time to 24 hours into the future. This allows for selecting clips
// across time zones, but prevents the user from wandering off into no-mans' land.
Calendar maxDateTime = Calendar.getInstance();
maxDateTime.setTime(new Date(System.currentTimeMillis() + 24*3600*1000));
datePicker.setMaximumDate(maxDateTime);
if (datePicker.doModal())
{
Calendar selectedDate = datePicker.getDateTime();
Date beforeDate = selectedDate.getTime();
ClipStore.getInstance().getClips(camera, beforeDate);
}
}
I'm having trouble getting the right format here. I'm trying to get a proper date from my android date picker to shove into a date object.
For example:
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
Those event handlers will give me: 2011, 7, 5 10,30 (if the date was August 5th, 2011 and the time was 10:30) Where do I get Am/Pm in Android?
I'm doing this but its not working right:
Date date = new Date(year,monthOfYear,dayOfMonth,hourOfDay,minute);
I need to accomplish the Android equivalent of this blackberry code. Blackberry date picker graciously provides a date object (rather than raw integers):
public void run() {
DateTimePicker datePicker = DateTimePicker.createInstance();
// Set the max time to 24 hours into the future. This allows for selecting clips
// across time zones, but prevents the user from wandering off into no-mans' land.
Calendar maxDateTime = Calendar.getInstance();
maxDateTime.setTime(new Date(System.currentTimeMillis() + 24*3600*1000));
datePicker.setMaximumDate(maxDateTime);
if (datePicker.doModal())
{
Calendar selectedDate = datePicker.getDateTime();
Date beforeDate = selectedDate.getTime();
ClipStore.getInstance().getClips(camera, beforeDate);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用日历:
一天中的时间从 0 点到 23 点。
Use a Calendar :
The hour of day goes from 0 to 23.
我相信 hourOfDay 使用 24 小时时间,因此它应该是 0 到 23 之间的值(包含 0 和 23)。如果大于 >=12,则为 PM。
I believe that hourOfDay uses 24-hour time, so it should be a value between 0 and 23 inclusive. If it's greater >=12, it's PM.
按照原样,Java.util 将使用军事时间。又名 10:30 指上午 10:30。晚上 10:30 将显示为 22:30。如果您想将其转换为 pm,请尝试以下操作:
By stock the Java.util will use Military time. aka 10:30 refers to 10:30 AM. 10:30 pm will be shown as 22:30. If you want to convert it to pm try something like this: