在 TextView 中显示当前日期

Show CurrentDate In TextView

Basically I need to show the current date in my text view then when I click the date it will open the date picker. I already solved the problem but it doesn't show me the current date. when I click the text view it will show me the date picker then I have to select the date. how can I get the current date on my text view?

////set the Current Date in Textview

        dateView1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);


                DatePickerDialog dialog = new DatePickerDialog(getContext(),android.R.style.Theme_DeviceDefault_Dialog,setListener,year,month,day);
                dialog.show();

                
            }
        });


        setListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month = month + 1;


                String date = month + "-" + day + "-" + year;
                dateView1.setText(date);
                dateView1.setText(date);


            }

        };

Click Here To see the image, what i have wanted to is show current date here.

试试这个

import java.time.format.DateTimeFormatter;  
import java.time.LocalDateTime;    
public class Main{
public static void main(String[] args) {
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd");  
    LocalDateTime now = LocalDateTime.now();  
    System.out.println(dtf.format(now));  
}

}

android工作室

dateView1.setText(dtf.format(now));

相同主题 ->