Android format date to MM-DD-YYYY from Datepicker

后端 未结 10 1774
轻奢々
轻奢々 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:18

    I want to display the date in 01-07-2013 MM-dd-yyyy format.

    yes you can Do this by Below Steps.

    Step 1: you First have to Convert your String to Particular Date Format

    Step 2: Using SimpleDateFormat Convert your Date to MM-dd-yyyy format

    Step 3: Convert to String and Use it.

    you can User Below Code for this.

     public class DateFormat {
    
       public static void main(String[] args) {
        // Put here what your are getting as String
        String mytime="Jan 7, 2013"; 
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MMM dd, yyyy"); //Step1: Convert yout String to perticular Date Formate according to your Date String your are getting
        Date myDate = null;
        try {
            myDate = dateFormat.parse(mytime);
    
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        //Step2
        SimpleDateFormat timeFormat = new SimpleDateFormat("MM-dd-yyyy");
        String finalDate = timeFormat.format(myDate); 
        //Step3: Here you will get your Final Date. Set your String now to textview.
           }
    }
    

    Hope it will help you.

提交回复
热议问题