String datetimeString = "6/7/2013 2:55:44 PM"; // A String representing your date java.util.Date result = new Date(); DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); // Below Will Throw Exception in thread "main" java.text.ParseException: Unparseable date: // SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); result = formatter.parse(datetimeString); // Throws ParseException System.out.println("Parse Java Util Date = "+result); System.out.println("formatted: " + formatter.format(result));
Output is:
Parse Java Util Date = Fri Jun 07 14:55:44 GMT+05:30 2013 formatted: 06/07/2013 02:55:44 PM
Refer below posts for more examples:
http://viralpatel.net/blogs/check-string-is-valid-date-java/
http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
No comments:
Post a Comment