2

这是我的代码,它显示当前时间和日期我想在编辑文本中仅显示当前日期,但同时显示日期和时间

这一行同时显示 lblDateAndTime.setText(fmtDateAndTime.format(myCalendar.getTime()));

                           public class MainActivity extends Activity {
DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
EditText lblDateAndTime;
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    myCalendar.set(Calendar.MINUTE, minute);
    updateLabel();
}
};

private void updateLabel() {
    lblDateAndTime.setText(fmtDateAndTime.format(myCalendar.getTime()));
}

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
lblDateAndTime = (EditText) findViewById(R.id.lblDateAndTime);
Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        new DatePickerDialog(MainActivity.this, d, myCalendar
                .get(Calendar.YEAR),  
  myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});
//
Button btnTime = (Button) findViewById(R.id.btnTime);
btnTime.setOnClickListener(new View.OnClickListener() {
    public  void onClick(View v) {
        new TimePickerDialog(MainActivity.this, t, myCalendar
                .get(Calendar.HOUR_OF_DAY), myCalendar
                .get(Calendar.MINUTE), true).show();
    }
});

updateLabel();
}// onCreate
} // class
4

4 回答 4

4
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    editText.setText(dateFormat.format(new Date())); // it will show 16/07/2013
    dateFormat = new SimpleDateFormat("dd MMM yyyy");
    editText.setText(dateFormat.format(new Date())); // it will show 16 Jul 2013        
于 2013-07-16T09:55:54.263 回答
3

而不是使用myCalendar.getTime()

您可以使用:

myCalendar.get(Calendar.DATE) + " "
    + (myCalendar.get(Calendar.MONTH) + 1) + " "
    + myCalendar.get(Calendar.YEAR)
于 2013-07-16T09:09:19.063 回答
1

按照这个..

  Calendar c = Calendar.getInstance();

  Log.v("hari","CurrentTime:"+c.getTime());

 SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
 String formattedDate1 = df1.format(c.getTime());
 Log.v("hari","CurrentDate_Format1:"+formattedDate1 );

 SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
 String formattedDate2 = df2.format(c.getTime());
Log.v("hari","CurrentDate_Format2:"+formattedDate2 );

 SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
 String formattedDate3 = df3.format(c.getTime());
 Log.v("hari","CurrentDate_Format3:"+formattedDate3 );
于 2013-07-16T09:55:06.127 回答
0
String date_n = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault()).format(new Date());
TextView date  = (TextView) findViewById(R.id.dateText);
date.setText(date_n);
于 2018-10-30T06:04:35.757 回答