Skip to content

Instantly share code, notes, and snippets.

@devDeejay
Last active September 4, 2018 16:52
Show Gist options
  • Select an option

  • Save devDeejay/91730eeee5e73e90bf00f71dd102f235 to your computer and use it in GitHub Desktop.

Select an option

Save devDeejay/91730eeee5e73e90bf00f71dd102f235 to your computer and use it in GitHub Desktop.
// 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