Android get current date and show it in TextView

前端 未结 10 2133
清歌不尽
清歌不尽 2021-02-14 09:23

I do not know what is wrong with code below -

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layo         


        
相关标签:
10条回答
  • 2021-02-14 09:56
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    
    String currentDateandTime = sdf.format(new Date());    
    tv.setText(currentDateandTime);
    
    0 讨论(0)
  • 2021-02-14 09:58

    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"/>
    
    0 讨论(0)
  • 2021-02-14 10:03
    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);
    
    0 讨论(0)
  • 2021-02-14 10:05

    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));
    
    0 讨论(0)
提交回复
热议问题