Tuesday, April 9, 2013

[android help] Android get current date and show it in TextView

Android get current date and show it in TextView - Stack Overflow



















I do not know what is wrong with code below -



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView1);

String dt;
Date cal = (Date) Calendar.getInstance().getTime();
dt = cal.toLocaleString();
tv.setText(dt.toString());

}





























TextView tv = (TextView) findViewById(R.id.textView1);
SimpleDateFormat dfDate_day= new SimpleDateFormat("E, dd/MM/yyyy HH:mm:ss");
String dt="";
Calendar c = Calendar.getInstance();
data=dfDate_day.format(c.getTime());
tv.setText(dt);

























Use



long date = System.currentTimeMillis();

SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy h:mm a");
String dateString = sdf.format(date);





















Below solution might help -



final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

updateDisplay();

private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
























use this code:



tvDisplayDate = (TextView) findViewById(R.id.tvDate);


final Calendar c = Calendar.getInstance();
yy = c.get(Calendar.YEAR);
mm = c.get(Calendar.MONTH);
dd = c.get(Calendar.DAY_OF_MONTH);

// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(yy).append(" ").append("-").append(mm + 1).append("-")
.append(dd));






















SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
tv.setText(currentDateandTime);


this is the way how i do it






















note*:- import java.text.DateFormat;import java.util.Date;Donot import "import java.sql.Date;"



TextView tv = (TextView) findViewById(R.id.textView1);
String ct = DateFormat.getDateInstance().format(new Date());
tv.setText(ct);



















default






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...