我正在尝试使用自定义格式(例如dd-MMM-yyyy(10-aug-1982))从日期选择器对话框中捕获日期。我使用了以下代码,但只接受整数值作为月而不是字符串。请参阅我想要从日期选择器发送到编辑文本的注释代码。
请任何人指导我如何将月份作为字符串返回。在此先感谢。以下是我的代码。
XML
<EditText
android:id="@+id/date"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#d4d4d4"
android:inputType="none"
android:textIsSelectable="true"
android:hint="Select Date..."
android:padding="15dp"
android:textColor="#897"
android:textColorHint="#090"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
码
package com.example.raufabid.mycalendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText date;
DatePickerDialog datePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date = (EditText) findViewById(R.id.date);
date.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
// SimpleDateFormat mMonth = new SimpleDateFormat("MMM");
// String Month_name = mMonth.format(c);
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
}
}
修改
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate the date picker and a button
date = (EditText) findViewById(R.id.date);
bbc = (EditText) findViewById(R.id.editText2);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
try {
c.setTime(new SimpleDateFormat("MMM").parse("July"));
int mMonth = c.get(Calendar.MONTH) + 1;
//String mMonth = c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// SimpleDateFormat mMonth = new SimpleDateFormat("MMM");
// String Month_name = mMonth.format(c);
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
} catch (ParseException e) {
e.printStackTrace();
}
}
});
}
}
答案 0 :(得分:2)
更改为以下
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
date.setText(dateString);
}
}, mYear, mMonth, mDay);
答案 1 :(得分:1)
使用“ EEEE dd”代替“ dd”来获取日期名称。 就像2019年10月25日星期五。
// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
{
Calendar calendar = Calendar.getInstance();
c.set(year , monthOfYear , dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat("EEEE dd-MMM-yyyy");
String datestring=format.format(calendar.getTime());
editText.setText(datestring);
}
}, mYear, mMonth, mDay);