0

Here is my INPUT DATE:

String myDate = "2010-02-19";

I want to get OUTPUT from above DATE like below:

Friday, 19-Feb-2010

I tried many way for getting exact output but not success yet.

Here is my code that i trying using DateTimeFormatter and SimpleDateFormat:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String myDate = "2010-02-19";
            String strDateTimeFormatter, strSimpleDateFormat;

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, dd-MMM-yyyy", Locale.US);
            LocalDate localDate = LocalDate.parse(myDate, dateTimeFormatter);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, dd-MMM-yyyy");
            strSimpleDateFormat = simpleDateFormat.format(myDate);

            strDateTimeFormatter = String.valueOf(localDate);

            Log.d("TAG", "DateTimeFormatter: "+strDateTimeFormatter);
            Log.d("TAG", "SimpleDateFormat: "+strSimpleDateFormat);

        }
    });

I get java.time.format.DateTimeParseException: Text '2010-02-19' could not be parsed at index 0 from the following line:

            LocalDate localDate = LocalDate.parse(myDate, dateTimeFormatter);

How to get the actual OUTPUT that i want?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • `but not success yet.` curious, what output is showing for what you've tried ? – a_local_nobody May 20 '21 at 08:41
  • 1
    ParseException, because `YYYY-MM-dd` cannot be parsed with `EEEE, dd-MMM-yyyy` – Zoe May 20 '21 at 08:46
  • You are using java.time, the modern Java date and time API, and its `DateTimeFormatter`, that’s good. Then don’t try to involve `SimpleDateFormat` too since it is a troublesome and long outdated class. – Ole V.V. May 21 '21 at 05:29
  • Leave out the formatter from parsing since you are parsing a string in the default format. Just `LocalDate localDate = LocalDate.parse(myDate);`. After that `localDate.format(dateTimeFormatter)` will give you `Friday, 19-Feb-2010`. – Ole V.V. May 21 '21 at 05:33
  • Return same as myDate.@Ole V.V. – Shahriar Rahman Shihab May 22 '21 at 05:41
  • @ShahriarRahmanShihab Sorry, I have got no idea what you mean. If `localDate.format(dateTimeFormatter)` returns `2010-02-19` on your computer, it’s weird and nothing that I can explain. You may want to post that as a new question including a [mre]. Please ping me here if you want me to take a look at your new question. – Ole V.V. May 22 '21 at 08:24

0 Answers0