How to convert String and date time to java

advertisements

Hi everyone i used bootstrap date time picker to save date and time when i select the date in jsp. am getting date and time as a String in java class like this 23 January 2017 - 02:50 pm am trying to convert this to date is not working where i need to change

$(function () {
   $('#datetimepicker8').datetimepicker({
      startDate: new Date(),
      format   : 'dd MM yyyy - HH:ii p',
      autoclose: 1,
   }).on( 'changeDate', function(e) {
      // Revalidate the date field
      $('#timeTableUpdateForm').bootstrapValidator(
         'revalidateField', 'examDate');
   });
});

.

public class StringToDate {
   public static void main(String[] args) {
      SimpleDateFormat formatter =
         new SimpleDateFormat("dd MMMM yyyy-HH:mm:ss a");
      String dateInString = "23 January 2017 - 02:50 pm";
      try {
         Date date = formatter.parse(dateInString);
         System.out.println(date);
         System.out.println(formatter.format(date));
      }
      catch( ParseException e ) {
         e.printStackTrace();
      }
   }
}


Here is a working code:

   public static void main( String[] args ) {
      final SimpleDateFormat formatter =
         new SimpleDateFormat( "dd MMMMM yyyy - HH:mm a", Locale.US );
      final String dateInString = "23 January 2017 - 02:50 pm";
      try {
         final Date date = formatter.parse(dateInString);
         System.out.println(date);
         System.out.println(formatter.format(date));
      }
      catch( final ParseException e ) {
         e.printStackTrace();
      }
   }

With this output :

Mon Jan 23 02:50:00 CET 2017
23 January 2017 - 02:50 AM