Skip to Content

Java Date to String 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 Date to String Example
  3.         This Java Date to String example shows how to convert java.util.Date to
  4.         String in Java.
  5.  */
  6.  
  7. import java.text.DateFormat;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10.  
  11. public class ConvertDateToStringExample {
  12.  
  13.         public static void main(String args[]){
  14.                
  15.                 //create new java.util.Date object
  16.                 Date date = new Date();
  17.                
  18.                 /*
  19.                  * To convert java.util.Date to String, use SimpleDateFormat class.
  20.                  */
  21.                
  22.                 /*
  23.                  * crate new SimpleDateFormat instance with desired date format.
  24.                  * We are going to use yyyy-mm-dd hh:mm:ss here.
  25.                  */
  26.                 DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
  27.                
  28.                 //to convert Date to String, use format method of SimpleDateFormat class.
  29.                 String strDate = dateFormat.format(date);
  30.                
  31.                 System.out.println("Date converted to String: " + strDate);
  32.                
  33.         }
  34. }
  35.  
  36. /*
  37. Output of above given java.util.Date to String example would be
  38. Date converted to String: 2011-17-10 11:17:50
  39. */