如何在android中以DD-MMM-YYYY格式设置日期

时间:2016-08-08 06:30:44

标签: android datepicker android-datepicker datepickerdialog

Edittext CheckIndate当我选择checkindate日期将在2016-08-08追加 但我想要像2016年8月8日这样的格式 我该如何实现这一点请帮助我。

CheckInDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick (View v){
    if (v == CheckInDate) {
        final Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, mDay);

        DatePickerDialog dpd = 
            new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar cal = Calendar.getInstance();

                    //     Calendar cal = new GregorianCalendar();

                    cal.set(Calendar.MONTH, monthOfYear);
                    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    cal.set(Calendar.YEAR, year);
                    if (cal.before(c)) {
                        Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG)
                             .show();
                        return;
                    }

                    StringBuilder date = new StringBuilder();
                    date.delete(0, date.length());
                    date.append(year)
                        .append("-")
                        .append((monthOfYear + 1) < 10 ? "0" : "")
                        .append((monthOfYear + 1))
                        .append("-")
                        .append((dayOfMonth < 10 ? "0" : ""))
                        .append(dayOfMonth);

                    CheckInDate.setText(date.toString());
                }
            }, mYear, mMonth, mDay);
        dpd.getDatePicker().setMinDate(c.getTimeInMillis());
        dpd.show();
    }

};

3 个答案:

答案 0 :(得分:0)

你可以这样转换来自datepicker的日,月和年,并像这样传递:

DatePickerDialog dpd =new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    String mDate=convertDate(convertToMillis(yourDay,yourMonth,yourYear));
                }
            }, mYear, mMonth, mDay);
           dpd.getDatePicker.setMinDate(c.getTimeInMillis());
          dpd.show();



    public String convertDate(long mTime) {
            SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
            String formattedDate = df.format(mTime);
            return formattedDate;
        }

        public long convertToMillis(int day, int month, int year) {
            Calendar calendarStart = Calendar.getInstance();
            calendarStart.set(Calendar.YEAR, year);
            calendarStart.set(Calendar.MONTH, month);
            calendarStart.set(Calendar.DAY_OF_MONTH, day);
            return calendarStart.getTimeInMillis();
        }

答案 1 :(得分:0)

使用它,它将以2016年8月8日的形式返回日期

     private static String formatDate(int year, int month, int day) {
      Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, monthOfYear);
                    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    cal.set(Calendar.YEAR, year);
        Date myDate = cal.getTime();

       String date=new SimpleDateFormat("dd-MMM-yyyy").format(myDate);


    return date;
}

答案 2 :(得分:0)

你可以添加像这样的方法

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.MONTH, monthOfYear);
        cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        cal.set(Calendar.YEAR, year);
        if (cal.before(c)) {
           Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show();
           return;
        }
        int yrr=year;
        int monn=monthOfYear+1;
        int dayy=dayOfMonth;
        CheckInDate.setText(formatDate(yrr, monn, dayy));
}

formatDate方法就在这里..

private static String formatDate(int year, int month, int day) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(0);
    cal.set(year, month, day);
    Date date = cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy");

    return sdf.format(date);
}

修改

以yyyy-MM-dd格式在数据库中插入日期。

简单解决方案将您选择的日期存储在String中(在OnDateSet方法中)并插入字符串。

int yrr=year;
int monn=monthOfYear+1;
int dayy=dayOfMonth;
strDate = yrr + "-" + monn + "-" + dayy;

第二个解决方案从中获取日期 String date = CheckInDate.getText().toString();

调用DateFormater(date)方法获取字符串yyyy-MM-dd。

public String DateFormater (String date)
{
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("dd MMM yyyy");  //format of the date which you send as parameter(if the date is like 08-Aug-2016 then use dd-MMM-yyyy)
    String s = "";
    try {
        Date dt = sdf.parse(date);
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        s = sdf.format(dt);

    } catch (ParseException e) {
        e.printStackTrace();
    }
    return s;
}