How to get datepicker value in date format?

后端 未结 5 1035
礼貌的吻别
礼貌的吻别 2021-02-08 05:55

I have a problem with my Datapicker

i use the code for getting date,month & year shown below

           DatePicker datePicker;
           datePicker         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-08 06:24

    You can format the date like this :

    int   day  = datePicker.getDayOfMonth();
    int   month= datePicker.getMonth();
    int   year = datePicker.getYear();
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    String formatedDate = sdf.format(calendar.getTime());
    

    You can parse the String back to Date object by calling

    Date date = sdf.parse(formatedDate);
    

提交回复
热议问题