17

在我的应用程序中,我在文本视图中将时间显示为 07:00 PM。单击文本视图时,会弹出一个时间选择器对话框,在该时间选择器中,我必须显示与文本视图中出现的时间完全相同的时间。但是,我不知道该怎么做。

代码

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm a");
        //int hr = 0;
        Date date = null;
        try 
        {
            date = sdf.parse(resDateArray[3]);
        } 
        catch (ParseException e) {
            e.printStackTrace();
        }
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //tp is reference variable for time picker

        tp.setCurrentHour(calendar.get(Calendar.HOUR));
        tp.setCurrentMinute(calendar.get(Calendar.MINUTE));

        }//else
4

7 回答 7

38

您应该使用 aSimpleDateFormatDate您的String. 然后打电话

picker.setCurrentHour(date.getHours())

picker.setCurrentMinute(date.getMinutes())

由于该Date对象已被弃用,因此您应该使用 aCalendar来代替它。您可以通过这种方式实例化 a Calendar

Calendar c = Calendar.getInstance();
c.setTime(date);
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendat.MINUTE));

编辑:完整代码:

import java.util.Date;
import java.text.SimpleDateFormat; //Don't use "android.icu.text.SimpleDateFormat"
import java.text.ParseException; //Don't use "android.net.ParseException"

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Date date = null;
try {
    date = sdf.parse("07:00");
} catch (ParseException e) {
}
Calendar c = Calendar.getInstance();
c.setTime(date);

TimePicker picker = new TimePicker(getApplicationContext());
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendar.MINUTE));
于 2012-09-19T12:02:16.427 回答
4

如果有人使用以下格式而不使用 timepicker 小部件,请使用这个..

mStartTime.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            new TimePickerDialog(AddAppointmentActivity.this, onStartTimeListener, calendar
                    .get(Calendar.HOUR), calendar.get(Calendar.MINUTE), false).show();

        }
    });

TimePickerDialog.OnTimeSetListener onStartTimeListener = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String AM_PM;
        int am_pm;

        mStartTime.setText(hourOfDay + " : " + minute + "  " + AM_PM);
        calendar.set(Calendar.HOUR, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);

    }
};
于 2013-10-11T10:55:16.337 回答
4

我创建了这个辅助函数来获取时间

        public static void showTime(final Context context, final TextView textView) {

    final Calendar myCalendar = Calendar.getInstance();
    TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            String am_pm = "";
            myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            myCalendar.set(Calendar.MINUTE, minute);
            if (myCalendar.get(Calendar.AM_PM) == Calendar.AM)
                am_pm = "AM";
            else if (myCalendar.get(Calendar.AM_PM) == Calendar.PM)
                am_pm = "PM";
            String strHrsToShow = (myCalendar.get(Calendar.HOUR) == 0) ? "12" : myCalendar.get(Calendar.HOUR) + "";
            //UIHelper.showLongToastInCenter(context, strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);
            textView.setText(strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);
        }
    };
    new TimePickerDialog(context, mTimeSetListener, myCalendar.get(Calendar.HOUR), myCalendar.get(Calendar.MINUTE), false).show();
}

以及如何以所需格式获取当前时间,我使用以下代码。// PARAM Date 是您要格式化的时间日期 // PARAM currentFormat of date // PARAM requiredFormat of date

    public static String getFormattedDate(String date, String currentFormate, String requiredFormate) {
    //SimpleDateFormat formatActual = new SimpleDateFormat("yyyy - MM - dd");
    if (date != null) {
        SimpleDateFormat formatActual = new SimpleDateFormat(currentFormate);
        Date dateA = null;
        try {
            dateA = formatActual.parse(date);
            System.out.println(dateA);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //SimpleDateFormat formatter = new SimpleDateFormat("dd MMM, yyyy");
        SimpleDateFormat formatter = new SimpleDateFormat(requiredFormate);
        String format = formatter.format(dateA);
        System.out.println(format);
        return format;
    } else {
        return "";
    }

}

getFormattedDate 的用法是:

       String date = getFormattedDate(dateYouWantToFormate,"hh:mm a","hh:mm");
于 2014-04-08T15:40:19.653 回答
2

如果您现在使用 TimePicker,最好检查构建版本,因为 setCurrentHour 和 setCurrentMinute 已被弃用。

SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
Date date = null;
try {
    date = sdf.parse("07:00");
} catch (ParseException e) {
}
Calendar c = Calendar.getInstance();
c.setTime(date);

TimePicker timePicker = new TimePicker(getApplicationContext());

if (Build.VERSION.SDK_INT >= 23) {
    timePicker.setHour(calculateHours());
    timePicker.setMinute(calculateMinutes());
}
else {
    timePicker.setCurrentHour(calculateHours());
    timePicker.setCurrentMinute(calculateMinutes());
}
于 2018-09-05T01:32:30.187 回答
2

在函数中检索时间时必须牢记一点,日历实例中onTimeSet()没有要设置的特殊函数。AM_PM当我们设置时间时AM_PM会自动设置相应的时间,例如如果在选择小时和分钟时选择了 AM,那么<calendarVariable>.get(Calendar.AM_PM)将返回 0,而在相反的情况下(在 PM 的情况下)它将返回 1。

于 2019-11-05T06:55:08.823 回答
0

对于 AM/PM,您必须为此实现一个逻辑,如下所示.. 这样您就可以获得您提到的输出...只需尝试下面的代码...

int hr=tp.getCurrentHour();
String a="AM";
if(hr>=12){
    hr=hr-12;
    a="PM";
}
String tm=hr+":"+tp.getCurrentMinute()+" "+a;

Toast.makeText(MainActivity.this, tm, 500).show();
于 2012-09-20T07:44:14.600 回答
0

sdk 23版之后的一些变化;

            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
            Date date = null;
            try {
                date = sdf.parse("12:02:32");
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(date != null) {
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                sleepTimeStartPicker.setHour(c.get(Calendar.HOUR_OF_DAY));
                sleepTimeStartPicker.setMinute(c.get(Calendar.MINUTE));
            }
于 2019-06-24T14:30:12.137 回答