Servlet Date Operations
Getting date from the user Through HTML Form Since we are using bootstrap, date input must use bootstrap standard gui One of the plugins out there bootstrap-datetimepicker.min.js Jquery bootstrap date picker Can format the input MY format – mm-dd-yyyy
MYSQL DATE DATE DATETIME Conversion is needed Yyyy-mm-dd DATETIME Yyyy-mm-dd hh:mm:ss – NOW | current time and date Conversion is needed From dd-mm-yyyy to yyyy-mm-dd
Java string to date conversion String input from html form must be parsed into a java date object. //convert string to date String traveldate = request.getParameter("traveldate"); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Date date = new Date(); try { date = formatter.parse(traveldate); } catch (Exception ex) { }
MY to mysql format conversion //convert to mysql date formatter = new SimpleDateFormat("yyyy-MM-dd"); traveldate = formatter.format(date);
NOW() function for mysql Exist in php for getting the current time and date Usually use to capture transaction date NOW() funtion not exist in Java Need to do manually
Current time and date in Java //get current date and time Date currentdate = new Date(); //format directly to mysql date SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String bookingdate = formatter.format(currentdate);
MYSQL TO JAVA DATE CONVERSION MYSQL DATE TO JAVA DATE //yyyy-MM-dd String traveldate = rs.getString("traveldate"); //convert traveldate string to date (still mysql date) SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); try { date = formatter.parse(traveldate); } catch (Exception ex) {}
//convert mysql date to MY date formatter = new SimpleDateFormat("dd-MM-yyyy"); traveldate = formatter.format(date);
MYSQL TO JAVA DATE CONVERSION MYSQL DATETIME TO JAVA DATETIME //yyyy-MM-dd HH:mm:ss //parse the date from string for date conversion String bookingdate = rs.getString("bookingdate"); formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date = new Date(); try { date = formatter.parse(bookingdate); } catch (Exception ex) {}
//convert to MY format dd-MM-yyyy formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a"); //formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss a"); //formatter = new SimpleDateFormat("dd MMMM, yyyy HH:mm:ss a"); bookingdate = formatter.format(date); a – AM/PM LET SAY MONTH IS NOVEMBER MM – 11 MMM – NOV MMMM – NOVEMBER