日期时间格式

时间:2014-12-08 08:11:38

标签: android android-dialogfragment android-datepicker android-timepicker

我在datepicker中选择日期,得到类似这样的内容是edittext:13/12/2014,但我必须这样显示:13 Dec 2014

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

public EditText activity_edittext;

public DatePickerFragment(EditText edit_text) {
    activity_edittext = edit_text;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        activity_edittext.setText(String.valueOf(day) + "/" + String.valueOf(month + 1 ) + "/" + String.valueOf(year));
    }
}

以同样的方式使用时间选择器,我得到这样的价值:21:12我必须显示如下内容:9:12 pm

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

        public EditText activity_edittext;

        public TimePickerFragment(EditText edit_text)
        {
            activity_edittext = edit_text;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            activity_edittext.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
        }
}

3 个答案:

答案 0 :(得分:4)

使用SimpleDateFormat格式化日期。您可以使用13 Dec 2014格式从13/12/2014 dd MMM yyyy形成try { String dateString = String.valueOf(day) + "/" + String.valueOf(month + 1 ) + "/" + String.valueOf(year); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date date = sdf.parse(dateString); sdf = new SimpleDateFormat("dd MMM yyyy"); String formatedDate = sdf.format(date); } catch (Exception e) {} 日期。

DateFormatSymbols

或者,您只需使用String month = new DateFormatSymbols().getMonths()[month-1]; 转换日期的月份,如下所示

9:12 pm

并将时间格式设为hh:mm a,格式如下{/ 1}}

try {

    String timeString = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    Date time = sdf.parse(timeString);

    sdf = new SimpleDateFormat("hh:mm a");
    String formatedTime = sdf.format(time);

} catch (Exception e) {}

答案 1 :(得分:1)

你错误地设置了。您需要检查它是哪个月,并根据它需要添加其文本。

尝试此设置日期:

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    String month_name="";
    switch(month) {
        case Calendar.JANUARY:
            month_name = "JAN";
        break;
        case Calendar.FEBRUARY:
            month_name = "FEB";

        case Calendar.MARCH:
            month_name = "MAR";
        break;
        case Calendar.APRIL:
            month_name = "APR";

        case Calendar.MAY:
            month_name = "MAY";
        break;
        case Calendar.JUNE:
            month_name = "JUN";

        case Calendar.JULY:
            month_name = "JUL";

        case Calendar.AUGUST:
            month_name = "AUG";
        break;
        case Calendar.SEPTEMBER:
            month_name = "SEP";
        case Calendar.OCTOBER:
            month_name = "OCT";
        break;
        case Calendar.NOVEMBER:
            month_name = "NOV";
        break;
        case Calendar.DECEMBER:
            month_name = "DEC";    
        break;
    }
    activity_edittext.setText(String.valueOf(day) + " " + month_name + " " + String.valueOf(year));
}

要以12小时格式设置时间,您需要检查其时间并相应地设置文本:

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    String hour = "";
    String meridiem = "";
    Calendar datetime = Calendar.getInstance();
    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
    datetime.set(Calendar.MINUTE, minute);

    if (datetime.get(Calendar.AM_PM) == Calendar.AM)
        meridiem = "AM";
    else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
        meridiem = "PM";

    hour = (datetime.get(Calendar.HOUR) == 0) ?"12":String.valueOf(datetime.get(Calendar.HOUR));
    activity_edittext.setText(hour + ":" + String.valueOf(minute) + " " + meridiem);
}

希望这有帮助。

答案 2 :(得分:0)

SimpleDateFormat formatter = new SimpleDateFormat ("dd M yyyy", Locale.getDefault ());
Date date = null;
try {
    date = formatter.parse("your date here");
} catch (Exception e) { }