0

First of all , i know there are lots of questions regarding my same problem.But that was not enough to solve my problem.So if anyone can provide anything then it would be great. In my term the date comes as string format first and after that i need to convert it into date.But i was not able to do that.

I want the format as the format string has .Like as date should be as like as 04-02-2019.

Here is my java code:

    Date date=null;
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    try {
        date=dateFormat.parse(paymentDate);
        Log.v("Date:",""+date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

Note : paymentDate="04-02-2019" Exception:

java.text.ParseException: Unparseable date: "04-02-2019"
at java.text.DateFormat.parse(DateFormat.java:362)
6
  • Try with: SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Commented Feb 4, 2019 at 17:05
  • Thanks.But putting 3M(MMM) was causing the problem? Commented Feb 4, 2019 at 17:13
  • My bad.I will check the docs. Commented Feb 4, 2019 at 17:15
  • @SakhawatHossain commonly to change a date format from 04-02-2019 to 04 Feb 2019 then you need to read in the date and then reformat it. It cannot read it in and reformat at the same time. Commented Feb 4, 2019 at 17:19
  • As an aside consider throwing away the long outmoded and notoriously troublesome SimpleDateFormat and friends, and adding 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. Commented Feb 6, 2019 at 9:19

1 Answer 1

2

Format dd-MMM-yyyy means date in format 04 Feb 2019 // 25 Mar 1993 etc.

Replace it with dd-MM-yyyy

Please have a look once again at your complied files. It should work.

Here is the code running as online snippet:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Still not the format i want.Your format gave me this (Thu Feb 07 00:00:00 GMT+06:00 2019).May be there is something wrong that i am not able to catch up.
Could you please update the question so we can run the code snippet?
Could you add also initializatio for paymentDate?
paymentDate has the value as paymentDate="04-02-2019"
Thanks for your suggestions. I followed your code too , but i am getting date=Mon Feb 04 00:00:00 GMT+06:00 2019 . I used the exact same code as yours.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.