如何设置12小时格式的时间选择器与AM&下午?

时间:2017-02-02 06:58:48

标签: java android timepicker

我想以12小时的格式存储时间。

时间选择器工作正常但我只想在EditText中选定的时间之后设置AM或PM。

this is my TimePicker theme

在这里,我的完整代码

  starttime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Initialize a new time picker dialog fragment
            DialogFragment dFragment = new TimePickerFragment();

            // Show the time picker dialog fragment
            dFragment.show(getActivity().getFragmentManager(),"Time Picker");

        }
    });

TimePickerDialog.java

   package com.Weal.sachin.omcom;

/**
 * Created by sachin on 2/1/2017.
 */

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.TextView;
import android.app.DialogFragment;
import android.app.Dialog;
import java.util.Calendar;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        // Get a Calendar instance
        final Calendar calendar = Calendar.getInstance();
        // Get the current hour and minute
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);

        /*
            Creates a new time picker dialog with the specified theme.

                TimePickerDialog(Context context, int themeResId,
                    TimePickerDialog.OnTimeSetListener listener,
                    int hourOfDay, int minute, boolean is24HourView)
         */

        // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHT
        TimePickerDialog tpd = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false);

        // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_DARK
        TimePickerDialog tpd2 = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,hour,minute,false);

        // TimePickerDialog Theme : THEME_HOLO_DARK
        TimePickerDialog tpd3 = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_HOLO_DARK,this,hour,minute,false);

        // TimePickerDialog Theme : THEME_HOLO_LIGHT
        TimePickerDialog tpd4 = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_HOLO_LIGHT,this,hour,minute,false);

        // TimePickerDialog Theme : THEME_TRADITIONAL
        TimePickerDialog tpd5 = new TimePickerDialog(getActivity(),
                AlertDialog.THEME_TRADITIONAL,this,hour,minute,false);

        // Return the TimePickerDialog
        return tpd;
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute){
        // Do something with the returned time
        TextView tv = (TextView) getActivity().findViewById(R.id.start_time);
        tv.setText( hourOfDay + ":" + minute );

    }
}

3 个答案:

答案 0 :(得分:2)

使用此逻辑找到上午

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    String AM_PM ;
    if(hourOfDay < 12) {
        AM_PM = "AM";
    } else {
        AM_PM = "PM";
    }
}

答案 1 :(得分:1)

这个答案是最好的选择,几行代码。

@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second)
{
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
    calendar.set(Calendar.MINUTE,minute);
    calendar.set(Calendar.SECOND,second);

    textViewStartTime.setText(startTimeFormat().format(calendar.getTime()));
}

public SimpleDateFormat startTimeFormat(){
    return this.startTimeFormat = new SimpleDateFormat("hh:00 a");
}

OutPut将: - 上午12:00

答案 2 :(得分:0)

尝试它会帮助你,

public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {
        String hourString = "";
        if(hourOfDay < 12) {
            hourString = hourOfDay < 10 ? "0"+hourOfDay : ""+hourOfDay;
        } else {
            hourString = (hourOfDay - 12) < 10 ? "0"+(hourOfDay - 12) : ""+(hourOfDay - 12);
        }
        String minuteString = minute < 10 ? "0"+minute : ""+minute;
        String secondString = second < 10 ? "0"+second : ""+second;
        String am_pm = (hourOfDay < 12) ? "am" : "pm";
        String time = hourString+":"+minuteString + " " + am_pm;
        time.setTextColor(getResources().getColor(R.color.dark_blue));
        time.setText(time);
    }