Skip to Content

Java String to java.sql.Date Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.         Java String to java.sql.Date Example
  3.         This Java String to java.sql.Date example shows how to convert Java String object
  4.         containing date to java.sql.Date object.
  5.  */
  6.  
  7. import java.sql.Date;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10.  
  11. public class ConvertStringToJavaSQLDateExample {
  12.        
  13.         public static void main(String args[]) throws ParseException{
  14.                
  15.                 //string containing date
  16.                 String strDate = "2011-12-31 00:00:00";
  17.        
  18.                 /*
  19.                  * To convert String to java.sql.Date, use
  20.                  * Date (long date) constructor.
  21.                  *
  22.                  * It creates java.sql.Date object from the milliseconds provided.
  23.                  */
  24.        
  25.                 //first convert string to java.util.Date object using SimpleDateFormat
  26.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
  27.                 java.util.Date date = sdf.parse(strDate);
  28.                
  29.                 java.sql.Date sqlDate = new Date(date.getTime());
  30.                
  31.                 System.out.println("String converted to java.sql.Date :" + sqlDate);
  32.         }
  33.        
  34. }
  35.  
  36. /*
  37. Output of above given String to java.sql.Date example would be
  38. 2011-01-31
  39. */