我在Android应用中做了以下两件事。首先,在点击编辑文本时,我正在打开一个Datepicker。我以12/5/2014格式点击edittext上的日期和日期集。我想以2014年5月12日的格式设置它。这该怎么做。其次,在点击另一个edittext时,我打开一个TimePicker,它在点击时以1:30的形式设置ediitext上的时间。我想把它设置为下午1:30。这该怎么做?我在下面发布我的代码,请一步一步指导我。
DatePicker代码
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
private void updateLabel() {
// TODO Auto-generated method stub
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
datepick.setText(sdf.format(myCalendar.getTime()));
}
};
datepick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(getActivity(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
} );
TimePicker代码
tasktime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker=new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
tasktime.setText( hourOfDay + ":" + minute);
}
}, hour, minute, true);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
答案 0 :(得分:1)
将日期格式从“MM / dd / yy”更改为“dd MMM,yyyy” 并且在时间中使用simpleDateFomate也使用格式“h:mm a”
答案 1 :(得分:0)
// try this way,hope this will help you....
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Time chosenDate = new Time();
chosenDate.set(dayOfMonth, monthOfYear, year);
long dt = chosenDate.toMillis(true);
CharSequence strDate = DateFormat.format(MM/dd/yy", dt);
datepick.setText(strDate.toString());
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar date = Calendar.getInstance();
Time chosenDate = new Time();
chosenDate.set(0, minute, hourOfDay, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.MONTH), date.get(Calendar.YEAR));
long dt = chosenDate.toMillis(true);
CharSequence strDate = DateFormat.format("kk:mm", dt);
tasktime.setText(strDate);
}
答案 2 :(得分:0)
使用 SimpleDateFormat 。使用以下方法并以字符串格式提供日期作为参数
public static String dateFormatter(String date)
{
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
String s = "";
try {
Date dt = sdf.parse(date);
sdf = new SimpleDateFormat("dd-MMM-yyyy");
s = sdf.format(dt);
// Log.v("*****", s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
对于时间,请参考以下链接: Convert time value to format “hh:mm Am/Pm” using Android