java - How to parse String "06\24\1989" into Date 06/24/1989 format using SimpleDateFormat -
How to parse the string "06 \ 24 \ 1989" (if the user has entered this way) In format (MM / DD / YYYY) using SimpleDateFormat.
Of course, you can only change all the backslashes to forward slash, and try to parse it with string to use only
SimpleDateFormat > Remember to convert a
date and another to it in the desired format
string Remember to use double backslash in string literals (
\ Is an escape character)
SimpleDateFormat sdfParse = New SimpleDateFormat ("MM \\ dd \\ yyyy"); Simple Data Format sdfFormat = New SimpleDetermat ("MM / DD / Yay"); Try {date date = sdfParse.parse ("09 \\ 24 \\ 1989"); Println (sdfFormat.format (date)); // prints 09/24/1989} catches (parsection e) {System.out.println ("invalid date"); }
new SimpleDateFormat ("MM / dd / yyyy") can do; If you want to validate that this is a valid date.
string input = "09 \\ 24 \\ 1989". ("\", "/"); SimpleDateFormat sdfParse = New SimpleDateFormat ("MM \\ dd \\ yyyy"); {SdfFormat.parse}} Try Hold (ParseException e) {System.out.println ("Invalid Date"); }
Comments
Post a Comment