Skip to Content

Add or substract seconds to current time using Java Calendar

  • 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.   Add or substract seconds to current time using Java Calendar
  3.   This example shows how to add or substract seconds in current time  
  4.   using Java Calendar class.
  5. */
  6.  
  7. import java.util.Calendar;
  8.  
  9. public class AddSecondsToCurrentDate {
  10.  
  11.   public static void main(String[] args) {
  12.    
  13.     //create Calendar instance
  14.     Calendar now = Calendar.getInstance();
  15.    
  16.     System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY)
  17.                       + ":"
  18.                       + now.get(Calendar.MINUTE)
  19.                       + ":"
  20.                       + now.get(Calendar.SECOND));
  21.                      
  22.     //add seconds to current date using Calendar.add method
  23.     now.add(Calendar.SECOND,100);
  24.  
  25.     System.out.println("New time after adding 100 seconds : "
  26.                       + now.get(Calendar.HOUR_OF_DAY)
  27.                       + ":"
  28.                       + now.get(Calendar.MINUTE)
  29.                       + ":"
  30.                       + now.get(Calendar.SECOND));
  31.  
  32.     /*
  33.      * Java Calendar class automatically adjust the date,hour or minutesaccordingly
  34.      * if adding seconds to the current time causes current minute,
  35.      * hour or date to be changed.
  36.      */
  37.      
  38.  
  39.     //substract seconds from current time using Calendar.add method
  40.     now = Calendar.getInstance();
  41.     now.add(Calendar.SECOND, -50);
  42.  
  43.     System.out.println("Time before 50 minutes : " + now.get(Calendar.HOUR_OF_DAY)
  44.                       + ":"
  45.                       + now.get(Calendar.MINUTE)
  46.                       + ":"
  47.                       + now.get(Calendar.SECOND));
  48.    
  49.   }
  50. }
  51.  
  52. /*
  53. Typical output would be
  54. Current time : 16:37:13
  55. New time after adding 100 seconds : 16:38:53
  56. Time before 50 minutes : 16:36:23
  57. */