How to set date time values?
In my android application, I want to set the date and 开发者_Python百科time in TextView
. In that date value I have selected and stored as a String
. Now I want to set the String
date value and current time.
I tried the following code. It did displayed stored date value but the time as 12.00 AM. I want current time?
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());
output as: 02-Aug-2011 12:00 AM
Need: 02-Aug-2011 [current time]
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);
Try this,
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+" ");
Just use this, to set current time:
Calendar c = Calendar.getInstance();
dateObj.setTime(c.getTimeInMillis());
精彩评论