0

I am trying to convert string to date using date format and used the following code but its shows an error.

      public static Date ConvertStringtodate(String Date) throws ParseException {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date Teststart = dateFormat.parse(Date);
            return Teststart;
        }

        public static void main(String[]agrs) throws ParseException {
            System.out.println(ConvertStringtodate("2022.02.10 17:54:55"));
        }

Here is the error

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date at java.text.DateFormat.format(DateFormat.java:310) at java.text.Format.format(Format.java:157)

4
  • hh in your format pattern should be HH - read the documentation for SimpleDateFormat closely to see the difference. Commented Feb 10, 2022 at 12:08
  • Although it's odd that the exception mentions format when you're actually calling parse - please could you include the full stack trace? Commented Feb 10, 2022 at 12:10
  • Thanks @JonSkeet This is what i received in console Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date at java.text.DateFormat.format(DateFormat.java:310) at java.text.Format.format(Format.java:157) at pages.CommonFunctionOfpages.ConvertStringtodate(CommonFunctionOfpages.java:317) at pages.CommonFunctionOfpages.main(CommonFunctionOfpages.java:322) Process finished with exit code 1 Commented Feb 10, 2022 at 12:16
  • 1
    The code you've provided wouldn't produce that stack trace - it would produce a stack trace calling java.text.DateFormat.parse. (I've just tried your exact code.) In future, please make the code you provide reflect the error you provide. As well as the answer you've received, I'd strongly advise: a) start following Java naming conventions; b) use java.time instead of the old java.util.Date and java.text.SimpleDateFormat classes. Commented Feb 10, 2022 at 12:36

2 Answers 2

3

At the main method, you are sending date as 2022.02.10 17:54:55. However, you wrote format of the pattern as yyyy-MM-dd hh:mm:ss. Change the pattern at the SimpleDateFormat constructor as yyyy.MM.dd HH:mm:ss.

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

Comments

0

The problem for me was the slashes '/' in the date input. for some reason the. The input string was "01/01/1991" instead of "01-01-1991". So I just replaced the slashes with dashes and everything worked just fine.

private Date convertStringToDate(String payload) throws ParseException {
        payload = payload.replace("/", "-");
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
        java.util.Date utilDate = formatter.parse(payload);
        return new Date(utilDate.getTime());
    }

Comments

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.