0

i have an database of order and it order date then it order date will be uploaded to database like this (3-3-2020)

then in my shop application, i call the database using JSON, and i want to convert 3-3-2020 to Tuesday, March, 2020

i try this solution

    pertemuan = getIntent().getStringExtra("pertemuan");

    SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy");
    String tanggal = format.format(pertemuan);

    pertemuanview.setText(tanggal);

but it give me this error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.appguru/com.app.appguru.Detail_Pesanan}: java.lang.IllegalArgumentException: Cannot format given Object as a Date

then how to reformat the date?

  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Mar 05 '20 at 18:44
  • Are you storing the date as text (`char` datatype) in your database? Given that your database engine has a `date` datatype, it’s recommended that you use it. – Ole V.V. Mar 05 '20 at 20:11

1 Answers1

1

You are passing wrong object in format.format it suppose to be Date object and you are passing String thats why it is throwing IllegalArgumentException. First you need to convert your string date to date object and pass to formatter like below.

    SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy");
    SimpleDateFormat currentDateformat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = null;
    try {
        date = currentDateformat.parse("3-3-2020");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String tanggal = format.format(date);

Hope this will help you.

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24