Android format date to MM-DD-YYYY from Datepicker

后端 未结 10 1707
轻奢々
轻奢々 2020-12-16 11:46

I have a datepicker. My app is pushing a notification. I want to display the date in 01-07-2013 MM-dd-yyyy format. Please check my code below:

 //---Button v         


        
10条回答
  •  囚心锁ツ
    2020-12-16 12:34

    A simple way to do this might be to simply collect the data and then use String.format:

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
    
        String dateString = String.format("%02d-%02d-%d", month, day, year);
        ((TextView) m_view.findViewById(R.id.dob)).setText(dateString);
    

提交回复
热议问题