如何在Android上正确格式化日期?

时间:2017-10-01 11:52:04

标签: java android date-formatting

我正在构建IOS和Android版本,在我的IOS中,日期的格式是这样的" 2017年6月23日上午8:58"。

在Android中,我在数字中获取日期,然后我将它们分配为:

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

 date_time = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
 timePicker();
}

这是timePicker()方法:

private void timePicker(){
        // 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) {

                        mHour = hourOfDay;
                        mMinute = minute;

                        et_show_date_time.setText(date_time+" "+hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        timePickerDialog.show();
    }

导致类似这样的事情" 27-6-2017 16:55"

我如何格式化类似于IOS版本?

1 个答案:

答案 0 :(得分:0)

您可以通过SimpleDateFormat

执行此操作
Date currentTime = Calendar.getInstance().getTime();
    String newFormat="";
    try
    {

        SimpleDateFormat form = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
        newFormat = form.format(currentTime);
        tvAppliedDate.setText(newFormat);


    }
    catch (Exception e)
    {

        e.printStackTrace();
        Toast.makeText(this, "parse error", Toast.LENGTH_SHORT).show();
    }