如何从android中的timepicker选择值减少时间间隔?

时间:2016-08-30 10:32:50

标签: android

我正在开发一个应用程序,在这个我创建的时间选择器,我想减少时间戳选择值的时间6小时(即06:00像hh:mm)。以下是我使用的代码。 例: 如果用户选择12:30 pm,则必须在textview中显示06:30 pm。 我该怎么做呢?

//java file
public class ProfileActivity4 extends AppCompatActivity implements View.OnClickListener{

    private TextView textView;
    private EditText eddate, edtime;
    private boolean loggedIn = false;
    Button btn;
    EditText edname,edaddress;
    Button ptime, pdate;

    TextView tvsname, tvsprice;
    NumberPicker numberPicker;
    TextView textview1,textview2;
    Integer temp;
    String pname, paddress, email, sname, sprice;
    private int mYear, mMonth, mDay, mHour, mMinute;
    private DrawerLayout drawerLayout;
    private Toolbar toolbar;

    Spinner spinnerPick;
    TextView tdpicktime;
    String picktime;



    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile4);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // back button pressed

                Intent i = new Intent(ProfileActivity4.this, List_Activity1.class);
                startActivity(i);
            }
        });

        //    initNavigationDrawer();

        //Initializing textview
        textView = (TextView) findViewById(R.id.textView);
        edname=(EditText)findViewById(R.id.ed_pname);
        edaddress=(EditText)findViewById(R.id.ed_add);
        tvsname=(TextView)findViewById(R.id.textView_name);
        tvsprice=(TextView)findViewById(R.id.textView2_price);
        btn=(Button)findViewById(R.id.button);

        spinnerPick=(Spinner)findViewById(R.id.spinner_picktime);
        tdpicktime=(TextView)findViewById(R.id.textView_picktime);

        //    ArrayAdapter<String> sAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.rides));
        ArrayAdapter<String> sAdapter1 = new ArrayAdapter<String>(this, R.layout.spinner_item, getResources().getStringArray(R.array.flighttype));
        spinnerPick.setAdapter(sAdapter1);

        spinnerPick.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                picktime = spinnerPick.getSelectedItem().toString();

                }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        eddate=(EditText)findViewById(R.id.editText_date);
        edtime=(EditText)findViewById(R.id.editText_time);

        ptime=(Button)findViewById(R.id.button_ptime);
        pdate=(Button)findViewById(R.id.button_pdate);

        ptime.setOnClickListener(this);
        pdate.setOnClickListener(this);

        Intent i = getIntent();
        // getting attached intent data
        final String name = i.getStringExtra("company_name");
        // displaying selected product name
        tvsname.setText(name);

        String price = i.getStringExtra("sedan_price");
        // displaying selected product name
        tvsprice.setText(price);

        textview1 = (TextView)findViewById(R.id.textView1_amount);
        // textview2 = (TextView)findViewById(R.id.textView_seats);

        final int foo = Integer.parseInt(price);
        textview1.setText(String.valueOf(foo));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
                String email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available");
                textView.setText(email);

                if (loggedIn) {

                    submitForm();

                    Intent intent = new Intent(ProfileActivity4.this, Confirm_Activity4.class);
                    intent.putExtra("company_name", name);
                    startActivity(intent);
                }
            }
        });

    }

    private void submitForm() {

        String pname = edname.getText().toString();
        String paddress = edaddress.getText().toString();
        String sname = textview1.getText().toString();
        String time = edtime.getText().toString();
        String date = eddate.getText().toString();
        String email= textView.getText().toString();

        new SignupActivity3(this).execute(pname,paddress,sname,time,date,email);

    }

    @Override
    public void onClick(View v) {

        if (v == pdate) {

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {

                            eddate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
        if (v == ptime) {

            // Get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {

                      edtime.setText(hourOfDay + ":" + minute);
                        }
                    }, mHour, mMinute, false);

            timePickerDialog.show();
        }
      }
}

2 个答案:

答案 0 :(得分:0)

使用时间戳选择日期初始化新的日历对象。

    Calendar calendar = Calendar.getInstance(); // this would default to now
    calendar.setTime(/*selected timepicker time*/); //or with epoch (long value like 1472554134000 with calendar.setTimeInMillis())
    calendar.add(Calendar.HOUR_OF_DAY, -6)

如果您使用12小时制,请使用 Calendar.HOUR 其他 Calendar.HOUR_OF_DAY

答案 1 :(得分:0)

TL;博士

LocalTime.parse( "12:30" ).minusHours( 6 ).toString()

java.time

ThreeTenABP项目中使用现代日期时间类,这是Java 8及更高版本中内置的the back-portjava-time的Android改编版。

您的输入和输出恰好采用标准ISO 8601格式。默认情况下,这些标准格式在java.time类中用于解析/生成字符串。所以不需要指定格式化模式;直接解析。

LocalTime类表示没有日期且没有时区的时间。

LocalTime lt = LocalTime.parse( "12:30" );
LocalTime earlier = lt.minusHours( 6 );
String output = earlier.toString();