1

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

  • Use java.time: `LocalDate.parse("2023-01-02").format(DateTimeFormatter.ofPattern("dd-MM-uuuu"))` gives `02-01-2023`. – Ole V.V. Jan 03 '23 at 18:48

1 Answers1

0

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

Code Chef
  • 31
  • 1
  • 7
  • TextView txt_Date_Number; txt_Date_Number.setText(post.Date_Number); txt_Date_Number = findViewById(R.id.tv_date); how to set this text – jayaraman raj Jan 02 '23 at 04:43
  • I think you are making a mistake while assigning id with textview variable. Your sequence is not correct. It should be like. TextView txt_Date_Number; txt_Date_Number = findViewById(R.id.tv_date); txt_Date_Number.setText(post.Date_Number); – Code Chef Jan 02 '23 at 04:58
  • To set text in your TextView you need to add this code. ```String dateFromAPI = post.Date_Number //"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); TextView txt_Date_Number; txt_Date_Number = findViewById(R.id.tv_date); txt_Date_Number.setText(finalDate); } catch (Exception exception) { } ``` – Code Chef Jan 02 '23 at 05:02
  • bro this text coming api txt_Date_Number.setText(post.Date_Number) api text how to convert date formate exmple 2023-01-02 convert 02-01-2023 – jayaraman raj Jan 02 '23 at 05:05
  • I have already mentioned in above comment and in answer how can you convert date format. – Code Chef Jan 02 '23 at 05:09
  • @ode Chef ok sorry – jayaraman raj Jan 02 '23 at 05:10
  • If my answer is useful for you so please up_vote it – Code Chef Jan 02 '23 at 05:12
  • why crash application Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.Tamiltv.newspaper.models.Recipe.Date_Number' on a null – jayaraman raj Jan 02 '23 at 05:30
  • In your models class Date_Number is null. First set its value – Code Chef Jan 02 '23 at 09:09
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table). – Ole V.V. Jan 03 '23 at 18:44