This discussion is archived
3 Replies Latest reply: Dec 9, 2012 8:47 AM by 977248 RSS

how to add minutes to date in java

816629 Newbie
Currently Being Moderated
I have date in string variable, i have to add/subtract minutes from it, please adivce , how i could do in java 1.5
sample data
String olddate = "12/16/2010 4:58:29 PM"
String minutes1 = "300"
i would like to get value as below for above data
String newdate ="12/16/2010 9:58:29 PM"
  • 1. Re: how to add minutes to date in java
    977248 Newbie
    Currently Being Moderated
    Use SimpleDateFormat to convert the date object to string. Once the string object converted to date object use the getTime() method to get the date in milliseconds.
    Now you can easily adds/substract the date object.

    Remember, the resulting long value from getTime() method is in milliseconds, to get the value in minutes you need to multiply it by 1000 and by seconds in a minutes, that is 60.

    When your subtraction/adds done convert it back to date object.


    This is the codes:

    import java.text.SimpleDateFormat;
    import java.text.ParseException;
    import java.util.Date;


    public class DateCalculator {

         public static void main(String[] args) throws ParseException {
              String olddate = "12/16/2010 4:58:29 PM";
              System.out.println("old Date: "+olddate);
              SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
              long oldDateInMillis = formatter.parse(olddate).getTime();
              String minutes1 = "300";
              
              String newDateAdds = formatter.format(new Date(oldDateInMillis + Long.valueOf(minutes1)*1000*60 ));
              String newDateSubstract = formatter.format(new Date(oldDateInMillis - Long.valueOf(minutes1)*1000*60 ));
              
              System.out.println("new Date [Additions] : "+newDateAdds);
              System.out.println("new Date [Substraction] : "+newDateSubstract);
         }

    }
  • 2. Re: how to add minutes to date in java
    jtahlborn Expert
    Currently Being Moderated
    ari.wayan wrote:
    Use SimpleDateFormat to convert the date object to string. Once the string object converted to date object use the getTime() method to get the date in milliseconds.
    Now you can easily adds/substract the date object.

    Remember, the resulting long value from getTime() method is in milliseconds, to get the value in minutes you need to multiply it by 1000 and by seconds in a minutes, that is 60.
    ...
    No, do not do date logic by hand. that is a great way to get the wrong answer. the correct way to do Date manipulation logic in java is:

    1. parse the String to a Date (using SimpleDateFormat is a common way to do this)
    2. create a Calendar from the Date
    3. use the manipulation methods on Calendar to change the Date, e.g. "Calendar.add(Calendar.HOUR_OF_DAY, 5)"
    4. convert Calendar back to Date and/or String as necessary
  • 3. Re: how to add minutes to date in java
    977248 Newbie
    Currently Being Moderated
    Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points