在我的Android应用程序中,我想在TextView
中设置日期和时间。在该日期值中,我选择并存储为String
。现在我想设置String
日期值和当前时间。
我尝试了以下代码。它确实显示了存储的日期值,但时间为12.00 AM。我想当前时间?
String lbl_date="02-Aug-2011";
Date dateObj = new Date(lbl_date);
SimpleDateFormat postFormater = new SimpleDateFormat("dd-MMM-yyyy hh:mm aa",Locale.ENGLISH);
String newDateStr = postFormater.format(dateObj);
txt_date_start.setText(newDateStr.toString());
输出为:02-Aug-2011 12:00 AM
需要:02-Aug-2011 [current time]
答案 0 :(得分:2)
试试这个,
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);
int mHour = c.get(Calendar.HOUR_OF_DAY);
int mMinute = c.get(Calendar.MINUTE);
TextView t=(TextView)findViewbyid(R.id.text);
t.setText(mYear+" "+mMOnth+" "+mDay+" ");
答案 1 :(得分:2)
Date now = new Date();
String s;
Format formatter;
formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm aa",Locale.ENGLISH);
s = formatter.format(date);
txt_date_start.setText(a);
答案 2 :(得分:1)
只需使用它,设置当前时间:
Calendar c = Calendar.getInstance();
dateObj.setTime(c.getTimeInMillis());