Search code examples
androidapidatecalendar

how to change date formate this coming data api 2023-01-02 formate 02-01-2023


2023-01-02 are coming from this text api. Not sure how to change them to 02-01-2023```

TextView txt_Date_Number;

txt_Date_Number.setText(post.Date_Number);

txt_Date_Number = findViewById(R.id.tv_date);

How to change date formate android


Solution

  • For Java:

       String dateFromAPI = "2023-01-02";
            try {
                Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateFromAPI);
                String finalDate = new SimpleDateFormat("dd-MM-yyyy").format(dateFromAPI);  
                Log.e("test1", finalDate);
            } catch (Exception exception) {
    
            }
    

    For Kotlin:

            val dateFromAPI = "2023-01-02";
            val date: Date = SimpleDateFormat("yyyy-MM-dd").parse(dateFromAPI)
            val finalDate=DateFormat.format("dd-MM-yyyy", date)  //output 02-01-2023