Android format date to MM-DD-YYYY from Datepicker

后端 未结 10 1709
轻奢々
轻奢々 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:22
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
     public class MainClass {
         public static void main(String[] args) {
         String pattern = "MM-dd-yyyy";
         SimpleDateFormat format = new SimpleDateFormat(pattern);
       try {
          Date date = format.parse("01-07-2013");
          System.out.println(date);
       } catch (ParseException e) {
         e.printStackTrace();
        }
    
        System.out.println(format.format(new Date()));
     }
    }
    

    Hope its helpful.

    You can also refer to the documentation.

    0 讨论(0)
  • 2020-12-16 12:22

    Try this:

            CharSequence formatted = DateFormat.format("MM/dd/yyyy", mDueDate);
            mDueDateView.setText(formatted);
    
    0 讨论(0)
  • 2020-12-16 12:31

    try this

    int year = mDatePicker.getYear();
    int month = mDatePicker.getMonth();
    int day = mDatePicker.getDayOfMonth();
    
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    
    SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
    String strDate = format.format(calendar.getTime());
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题