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.

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"); }   

Of course, you can only change all the backslashes to forward slash, and try to parse it with 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

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -