I do not know what is wrong with code below -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layo
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentDateandTime = sdf.format(new Date());
tv.setText(currentDateandTime);
Consider using a TextClock as it will handle updates for you. For more info here is the documentation Docs . AnalogClock and DigitalClock can also be useful if your objective is just to display current time and date
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:format24Hour="MMM,yyyy\n\thh:mm"/>
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
System.out.println("Currrent Date Time : "+formattedDate);
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));