Last active
September 4, 2018 16:52
-
-
Save devDeejay/91730eeee5e73e90bf00f71dd102f235 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Printing Message To Get User Input In Specified Format | |
| System.out.println("Enter Date In Format : DD-MM-YYYY"); | |
| String inputStringDate = input.next(); | |
| System.out.println("You entered " + inputStringDate); | |
| // Define The Format Of Date as dd-MM-yyyy (As per your requirements) | |
| SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); | |
| java.util.Date parsedDate = null; | |
| java.sql.Date sqlDate = null; | |
| // Try Catch Block to handle Parsing Exceptions while parsing date | |
| // You can use throws in method declaration too | |
| try { | |
| //Parsing Dates | |
| parsedDate = format.parse(inputStringDate); | |
| sqlDate = new java.sql.Date(parsedDate.getTime()); | |
| } catch (ParseException e) { | |
| System.out.println("Invalid Date Format"); | |
| e.printStackTrace(); | |
| } | |
| // Your SQL Date is ready, it holds just the Date information and not the Time information. | |
| // Just pass it as java.sql.Date to your Database layer | |
| // Insert into prepared statement using something like : | |
| // In your Database Layer | |
| preparedStatement.setDate(columnIndex, sqlDate); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment