[英]How to set the Date in the format 12 May,2014 and Time in the format 1:30 PM using Datepicker and Timepicker in Android?
I have done the following two things in my Android app. 我在Android应用程序中做了以下两件事。 Firstly, on clicking an edittext I am opening a Datepicker. 首先,在单击一个编辑文本时,我要打开一个Datepicker。 I click on a date and the date sets on the edittext in the format 12/5/2014. 我单击一个日期,然后在日期上以12/5/2014格式设置日期。 I want to set it in the format like 12 May,2014. 我想将其设置为2014年5月12日这样的格式。 how to do this. 这个怎么做。 Secondly, on clicking another edittext I open a TimePicker which on click sets the time on the ediitext in the form 1:30. 其次,在单击另一个编辑文本时,我打开一个TimePicker,单击该按钮将以1:30的形式在ediitext上设置时间。 I want to set it like 1:30 PM. 我想将其设置为下午1:30。 How to do this? 这个怎么做? I am posting my codes below, please guide me step by step. 我将代码发布在下面,请逐步引导我。
DatePicker code 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 code 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();
}
});
将日期格式从“ MM / dd / yy”更改为“ dd MMM,yyyy”,然后在时间中使用simpleDateFomate并使用格式“ h:mm a”
// 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);
}
Use SimpleDateFormat . 使用SimpleDateFormat 。 Use the method below and provide your date in string format as a parameter 使用以下方法,并以字符串格式提供日期作为参数
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;
}
For Time, please refer the link below : Convert time value to format “hh:mm Am/Pm” using Android 对于时间,请参考以下链接: 使用Android将时间值转换为格式“ hh:mm Am / Pm”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.