Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get current date and show it in TextView

Tags:

date

android

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());

}
like image 624
user1492748 Avatar asked Oct 17 '12 12:10

user1492748


People also ask

Which view is used to display the date and time in Android application?

TextClock can display the current date and/or time as a formatted string. This view honors the 24-hour format system setting.


1 Answers

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"/>
like image 167
Munene iUwej Julius Avatar answered Oct 18 '22 15:10

Munene iUwej Julius