简体   繁体   English

Android SQLite在将时间传递给对象之前在onCreate方法中转换时间

[英]Android SQLite convert time in onCreate method before passing it into object

currently I have a TimePickerDialog where the user will select the time and the time will be passed into an object's parameters to create a row in my SQLite database. 当前,我有一个TimePickerDialog,用户将在其中选择时间,并将时间传递到对象的参数中,以在SQLite数据库中创建一行。 I want to store the time as 24 hour in the database, but I want it to be displayed to the user in 12 hour format. 我想将时间以24小时的形式存储在数据库中,但是我希望以12小时的格式将其显示给用户。 This is my TimePickerDialog code: 这是我的TimePickerDialog代码:

TimePickerDialog.OnTimeSetListener time = new 
TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            if (hourOfDay < 10) {
                hourString = "0"
                        + hourOfDay;
            } else {
                hourString = String.valueOf(hourOfDay);
            }
            if (minute < 10) {
                minuteString = "0" + minute;
            } else {
                minuteString = String.valueOf(minute);
            }

            String twentyFourTime = hourString + ":" + minuteString;

            tvScheduleTime.setText(twentyFourTime);

        }
    };

Then on my onCreate method, I get the time from tvScheduleTime to save in the database 然后在我的onCreate方法上,我从tvScheduleTime获取了保存到数据库中的时间

btnSaveSchedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // schedule items
                String scheduleName = etScheduleName.getText().toString();
                String scheduleDesc = etScheduleDesc.getText().toString();
                String scheduleDate = Day + " " + selectedMonth + " " + Year;
                String scheduleTime = tvScheduleTime.getText().toString();

                int routeID = 12345;

                Log.w("RESULTS", scheduleTime + scheduleDate);

                DatabaseScheduleHelper db = new DatabaseScheduleHelper(getApplicationContext());
                db.addSchedule(new ObjectSchedule(scheduleTime, scheduleDate, scheduleName, scheduleDesc, routeID));


            }
        });

Essentially, my question is, I want the scheduleTime to be passed in the ObjectSchedule object to be in 24 hour format and but for the text in tvScheduleTime to be in 12 hour format . 本质上,我的问题是,我希望scheduleTime24小时格式ObjectSchedule对象中传递,但ObjectSchedule中的文本以12小时格式 tvScheduleTime But how can I return the value of twentyFourTime from the TimePickerDialog listener to my onCreate method? 但是,如何从TimePickerDialog侦听器向我的onCreate方法返回二十twentyFourTime时间的值? Then from there I will be able to do all my conversions. 然后,从那里我将可以完成所有转换。

If I get this right you just want a method to convert military time to normal time immediately when you get it (get it through DB or through OnTimeSetListener)? 如果我做对了,您只是想要一种在获得时立即将军事时间转换为正常时间的方法(通过数据库或通过OnTimeSetListener获得)? If so the method below should work (not tested). 如果是这样,下面的方法应该可以工作(未经测试)。

int hr = grab from DB or from TimeListener int hr =从数据库或从TimeListener中获取

hr = convertToNormalTime(hr);

private int convertToNormalTime(int militaryTime){
    int hr = militaryTime;
    if(hr > 12){
        hr = militaryTime - 12; // to convert military to normal time always subtract by 12
    }
    return hr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM