Java Date - Android 时间选择器

发布于 2024-11-28 13:18:49 字数 1300 浏览 0 评论 0原文

我在这里无法获得正确的格式。我正在尝试从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半寸时光 2024-12-05 13:18:49

使用日历:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DATE, dayOfMonth);
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date date = cal.getTime();

一天中的时间从 0 点到 23 点。

Use a Calendar :

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DATE, dayOfMonth);
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date date = cal.getTime();

The hour of day goes from 0 to 23.

蓝眼睛不忧郁 2024-12-05 13:18:49

我相信 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.

仅冇旳回忆 2024-12-05 13:18:49

按照原样,Java.util 将使用军事时间。又名 10:30 指上午 10:30。晚上 10:30 将显示为 22:30。如果您想将其转换为 pm,请尝试以下操作:

public String ampmChanger (String time) {
  int hour = (int)time.substring(0,2);
  String timeAppendage;
  if (hour > 12) { // For 1 PM and on
    hour -= 12; 
    timeAppendage = " PM"; 
  }
  else if (hour == 12) timeAppendage = " PM"; // For 12 pm
  else timeAppendage = " AM"; // For 0(12AM) to Noon

  return String.valueOf(hour)+time.substring(2)+timeAppendage;
}

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:

public String ampmChanger (String time) {
  int hour = (int)time.substring(0,2);
  String timeAppendage;
  if (hour > 12) { // For 1 PM and on
    hour -= 12; 
    timeAppendage = " PM"; 
  }
  else if (hour == 12) timeAppendage = " PM"; // For 12 pm
  else timeAppendage = " AM"; // For 0(12AM) to Noon

  return String.valueOf(hour)+time.substring(2)+timeAppendage;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文