Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>With current date and time.</p> <pre><code>public static void main(String[] args) { try { final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss"; final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US); Date sample = new Date(); int iMinutes = 30;//minutes added by the user Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, Integer.valueOf(sample.getMinutes())); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); String dt = sdf.format(cal.getTime()); System.out.println("Current Date and time:"+sample); System.out.println("Date and time with added Minutes : " + (dateFormat.parse(dt))); } catch (ParseException ex) { Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <p>The output will be displays as:<br> Current Date and time:Tue Jun 12 15:57:55 IST 2012<br> Date and time with added Minutes : Tue Jun 12 16:54:55 IST 2012<br> Here the minutes "57" was added to the calendar and the time has moved forward by "30" mins.And that is the your result(Date and time with added Minutes).<br><br></p></li> <li><p>With user in input minutes.</p> <pre><code>public static void main(String[] args) { try { final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss"; final DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US); int iMinutes = 30;//minutes added by the user Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, iMinutes); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); String dt = sdf.format(cal.getTime()); System.out.println("Current Date and time:"+sample); System.out.println("Date and time with added Minutes : " + (dateFormat.parse(dt))); } catch (ParseException ex) { Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <p>This will work as per your desire first you take the minutes from the user and assign that minutes to the "iMinutes" variable of the code it will add that much minutes to the calander.<br><br> The output will be displayed as:<br> Current Date and time:Tue Jun 12 16:07:55 IST 2012<br> Date and time with added Minutes : Tue Jun 12 16:37:55 IST 2012</p></li> </ol> <p>And if you want to set the minutes then use "set" instead of "add" in the "cal.add".</p> <p>Hope this will solve your problem.<br> Regards.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload